-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup.js
More file actions
97 lines (88 loc) · 2.84 KB
/
backup.js
File metadata and controls
97 lines (88 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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');
console.log(backupDirPath);
const dbOptions = {
user: '',
pass: '',
host: '',
port: '',
database: '',
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 db_uri = process.env.DB_URI || '' ; // enter your db url here
let cmd = `mongodump --uri "${db_uri}" --out ${newBackupPath} `
exec(cmd, (error, stdout, stderr) => {
if (error) {
console.log(error);
}
if (this.empty(error)) {
console.log('dumped ready --> ', newBackupPath);
// 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 => {});
}
}
}
});
}
};