How to backup, restore and duplicate databases using Mongo and Compose (MongoHQ)April 3 2016
I've been battling back and forth with Mongo on Compose trying to duplicate a database I had. I wanted to take our development database and make a new database for production based on the development data. On a single-instance version of Mongo 3.2 you can do this with...
# won't work on compose.io db.copyDatabase('sandbox', 'production')
With the right roles or individual permissions (listCollections etc...) this will spin up a new database called production with all the collections of sandbox.
Unfortunately, all of Compose's instances run on sharded hosts so these commands just fail and complain about permissions which sent me into a fruitless, four hour journey into Mongo's user roles and permissions.
Now, I'll show you how I performed this copy with mongo backup/restore tools on compose.io:
1. Download your backup
Compose keeps tarball backups of your deployments as the raw database information that Mongo reads and writes to. These files are appended by ".ns", ".1" etc... once untarred. What we need to do first is spin up a new local mongo instance that uses the backup files as it's database:
mongod --dbpath ~/Downloads/my-untarred-backup/
Now, in a new terminal tab, we can dump the local database we just made into BSON which can be used for all sorts of things by [mongorestore]:(https://docs.mongodb.org/manual/reference/program/mongorestore/)
mongodump -d sandbox -o ~/Desktop/bson_backup/
Now we have valid .bson backups of all the collections from the database "sandbox". You can do this for all the databases you want to be able to export.
2. Restore from your dump
Now you can go into your Compose browser, make a new database, add yourself as a user and execute a mongorestore from any of your dumped collections:
mongorestore --host link.to.host --db newdbname -u you -p passphrase --port 99999 ~/Downloads/bson_backup/sandbox/
The cool thing about mongorestore is it will copy over your indexes, but rename them to your "newdbname".
Hope that helps someone else searching for how to copy a database from one to another on Compose/MongoHQ.