MongoDB Backup Methods

When deploying MongoDB in production, you should have a strategy forcapturing and restoring backups in the case of data lossevents.

Back Up with Atlas

MongoDB Atlas, the official MongoDB cloud service, provides 2 fully-managedmethods for backups:

  • Continuous Backups, which takeincremental backups of data in your cluster, ensuring your backups aretypically just a few seconds behind the operational system. Atlas continuousbackups allow you to restore from stored snapshots or from a selected pointin time within the last 24 hours. You can also query a continuous backupsnapshot.
  • Cloud Provider Snapshots, whichprovide localized backup storage using the native snapshot functionality ofthe cluster’s cloud service provider.

Back Up with MongoDB Cloud Manager or Ops Manager

MongoDB Cloud Manager is a hosted back up, monitoring, and automation service forMongoDB. MongoDB Cloud Manager supports backing up and restoring MongoDBreplica sets and sharded clusters from a graphical user interface.

MongoDB Cloud Manager

The MongoDB Cloud Manager supports the backing up and restoring of MongoDBdeployments.

MongoDB Cloud Manager continually backs up MongoDB replica setsand sharded clusters by reading theoplog data from your MongoDB deployment. MongoDB Cloud Manager creates snapshotsof your data at set intervals, and can also offer point-in-time recoveryof MongoDB replica sets and sharded clusters.

Tip

Sharded cluster snapshots are difficult to achieve with other MongoDBbackup methods.

To get started with MongoDB Cloud Manager Backup, sign up for MongoDB Cloud Manager. Fordocumentation on MongoDB Cloud Manager, see the MongoDB Cloud Manager documentation.

Ops Manager

With Ops Manager, MongoDB subscribers can install and run the same coresoftware that powers MongoDB Cloud Manager on their own infrastructure.Ops Manager is an on-premise solution that has similar functionality toMongoDB Cloud Manager and is available with Enterprise Advanced subscriptions.

For more information about Ops Manager, see the MongoDB EnterpriseAdvanced pageand the Ops Manager Manual.

Back Up by Copying Underlying Data Files

Back Up with Filesystem Snapshots

You can create a backup of a MongoDB deployment by making a copy of MongoDB’sunderlying data files.

If the volume where MongoDB stores its data files supports point-in-timesnapshots, you can use these snapshots to create backups of a MongoDBsystem at an exact moment in time.File system snapshots are an operating system volume manager feature,and are not specific to MongoDB. With file system snapshots, the operatingsystem takes a snapshot of the volume to use as a baseline for data backup.The mechanics of snapshots depend onthe underlying storage system. For example, on Linux, the Logical VolumeManager (LVM) can create snapshots. Similarly, Amazon’s EBS storagesystem for EC2 supports snapshots.

To get a correct snapshot of a running mongod process, youmust have journaling enabled and the journal must reside on the samelogical volume as the other MongoDB data files. Without journalingenabled, there is no guarantee that the snapshot will be consistent orvalid.

To get a consistent snapshot of a sharded cluster, you mustdisable the balancer and capture a snapshot from every shard as well as aconfig server at approximately the same moment in time.

For more information, see theBack Up and Restore with Filesystem Snapshots andBack Up a Sharded Cluster with File System Snapshots forcomplete instructions on using LVM to create snapshots. Also seeBack up and Restore Processes for MongoDB on Amazon EC2.

Back Up with cp or rsync

If your storage system does not support snapshots, you can copy thefiles directly using cp, rsync, or a similar tool. Sincecopying multiple files is not an atomic operation, you must stop allwrites to the mongod before copying the files. Otherwise, you willcopy the files in an invalid state.

Backups produced by copying the underlying data do not support pointin time recovery for replica sets and are difficult to manage forlarger sharded clusters. Additionally, these backups are largerbecause they include the indexes and duplicate underlying storagepadding and fragmentation. mongodump, by contrast, createssmaller backups.

Back Up with mongodump

mongodump reads data from a MongoDB database andcreates high fidelity BSON files which the mongorestoretool can use to populate a MongoDB database.mongodump and mongorestore are simple andefficient tools for backing up and restoring smallMongoDB deployments, but are not ideal for capturing backups of largersystems.

mongodump and mongorestore operate against arunning mongod process, and can manipulate the underlyingdata files directly. By default, mongodump does notcapture the contents of the local database.

mongodump only captures the documents in the database. Theresulting backup is space efficient, but mongorestore ormongod must rebuild the indexes after restoring data.

When connected to a MongoDB instance, mongodump canadversely affect mongod performance. If your data is largerthan system memory, the queries will push the working set out ofmemory, causing page faults.

Applications can continue to modify data while mongodumpcaptures the output. For replica sets, mongodump providesthe —oplog option to include in itsoutput oplog entries that occur during the mongodumpoperation. This allows the corresponding mongorestoreoperation to replay the captured oplog. To restore a backup createdwith —oplog, use mongorestorewith the —oplogReplay option.

However, for replica sets, consider MongoDB Cloud Manager orOps Manager.

Note

mongodump and mongorestorecannot be part of a backup strategy for 4.2+ sharded clustersthat have sharded transactions in progress as these tools cannotguarantee a atomicity guarantees of data across the shards.

For 4.2+ sharded clusters with in-progress sharded transactions, forcoordinated backup and restore processes that maintain the atomicityguarantees of transactions across shards, see:

See Back Up and Restore with MongoDB Toolsand Back Up a Sharded Cluster with Database Dumpsfor more information.