Skip to content

Instantly share code, notes, and snippets.

@keithics
Last active February 2, 2017 02:49
Show Gist options
  • Save keithics/fd53431b6f57014249ca to your computer and use it in GitHub Desktop.
Save keithics/fd53431b6f57014249ca 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);
};
@avidas
Copy link

avidas commented Jan 28, 2016

Doesn't seem like mongoose dependency is necessary here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment