Last active
October 16, 2021 20:29
-
-
Save deepak-terse/2dec981d4b1ef1076c7dd7ed1de65868 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Problem Statement: | |
- To have a cost-effective data backup backup mechanism for mongodb on a daily basis. | |
Solution: | |
- To write a cron job that will backup data from all collections of a mongo db and store it on the server | |
- 7 days of backup will be maintained at any given point of time (post 7 days of successful backup). This is done so that the DB could be restored to any day in the last 7 days. | |
- Appropriate success/failure logs are maintained for further debugging. | |
- Alos log the number of records that are backed up. | |
- Library used: | |
- `sails-hook-cron` : provides method to write a cron job | |
- `async` : to carry out asynchronous operations | |
- `fs` : for accessing file storage | |
- ``moment` : to name file based on the day of the week | |
*/ | |
const async = require("async"); | |
const fs = require('fs'); | |
const moment = require('moment'); | |
module.exports.cron = { | |
backup: { | |
//Backup scheduled for every day at 00:00 AM | |
schedule: '0 0 0 * * *', | |
onTick: function() { | |
//Get all modelNames | |
const models = Object.keys(sails.models).filter((item) => { | |
return item !== "archive" | |
}); | |
let backup = {}; | |
//Get all data from models and save it in backup | |
async.mapLimit(models, models.length, | |
(modelName, cb) => { | |
sails.models[modelName].find({}).exec((err, result) => { | |
if (err) { | |
cb(err, null); | |
} else if (result) { | |
backup[modelName] = result; | |
cb(null, `${modelName} has ${result.length} records`); | |
} | |
}); | |
}, (err, results) => { | |
backup.logs = results | |
backup.time = new Date(); | |
//To store 7 days of backup at most. Backups after that will be overridden. | |
fs.writeFileSync(`.tmp/public/backup${moment().isoWeekday()}.json`, JSON.stringify(backup)); | |
} | |
) | |
}, | |
start: true | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment