Skip to content

Instantly share code, notes, and snippets.

@swarajgiri
Created September 11, 2014 09:41
Show Gist options
  • Save swarajgiri/71c2b8d0a54718a06968 to your computer and use it in GitHub Desktop.
Save swarajgiri/71c2b8d0a54718a06968 to your computer and use it in GitHub Desktop.
Mongo db back up
var shell = require('shelljs'),
cfg = require('./cfg'),
DSS = require('distack'),
co = require('co'),
filestore = new DSS(cfg.storestack);
require('datejs');
var backupName = new Date().toString('yyyy-MM-dd-HH-mm') + '.tar.gz';
function storeWrite(tags, key, path) {
return function (cb) {
filestore.write(tags, key, path, cb);
};
}
if (! shell.which('mongodump')) {
shell.echo('Sorry, this script requires mongodump');
shell.exit(1);
}
// dumb db
shell.exec('mongodump --db '+ cfg.mongo);
// tar
shell.exec('tar -zcvf dump.tar.gz dump/');
// remove dump folder
shell.exec('rm -r dump');
// rename to current date
shell.exec('mv dump.tar.gz ' + backupName);
co(function* () {
yield storeWrite(
['cloud'],
'db-backups/' + backupName,
backupName
);
})(function (err) {
if (err) {
throw err;
}
// remove local file
shell.exec('rm ' + backupName);
console.log('done');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment