The best backup strategy for MongoDB is to implement high availability, using a replica set. This ensures that the data is as up to date as possible and ensures that it is always available. However, you should also consider the following if your data is extremely critical and cannot be replaced:
What if the data center fails? In this case, you can back up the data periodically and store it offsite, or you can add a replica somewhere offsite.
What if something happens to corrupt the application data that gets replicated? This is always a concern. In such a case, the only option is to have a backup from a previous point.
If you decide that you need to implement periodic backups of data, you should also consider the impact that backups will have on the system and decide on a strategy. For example:
Production impact: Backups are often intensive and need to be performed at a time when they will have a minimum impact on your environment.
Requirements: If you plan on implementing something like a block-level snapshot to back up the database, you need to ensure that the system infrastructure supports it.
Sharding: If you are sharding the data, all shards must be consistent; you cannot back up one without backing up all. Also, you must stop the writes to the cluster to generate the point-in-time backup.
Relevant data: You can reduce the impact that backups have on your system by only backing up data that is critical to your system. For example, if a database will never change, it only needs to be backed up once, or if data in a database can easily be regenerated but is very large, it may be worth accepting the cost of regeneration rather than doing frequent backups.
There are two main approaches to backing up MongoDB. The first is to perform a binary dump of the data, using the mongodump
command. The binary data can be stored offsite for later use. For example, you could use the following command to dump the database for a replica set named rset1
on host mg1.test.net
and on a stand-alone system named mg2.test.net
to a folder called /opt/backup/current
:
mongodump --host rset1/mg1.test.net:27018,mg2.test.net --out /opt/backup/current
The second method for backing up MongoDB databases is to use a file system snapshot. These snapshots are very quick to complete, but they are also large, and you need to have journaling enabled and the system able to support the block-level backups.
Note
If you are interested in implementing a snapshot method for backups, check out the guide at the following location: http://docs.mongodb.org/manual/tutorial/back-up-databases-with-filesystem-snapshots/.