From f295958c443a8b2901b67f3afbf77735a2f8887d Mon Sep 17 00:00:00 2001 From: Alicia Sykes Date: Sun, 21 Apr 2024 14:45:52 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Put=20config=20backups=20in=20own?= =?UTF-8?q?=20directory?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env | 2 +- services/save-config.js | 22 ++++++++++++++++------ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/.env b/.env index 612a0937..c9ca94a2 100644 --- a/.env +++ b/.env @@ -52,7 +52,7 @@ # VUE_APP_VERSION=2.0.0 # Directory for conf.yml backups -# BACKUP_DIR=./user-data/ +# BACKUP_DIR=./user-data/config-backups # Setup any other user defined vars by prepending VUE_APP_ to the var name # VUE_APP_pihole_ip=http://your.pihole.ip diff --git a/services/save-config.js b/services/save-config.js index f946be54..1665c3c4 100644 --- a/services/save-config.js +++ b/services/save-config.js @@ -14,18 +14,23 @@ module.exports = async (newConfig, render) => { return configObj.filename.replaceAll('/', '').replaceAll('..', ''); }; + // Path to config file (with navigational characters stripped) const usersFileName = makeSafeFileName(newConfig); + // Path to user data directory + const userDataDirectory = process.env.USER_DATA_DIR || './user-data/'; + // Define constants for the config file const settings = { - defaultLocation: process.env.USER_DATA_DIR || './user-data/', + defaultLocation: userDataDirectory, + backupLocation: process.env.BACKUP_DIR || path.join(userDataDirectory, 'config-backups'), defaultFile: 'conf.yml', filename: 'conf', backupDenominator: '.backup.yml', }; // Make the full file name and path to save the backup config file - const backupFilePath = `${path.normalize(process.env.BACKUP_DIR || settings.defaultLocation) + const backupFilePath = `${path.normalize(settings.backupLocation) }/${usersFileName || settings.filename}-` + `${Math.round(new Date() / 1000)}${settings.backupDenominator}`; @@ -45,15 +50,20 @@ module.exports = async (newConfig, render) => { message: !success ? errorMsg : getSuccessMessage(), }); - // Makes a backup of the existing config file + // Create a backup of current config, and if backup dir doesn't yet exist, create it await fsPromises - .copyFile(defaultFilePath, backupFilePath) - .catch((error) => render(getRenderMessage(false, `Unable to backup ${settings.defaultFile}: ${error}`))); + .mkdir(settings.backupLocation, { recursive: true }) + .then(() => fsPromises.copyFile(defaultFilePath, backupFilePath)) + .catch((error) => render( + getRenderMessage(false, `Unable to backup ${settings.defaultFile}: ${error}`), + )); // Writes the new content to the conf.yml file await fsPromises .writeFile(defaultFilePath, newConfig.config.toString(), writeFileOptions) - .catch((error) => render(getRenderMessage(false, `Unable to write to ${settings.defaultFile}: ${error}`))); + .catch((error) => render( + getRenderMessage(false, `Unable to write to ${settings.defaultFile}: ${error}`), + )); // If successful, then render hasn't yet been called- call it await render(getRenderMessage(true));