I have been using Compose.io to host my MongoDB database and quite often I need to test migration scripts against a clone of a database. I have tried several different ways to do a clone, each with pros and cons. Some methods require access to the filesystem, and that's not always possible. Following two methods, I find most practical.
Method 1: mongodump + mongorestore
MongoDB installation contains mongodump and mongorestore command line tools for maintenance of the MongoDB.
To create a backup you need to run following command:
mongodump --host <hostname>:<portname> --db <database_name> -u <username> -p<password> -o /path/to/local/file
Depending on the folder where you want to create a database dump, you might need admin rights to run the command.
Compose.io documentation has an important note:
Please Note: The lack of a space between the -p and your database password is significant.
Which means that you need to write
-p<Password> to get the password right.
After running the command, you should see a folder containing your collections in a binary json format.
Now you have a copy of the database on your local disk and next step is to restore a database to a different name.
By using flag --collection you can specify the collection to be backed up.
Restore is done using mongorestore command. We need a target database and access to it, from user role point of view, you need backup role.
This is an example command, and it's output:
Method 2: Graphical user-interface
If you prefer graphical user-interface, or you want to copy multiple collections, then you might want to try MongoVUE. Unfortunately, it's only on Windows, but don't' worry Robomongo is also for the OSX and Linux. I had a problem when authenticating to the Compose.io, which might have something to do compatibility with the Mongo v3.
With MongoVUE, you can connect to a source database and select which collections should are copied to the target database.
After creating a database connection, you can choose "Copy Collections to Different Server..." from the context-menu.
You should see a dialog window with source collections and target database selection.
Just press Copy and you're ready!
For many developers/DevOps, those two methods are enough, but there are other alternatives to explore. MongoDB documentation covers many aspects, such as using their product MongoDB Cloud Manager. One thing to note is that all methods described will have some impact on the performance.
If you have simple methods how to manage MongoDB database cloning then let me know!