Skip to content

Instantly share code, notes, and snippets.

@avidas
Forked from keithics/gist:fd53431b6f57014249ca
Created January 28, 2016 03:33
Show Gist options
  • Save avidas/2b73496df5a49821013a to your computer and use it in GitHub Desktop.
Save avidas/2b73496df5a49821013a to your computer and use it in GitHub Desktop.
Mongodump and Amazon Glacier
'use strict';
/**
* Module dependencies.
*/
var mongoose = require('mongoose'),
AWS = require('aws-sdk'),
exec = require('child_process').exec,
fs = require('fs'),
name = new Date().toISOString();
/**
* Create a MongoDump and upload it to Amazon Glacier
*/
exports.dump = function(req, res) {
AWS.config.update({region: 'ap-northeast-1'});
var glacier = new AWS.Glacier();
var destination = 'mongodump/'+name;
var mongodump = 'mongodump --username USERNAME --password PASSWORD --out '+destination+' --db DATABASE';
var zip = 'zip -r '+destination+'.zip '+destination;
var deleteFolder = 'rm -rf '+destination;
function puts(error, stdout, stderr) {
var file = fs.readFileSync(destination+".zip");
var params = {vaultName: 'VAULT_NAME', body: file};
glacier.uploadArchive(params, function(err, data) {
if (err){
console.log("Error uploading archive!", err);
}
else{
//delete zip file
exec(deleteFolder+".zip", function(){
//
});
console.log("Archive ID", data.archiveId);
}
res.send("ok");
});
}
exec(mongodump +' && ' +zip+' &&' + deleteFolder, puts);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment