Backing up and restoring a mongodb to a different database

I’m using mongodb as my NoSQL database of choice for I needed a quick way to backup the mongo database to another server, and then load that production database in a test environment locally so I could do some testing.

Here is how I backed up mongodb using mongodump as described here:

First, ssh to the production database server, then use mongodump with the –db argument to specify which database to dump:

This will create a ./dump folder that you can then tar up and gzip and transfer down to your local development server.

Now I wanted to restore the production database locally so I can do some bug fixes that only surfaced with the larger production database.

For mongorestore you give it the target database to import into with –db and the last argument is the path to the original production database that you unzipped on the local filesystem inside ./dump/example_production. BE SURE TO BACKUP YOUR LOCAL DB FIRST.

Its worth noting my database is not very large, as my node.js app is still very small, so this method works well in the interim until I need a more robost solution of backing up mongo databases