I am struggling to find a good way to copy over our production MongoDB database to overwrite our development database on demand. db.copyDatabase doesn't work from the command line using mongo-cli. Is there a better way to clone a database and overwrite a current one with Mongo?
1 Answer
You could use a shell script to do that using the mongodump
and mongorestore
tools.
What I do for this same scenario is to use the --eval
option from the mongo-cli to drop the database, take a dump of the production database and then restore to the development database.
- ssh user@host 'mongodump --host dbhost --port dbport --db dbproduction; cd dump ; zip dump.zip dbproduction/* ;'
- scp user@host:dump/dump.zip /desiredPath/dump.zip ;
- mongo --eval 'new Mongo().getDB("dbdevelopment"); db.dropDatabase();'
- unzip dump.zip -d dump ; cd dump/ ;
- mongorestore --db dbdevelopment dbproduction;
This will need to run from the development database's server with access to the production databases's server and it will do exactly what you need it to: dump and zip the production database, download it, drop the development database, unzip the downloaded file, and restore.