Skip to content

Instantly share code, notes, and snippets.

@theouerd
Created November 25, 2019 20:35
Show Gist options
  • Save theouerd/f4ce191299745cc67d4c41de81532354 to your computer and use it in GitHub Desktop.
Save theouerd/f4ce191299745cc67d4c41de81532354 to your computer and use it in GitHub Desktop.
Auto backup function.
const fs = require('fs');
const _ = require('lodash');
const exec = require('child_process').exec;
const path = require('path');
// Concatenate root directory path with our backup folder.
const backupDirPath = path.join(__dirname, 'database-backup');
const dbOptions = {
user: '<databaseUsername>',
pass: '<databasePassword>',
host: 'localhost',
port: 27017,
database: '<databaseName>',
autoBackup: true,
removeOldBackup: true,
keepLastDaysBackup: 2,
autoBackupPath: backupDirPath
};
// return stringDate as a date object.
exports.stringToDate = dateString => {
return new Date(dateString);
};
// Check if variable is empty or not.
exports.empty = mixedVar => {
let undef, key, i, len;
const emptyValues = [undef, null, false, 0, '', '0'];
for (i = 0, len = emptyValues.length; i < len; i++) {
if (mixedVar === emptyValues[i]) {
return true;
}
}
if (typeof mixedVar === 'object') {
for (key in mixedVar) {
return false;
}
return true;
}
return false;
};
// Auto backup function
exports.dbAutoBackUp = () => {
// check for auto backup is enabled or disabled
if (dbOptions.autoBackup == true) {
let date = new Date();
let beforeDate, oldBackupDir, oldBackupPath;
// Current date
currentDate = this.stringToDate(date);
let newBackupDir =
currentDate.getFullYear() +
'-' +
(currentDate.getMonth() + 1) +
'-' +
currentDate.getDate();
// New backup path for current backup process
let newBackupPath = dbOptions.autoBackupPath + '-mongodump-' + newBackupDir;
// check for remove old backup after keeping # of days given in configuration
if (dbOptions.removeOldBackup == true) {
beforeDate = _.clone(currentDate);
// Substract number of days to keep backup and remove old backup
beforeDate.setDate(beforeDate.getDate() - dbOptions.keepLastDaysBackup);
oldBackupDir =
beforeDate.getFullYear() +
'-' +
(beforeDate.getMonth() + 1) +
'-' +
beforeDate.getDate();
// old backup(after keeping # of days)
oldBackupPath = dbOptions.autoBackupPath + 'mongodump-' + oldBackupDir;
}
// Command for mongodb dump process
let cmd =
'mongodump --host ' +
dbOptions.host +
' --port ' +
dbOptions.port +
' --db ' +
dbOptions.database +
' --username ' +
dbOptions.user +
' --password ' +
dbOptions.pass +
' --out ' +
newBackupPath;
exec(cmd, (error, stdout, stderr) => {
if (this.empty(error)) {
// check for remove old backup after keeping # of days given in configuration.
if (dbOptions.removeOldBackup == true) {
if (fs.existsSync(oldBackupPath)) {
exec('rm -rf ' + oldBackupPath, err => {});
}
}
}
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment