There are a few ways you can make a backup with Mongo. The most simple is to take a copy of the database files and put them somewhere safe. This is made easier if you’ve put each database into its own directory. However, this is a pretty naive method as you will have to stop the server and restart it. You also will be taking a full backup. That said, this method does have its uses in a scenario where you are messing around with a secondary but that’s beyond the scope of this short post.
Its possible to extend this a little by using mongodump against the file system. The tool will lock the files so the database cannot be in use at the time but it at least provides a binary export of the data.
For a developer scenario, its more likely that we’ll be using mongodump, mongorestore, mongoimport and mongoexport against our running database. We may need some data from a co-worker or from the live system or we may be the provider of some data to another member of our team.
Mongodump has a number of options that include which database to dump, which collection (if you don’t want the whole database), credentials and so on. Here’s a complete example:
mongodump --host DBHOST --db DATABASE --username USER --password PASSWORD --out c:\mongo-backups\DBHOST-backups\DATABASE
Here we are connecting to a server on the default port with user credentials. We are backing up a named database and saving its data to a specified location. It’s worth remembering that this backup procedure will wipe out any files in the target location. So were you to script this, you’d do well to include some datetime information in the target folder name.
Mongorestore is the opposite of mongodump and takes a source location and puts it into a target database. If that target database doesn’t exist it will create it. However, if you’re creating a new one then it will be a good idea to remember to add any required credentials before considering it ready for use.
mongorestore --host DBHOST --db DATABASE --username USER --password PASSWORD c:\mongo-backups\DBHOST-backups\DATABASE
Following the dump example, we are connecting to a server on the default port with user credentials and restoring a named database from a specified location. If we wanted, we could restore an individual collection though we’d have to specify a filename rather than a directory.
Importing and Exporting Data
Its worth knowing how to import and export data too. This is different to taking a backup in that we’re dealing with a database and collection which already exists and we want to share data with someone. They may have a different setup than us but we want to share a collection. This collection could be post code data, a latitude and longitude database or something else equally useful.
When importing data, mongo supports datafiles where each line is a Json document or the entire document is a Json array of documents. Its worth noting as if you peek into a file you may see each line is valid Json but it doesn’t ‘look’ like valid Json. Mongo will still support it.
+ Import data from a JSON file mongoimport --db test --collection PostCodes --type json --file c:\postcodes.json + Import data from a JSON file where the data is a JSON array mongoimport --db test --collection PostCodes --type json --file c:\postcodes.json --jsonArray + Export data to a JSON file mongoexport --db test --collection PostCodes > c:\postcodes.json
The export command also supports queries but that’s an exercise for the reader! For my own learning, I’ve used the site GenerateData.com to create Json files and import them. I recommend checking it out.