Skip to content

Commit

Permalink
✨ Put config backups in own directory
Browse files Browse the repository at this point in the history
  • Loading branch information
Lissy93 committed Apr 21, 2024
1 parent fa6d192 commit f295958
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 16 additions & 6 deletions services/save-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;

Expand All @@ -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));
Expand Down

0 comments on commit f295958

Please sign in to comment.