Skip to main content
MongoDB
CHAPTER 21 Beginner

MongoDB Backup & Restore | mongodump and mongorestore

Updated: May 16, 2026
15 min read

# 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 the mongosh 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:

bash
12
# Backup the entire 'ecommerce_db' to a specific folder
mongodump --db ecommerce_db --out /backups/daily_backup/

*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 use mongodump on your laptop to pull a backup directly off a remote server! You just provide the Connection String URI.
bash
12
# Securely download a backup from an Atlas Cloud cluster!
mongodump --uri "mongodb+srv://admin:password@cluster0.net/ecommerce_db" --out ./local_backup/

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.

bash
12
# Restore the ecommerce_db from the backup folder
mongorestore --db ecommerce_db /backups/daily_backup/ecommerce_db/

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.
bash
12
# DANGER: This drops the live collections BEFORE restoring the clean backup!
mongorestore --drop --db ecommerce_db /backups/daily_backup/ecommerce_db/

8. Mini Project: Automating Backups with Cron

A backup is useless if you forget to run it. In Linux environments, Database Administrators use cron to schedule automated bash scripts.
bash
12345678910
#!/bin/bash
# A simple automated backup script (backup.sh)
DATE=$(date +"%Y-%m-%d")
BACKUP_DIR="/archive/mongo_backups/$DATE"

# Run the dump
mongodump --db production_db --out $BACKUP_DIR

# Keep only the last 7 days of backups to save disk space
find /archive/mongo_backups/ -type d -mtime +7 -exec rm -rf {} +

*(You configure the Linux Cron daemon to execute this script every night at 3:00 AM).*

9. Common Mistakes

  • Never Testing the Restore: Generating mongodump files every night is useless if the BSON files are corrupted. A backup does not officially exist until you have successfully executed mongorestore on 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. 1. What file format does mongodump use to save the actual document data?
  1. 2. What flag must be appended to mongorestore to forcefully delete existing collections before applying the backup data?

12. MongoDB Challenges

Write the terminal command to use mongodump to backup ONLY the invoices collection from the accounting database into the current directory. *(Hint: use the --collection flag).*
bash
1
mongodump --db accounting --collection invoices --out ./

13. MCQ Quiz with Answers

Question 1

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?

Question 2

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 over mongodump?
  • 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 mastering mongodump 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.

17. Next Chapter Recommendation

Our data is backed up against hardware failures, but what about malicious human failures? If a hacker accesses the database, can they delete everything? In Chapter 22: MongoDB Security Best Practices, we will master Roles, Authentication, and Role-Based Access Control (RBAC).

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·