Restoring a MongoDB instance

how-to
Dec 21, 20112 mins

At App47, our production MongoDB instance is managed by MongoHQ — that is, we use their PaaS to host our data and leave the details of running and maintaining MongoDB instances up to them. It’s a handy service and worth the money at this point in our company’s evolution (eventually we might need more control over our instances and thus might look to take on some of these responsibilities, etc).

Nevertheless, while production data is hosted in the cloud, in many cases, local testing is desired and accordingly, from time to time, we replicate the state of production into local MonogDB instances. This is easily done with MongoDB’s mongorestore command. This utility can take the output of a mongodump (which, MongoHQ uses as part of their backup offering); thus, we download from S3 a backup of a particular MonogDB instance and restore that data locally as follows:

First, unzip the contents of a zipped mongodump:

$>tar xvf instance_name_628640_20111221150030.gtar

where instance_name is the name of your database. This will consequently create a directory (named after your instance, i.e. foo) containing a .bson file for each of your instances collections.

Next, to import that data to a locally running MongoDB instance, run from the root directory where you unzipped the backup from MongoHQ:

$>mongorestore -vvv instance_name

Where instance_name is the directory containing all the datastore’s .bson files. The -vvv enables verbosity, which is handy to see what’s going on, so standby for a lot of text to be printed out. This will create (or append to) an existing datastore instance (i.e. instance_name!) so if you already have an existing instance you might want to add --drop option to the mongorestore.

andrew_glover

When Andrew Glover isn't listening to “Funkytown” or “Le Freak” he enjoys speaking on the No Fluff Just Stuff Tour. He also writes articles for multiple online publications including IBM's developerWorks and O'Reilly’s ONJava and ONLamp portals. Andrew is also the co-author of Java Testing Patterns, which was published by Wiley in September 2004; Addison-Wesley’s Continuous Integration; and Manning’s Groovy in Action.

More from this author