-
Notifications
You must be signed in to change notification settings - Fork 1
/
backup.js
74 lines (62 loc) · 2.06 KB
/
backup.js
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
module.exports = {
backupDatabases(langs, callback) {
const backupFuncArray = [];
langs.forEach(lang => {
function backupDB(cb) {
// First we dump the current database
// Upload to s3
const today = new Date();
const backupName = `videowiki-${lang}__${today.getMonth() + 1}-${today.getDate()}-${today.getFullYear()}-${Date.now()}__gzipped_compressed`;
const outPath = path.resolve(__dirname, BACKUP_DIR, lang, backupName);
const command = `mongodump --uri ${DATABASE_BASE}-${lang} --gzip --archive=${outPath}`
// console.log(command)
exec(command, (err, stdout, stderr) => {
if (err || !fs.existsSync(outPath)) {
console.log('error creating a backup', err, stderr)
return cb();
}
utils.uploadToS3(fs.createReadStream(outPath), `${lang}/${backupName}`, (err, data) => {
console.log(err, data);
cb();
})
})
}
backupFuncArray.push(backupDB);
})
async.series(backupFuncArray, (err, result) => {
})
},
cleanupBucket(callback) {
s3Bucket.listObjects({
Bucket: bucketName,
}, (err, objects) => {
// console.log(err, objects)
const lastWeekDate = new Date();
const toBeDeletedKeys = [];
lastWeekDate.setDate(lastWeekDate.getDate() - 7);
objects.Contents.forEach(element => {
if (new Date(element.LastModified).getTime() < lastWeekDate.getTime()) {
toBeDeletedKeys.push({
Key: element.Key
});
}
console.log(new Date(element.LastModified).getTime());
});
if (toBeDeletedKeys.length > 0) {
console.log('deleting old backups', toBeDeletedKeys);
s3Bucket.deleteObjects({
Bucket: bucketName,
Delete: {
Objects: toBeDeletedKeys,
Quiet: false,
},
}, (err, data) => {
if (err) return callback(err);
return callback(null, data);
})
} else {
return callback(null);
}
})
}
}