How To

How to Backup MongoDB Correctly

Have a strategy to capture and restore backups to combat data loss risk. Even when the likelihood of data loss is low, it is always best practice to guard against the worst possible outcome and back up your data.

There are several ways to back up MongoDB and protect your data. This is something you can do manually. Alternatively, it is a process that can be automated. Here is how to back up MongoDB.

MongoDB Backup Guide

Use MongoDB Atlas

One of the better options for backing up MongoDB is MongoDB Atlas. The advantage here is that backups are fully managed. There are two ways to do this. The first is Legacy Backup, which takes incremental data backups.

Though this is available, it’s not the recommended way to complete a backup as you do not receive all of your data fully backed up. The best way to do it through Atlas is with Cloud Backups.

Use Cloud Backups via MongoDB Atlas

Cloud Backups utilize MongoDB’s native snapshot functionality to offer several backup options. Users can choose on-demand snapshots, which trigger an immediate snapshot of your deployment at a defined time.

They can also use continuous cloud backups—strongly recommended—which set a schedule for recurring backups for your deployment. Continuous cloud backups are automated, easy to set up, and the simplest way to backup MongoDB.

Use MongoDB Cloud Manager

MongoDB Cloud Manager is a MongoDB service that backups, monitors, and automates certain functions. This includes backing up and restoring replica sets and sharded clusters from a GUI. For a user, Cloud Manager will perform continual backups of those replica sets and sharded groups by reading the oplog data and, like Atlas, will create snapshots of data at set intervals.

This way of doing backups also offers point-in-time recovery of data. This is the easiest way to achieve shared cluster snapshots compared to other MongoDB backup methods.

Use MongoDB Ops Manager

Another option is to use Ops Manager. For MongoDB Enterprise Advanced subscribers, Ops Manager is another way to perform Cloud Manager tasks. They can install and run the same software used in Cloud Manager in their infrastructure.

Use Copies of the Underlying Data Files

You can back up MongoDB by copying the underlying data files. You can go this route if the volume where your data files are stored supports point-in-time snapshots. File system snapshots are an OS volume manager feature unrelated to MongoDB. All this is where an operating system takes a picture of the volume to use as a standard for creating data backup.

To do this, enable journaling, as this is the only way to ensure the snapshot is consistent and valid. Turn off the balancer if you want a consistent snapshot of a sharded cluster. From there, you can capture a snapshot of every shard and config server roughly simultaneously.

Back Up with cp or rsync

Some storage systems do not support snapshots. An alternative is to copy files directly with cp, rsync, or something similar. As multi-file copying is an atomic operation, remember to stop all writes to the mongod before beginning. If you do not, your copied files will be invalid.

This method’s downside is that it does not support point-in-time recovery for replica sets and is difficult to manage for larger sharded clusters. You also often have larger-than-preferred backups, including indexes, duplicate storage padding, and fragmentation.

Use mongodump for Your MongoDB Backup

Mongodump creates high-fidelity BSON files that the Mongorestore tool can use to fill a MongoDB database. This is a simple, efficient way to back up and restore small MongoDB deployments; however, it could be better for larger systems.

Mongodump and Mongorestore work against a running Mongod process and, by default, do not capture the local database contents. To this point, mongodump only captures database documents.

Disadvantages of Mongodump

A quick note about mongodump is that it can adversely affect mongod performance. When your data is larger than system memory, queries push the working set out of memory, causing page faults. Fortunately, you can continue to modify data while mongodump captures the output.

You may also like to read 7 Ways To Protect Your Business Data

Ensuring Data Consistency During Backups

When backing up MongoDB, keeping data consistent is vital. This is especially true for large databases or sharded clusters across multiple servers. If backups are inconsistent, you could face data loss or corruption, making the backup worthless. To keep data consistent, use MongoDB’s built-in tools and follow best practices.

One key method is to enable journaling, which logs all write actions. This ensures that even if a backup occurs while the database is active, the journal can replay actions and restore the database to a consistent state. For sharded clusters, turning off the balancer during backups stops data from moving between shards, which prevents inconsistencies.

Scheduling Regular Backups

Regular backups are crucial for keeping your data up-to-date and easy to restore if something goes wrong. MongoDB’s tools, like Atlas and Cloud Manager, offer automated backup scheduling. This means backups happen at set times without manual input, lowering the risk of human error.

For MongoDB Atlas users, continuous cloud backups let you plan for repeated backups. This makes the backup process easier and ensures you always have a recent copy available. Similarly, MongoDB Cloud Manager and Ops Manager give you options to schedule and monitor backups, keeping your backup plan effective and reliable.

Which One Should I Use?

The method you use to back up MongoDB depends greatly on how your system is deployed. The Mongostore and Mongo dump utilities can create small backups of small deployments and work well with BSON data dumps.

However, Cloud Backups from MongoDB Atlas can provide file system snapshots or block-level disk snapshots, which are far more resilient and non-disruptive.

Note: You can check out this article for more details if required.

Boris Dzhingarov

Boris Dzhingarov is passionate about blogging and specialist in writing about tech, business, marketing, and more. He writes for several sites online.

Related Articles

Back to top button