MongoDB Backup & Restore | mongodump and mongorestore
# CHAPTER 21
Backup and Restore in MongoDB
1. Introduction
If you are using MongoDB Atlas (as covered in Chapter 20), your data is automatically backed up. However, if you are hosting MongoDB on your own private cloud servers (AWS EC2, DigitalOcean), you are entirely responsible for Disaster Recovery. A server crash without a backup means the death of the company. In this chapter, we will step out of the Mongo Shell and into the Operating System Terminal to master the Database Administrator tools:mongodump and mongorestore.
2. Learning Objectives
By the end of this chapter, you will be able to:- Understand the difference between logical and physical backups.
-
Export an entire database to BSON files using
mongodump.
-
Reconstruct a database from a backup using
mongorestore.
- Target specific collections for backup.
- Understand the 3-2-1 Backup Strategy.
3. Database Tools Installation
Unlike themongosh terminal, the backup tools are standalone programs. If they are not recognized in your terminal, you must download the MongoDB Database Tools package from the official MongoDB website and add them to your system's PATH.
4. Creating a Backup (mongodump)
The mongodump utility executes a "Logical Backup." It reads all the documents in your database and writes them to the hard drive as highly compressed raw BSON files.
Open your standard OS terminal (Bash, PowerShell) and run:
*Output: MongoDB will create a folder at /backups/dailybackup/ecommercedb/. Inside, you will see pairs of files for every collection: e.g., users.bson (the data) and users.metadata.json (the indexes and validation rules).*
5. Backing Up Remote/Cloud Databases
You can usemongodump on your laptop to pull a backup directly off a remote server! You just provide the Connection String URI.
6. Disaster Recovery (mongorestore)
Your server caught fire. You bought a new server, installed a fresh copy of MongoDB, and copied your /backups/ folder onto it. How do you revive the data?
You use the mongorestore utility. It reads the raw BSON files and forcefully inserts the data and rebuilds the indexes back into the MongoDB engine.
7. Overwriting Live Data (--drop)
If a hacker maliciously alters your live database, restoring from a backup *without* dropping the corrupted data first will result in a messy merge of good and bad data. You must use the --drop flag.
8. Mini Project: Automating Backups with Cron
A backup is useless if you forget to run it. In Linux environments, Database Administrators usecron to schedule automated bash scripts.
*(You configure the Linux Cron daemon to execute this script every night at 3:00 AM).*
9. Common Mistakes
-
Never Testing the Restore: Generating
mongodumpfiles every night is useless if the BSON files are corrupted. A backup does not officially exist until you have successfully executedmongorestoreon a test server and verified the application still works.
- Storing Backups on the Same Server: If you save your backup folder to the exact same hard drive that the live MongoDB server lives on, and that hard drive dies, you lose both the database and the backup. Backups MUST be uploaded off-site (e.g., AWS S3).
10. Best Practices
- The 3-2-1 Backup Strategy:
- Keep 3 copies of your data (1 Primary, 2 Backups).
- Store them on 2 different types of media (e.g., Local Drive + Cloud Storage).
- Keep 1 copy completely off-site (e.g., in a different physical city or AWS region).
11. Exercises
-
1.
What file format does
mongodumpuse to save the actual document data?
-
2.
What flag must be appended to
mongorestoreto forcefully delete existing collections before applying the backup data?
12. MongoDB Challenges
Write the terminal command to usemongodump to backup ONLY the invoices collection from the accounting database into the current directory. *(Hint: use the --collection flag).*
13. MCQ Quiz with Answers
When examining the output directory of a successful mongodump, you will find two files for every collection (e.g., users.bson and users.metadata.json). What is the purpose of the metadata JSON file?
Why is executing mongorestore with the --drop flag considered a highly dangerous, yet necessary, disaster recovery operation?
14. Interview Questions
-
Q: Explain the difference between a Logical Backup (
mongodump) and a Physical Backup (copying the underlying database files). When would an enterprise choose a Physical Backup strategy overmongodump?
- Q: Explain the mechanics of the 3-2-1 Backup Strategy and why it is the industry standard for database disaster recovery.
15. FAQs
Q: Can I backup data into a single, compressed file instead of a giant folder? A: Yes! You can use the--archive flag: mongodump --db mydb --archive=mydb.archive. This streams the entire database into a single, easily transportable file!
16. Summary
Data loss is an existential threat to any organization. By masteringmongodump to generate portable BSON backups, and wielding mongorestore to rebuild architectures from scratch, you have acquired the fundamental administrative skills required to keep a company alive through catastrophic hardware failures.