Published on 2016-04-18 by John Collins. Socials: YouTube - X - Spotify - Amazon Music - Apple Podcast |
The following tutorial describes how you can backup a single MongoDB database server running on a local Linux machine using the mongodump command line tool, and then subsequently restore that dump to the server using mongorestore.
Note: these steps are not suitable for shards or replication sets, as you will not be able to guarantee the integrity of the backup generated, especially for write-intensive applications. For larger multi-node MongoDB deployments, you should look into using something more robust like MongoDB Cloud Manager.
Backing up a database using mongodump is easy: first I create a directory to store my dumps in, then I dump the database to that location using the --out param:
[john@alphadevx ~]$ mkdir mongobackup [john@alphadevx ~]$ mongodump --host 127.0.0.1 --port 27017 --out mongobackup/ 2016-04-18T16:53:26.754+0100 writing test.restaurants to 2016-04-18T16:53:26.755+0100 done dumping test.restaurants (1 document)
Note that host and port are optional if you are running on localhost and the default MongoDB port above, I just included them for illustration.
In my test MongoDB database, I only have one collection called restaurants with a single record. The mongodump tool created a .bson (binary JSON) file to store the records, and a .metadata.json file to store meta data about that collection:
[john@alphadevx ~]$ tree mongobackup/ mongobackup/ └── test ├── restaurants.bson └── restaurants.metadata.json 1 directory, 2 files
Here is the content of the .metadata.json, as you can see it contains meta data such as index and namespace descriptions:
[john@alphadevx ~]$ cat mongobackup/test/restaurants.metadata.json {"options":{},"indexes":[{"v":1,"key":{"_id":1},"name":"_id_","ns":"test.restaurants"}]}
Obviously in production, you would set up a cron tab entry or some other automated scheduled task to run mongodump on a regular basis to keep your backup up-to-date.
No backup solution is valid until you do a test restore from the backup, so let us test it now using the mongorestore tool.
Firstly, let is drop our restaurants collection by logging into MongoDB via the mongo command line client and issuing the following command:
> db.restaurants.drop() true
Now to test the restore, from the regular Linux prompt run the following:
[john@alphadevx ~]$ mongorestore --port 27017 mongobackup/ 2016-04-18T17:03:59.636+0100 building a list of dbs and collections to restore from mongobackup dir 2016-04-18T17:03:59.637+0100 reading metadata for test.restaurants from mongobackup/test/restaurants.metadata.json 2016-04-18T17:03:59.672+0100 restoring test.restaurants from mongobackup/test/restaurants.bson 2016-04-18T17:03:59.674+0100 restoring indexes for collection test.restaurants from metadata 2016-04-18T17:03:59.674+0100 finished restoring test.restaurants (1 document) 2016-04-18T17:03:59.674+0100 done
Now when you log back in via mongo, you should see that the restaurants collection has been restored.