Skip to content

Instantly share code, notes, and snippets.

@outbounder
Created December 8, 2014 10:19
Show Gist options
  • Save outbounder/831c91a328b3ac29b5e6 to your computer and use it in GitHub Desktop.
Save outbounder/831c91a328b3ac29b5e6 to your computer and use it in GitHub Desktop.
mongodb-sync.js
/**
* Usage: $ node mongodb-sync.js <user>@<ip> <database-name>
**/
require("shelljs/global")
// prepare remote db
var remote_commands = [
"mongodump --db "+process.argv[3]+" --out /tmp/mongodump-sync",
"tar -zcvf /tmp/mongodump-sync.tar.gz /tmp/mongodump-sync"
]
exec("ssh "+process.argv[2]+" '"+remote_commands.join(";")+"'")
// download locally and install
var local_commands = [
"scp "+process.argv[2]+":/tmp/mongodump-sync.tar.gz /tmp/mongodump-sync.tar.gz",
"mkdir /tmp/mongodump-sync",
"tar -zxvf /tmp/mongodump-sync.tar.gz -C /tmp/mongodump-sync",
"rm /tmp/mongodump-sync/tmp/mongodump-sync/"+process.argv[3]+"/sessions.bson",
"mongorestore /tmp/mongodump-sync/tmp/mongodump-sync/"+process.argv[3],
"rm -rf /tmp/mongodump-sync*"
]
exec(local_commands.join(";"))
// erease leftovers after donwload
exec("ssh "+process.argv[2]+" 'rm -rf /tmp/mongodump-sync*'")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment