MongoDB - Create a Backup

To create a backup in MongoDB, either copy the files directly, or use one of several backup/management tools.

There are several ways to backup a MongoDB database:

Copy the Data Files

You can copy the underlying data files that MongoDB uses to store data. These are located in the data directory.

The default location of the data directory is /data/db, however, if you use a different location you will need to use that instead.

You should copy the whole directory for a complete backup.

You can also use snapshots if the volume supports it. For example, on Linux, use LVM (Logical Volume Manager) to create a snapshot, then you can copy from that snapshot to your backup site/remote location.

Use mongodump

You can use mongodump to backup the data and mongorestore to restore it.

To quickly backup all contents of the running server, open a new Terminal/Command Prompt, change to a directory that you want the /dump folder to be created in, and type the following:

You will need provide the full path if the MongoDB bin directory is not in your PATH.

If you find that you can't run mongodump, be sure that you've either exited the mongo utility, or opened a new Terminal/Command Prompt window before running mongodump, as it is a separate utility.

Resulting message:

2016-07-12T15:44:34.467+0700	writing music.artists to 
2016-07-12T15:44:34.467+0700	writing music.musicians to 
2016-07-12T15:44:34.467+0700	writing music.catalog to 
2016-07-12T15:44:34.468+0700	done dumping music.artists (13 documents)
2016-07-12T15:44:34.469+0700	done dumping music.musicians (10 documents)
2016-07-12T15:44:34.469+0700	done dumping music.catalog (10 documents)
2016-07-12T15:44:34.470+0700	writing music.producers to 
2016-07-12T15:44:34.470+0700	writing music.jazz to 
2016-07-12T15:44:34.470+0700	done dumping music.producers (5 documents)
2016-07-12T15:44:34.470+0700	done dumping music.jazz (1 document)
2016-07-12T15:44:34.534+0700	writing test.restaurants to 
2016-07-12T15:44:34.705+0700	done dumping test.restaurants (25359 documents)

And here's what that looks like on my Mac's Finder:

Screenshot of backups

As you can see, it has created a folder called dump, then a folder for each database, then dumped all collections and their metadata into the respective database folder. I've expanded the music database folder to show the files within that directory.

Note that mongodump overwrites output files if they exist in the backup data folder, so be sure to move or rename any files you need to keep before running mongodump again.

Backup a Single Database

You can backup a single database by specifying the name of the database in the --db parameter:

Backup a Single Collection

You can backup a single collection by specifying the name of the collection in the --collection parameter:

Specify a Backup Location

Use the --out parameter to specify the directory that you'd like the backup to be written to:

More Options

mongodump has many more options for specifying how the data is backed up. You can always run mongodump --help to see which options are available.

Restoring a mongodump Backup

You can restore any mongodump backup by running mongorestore. which works in a similar way to mongodump.

See mongorestore --help for more info.

mongodump and mongorestore are mainly intended for smaller deployments, and for partial backup and restores based on a query, syncing from production to staging or development environments, or changing the storage engine of a standalone.

For larger systems, or sharded clusters, or replica sets, use a more robust backup system, such MongoDB Cloud Manager or Ops Manager.

MongoDB Cloud Manager

MongoDB Cloud Manager is a hosted platform for managing MongoDB.

You can use MongoDB Cloud Manager to continually back up MongoDB replica sets and sharded clusters by reading the oplog data from your MongoDB deployment.

MongoDB Cloud Manager works on a subscription basis. More info here.

Ops Manager

Ops Manager is like MongoDB Cloud Manager, except it is installed on your local environment (i.e. not in the cloud). So you can use it for monitoring, automating, and backup up your MongoDB deployment.

Ops Manager is available to MongoDB Subscribers.