Nearby lessons

22 of 22

🗄️ MongoDB: Backup and Restore Tutorial

MongoDB provides powerful tools for backing up and restoring databases using the command-line utilities `mongodump` and `mongorestore`. These tools are essential for data safety, migration, and disaster recovery.

💾 1️⃣ Why We Use Backup

MongoDB Shell
NoSQL
Database
1// Key reasons for taking MongoDB backups
2🛡️ Data Protection → Prevents loss from deletion, corruption, or hardware failure.
3🌪️ Disaster Recovery → Quickly restore data after system crashes or cyberattacks.
4🕒 Version Control → Retrieve historical data (useful for debugging/auditing).
5🚚 Migration → Safely move data between environments.
6📜 Compliance → Meet legal and data retention requirements.
7🧪 Testing → Use real data for development without harming production.
Query Result
JSON Output
✅ Regular backups ensure business continuity and data safety.

⚙️ 2️⃣ MongoDB Backup Database Concept

MongoDB Shell
NoSQL
Database
1// Tool Used: mongodump
2// This utility creates BSON-format backups of databases and collections.
3
4🧰 Tool: mongodump
5
6// Common Scenarios
71️⃣ Backup All Databases:
8mongodump -o c:\backup
9
102️⃣ Backup Specific Database:
11mongodump -d school -o c:\backup
12
133️⃣ Backup Specific Collection:
14mongodump -d school -c students -o c:\backup
Query Result
JSON Output
✅ Backup files created successfully in the target directory (e.g., C:\backup).

💻 3️⃣ mongodump Command Line Examples

MongoDB Shell
NoSQL
Database
1// Example 1: Full Server Backup
2C:\Users\admin> mongodump -o c:\backup
3
4// Example 2: Specific Database Backup
5C:\Users\admin> mongodump -d school -o c:\backup
6
7// Example 3: Specific Collection Backup
8C:\Users\admin> mongodump -d school -c students -o c:\backup
Query Result
JSON Output
✅ Example 1: All databases dumped successfully.
✅ Example 2: 'school' database collections exported.
✅ Example 3: Only 'students' collection backed up.

💡 4️⃣ MongoDB Restore Database Concept

MongoDB Shell
NoSQL
Database
1// Tool Used: mongorestore
2// Restores databases and collections from BSON backups created with mongodump.
3
4🧰 Tool: mongorestore
5
6// Common Restore Scenarios
71️⃣ Restore All Databases:
8mongorestore --dir c:\backup
9
102️⃣ Restore Specific Database:
11mongorestore -d school c:\backup\school
12
133️⃣ Restore Specific Collection:
14mongorestore -d school -c students c:\backup\school\students.bson
Query Result
JSON Output
✅ Data restored successfully from BSON files.

⚙️ 5️⃣ mongorestore Command Line Examples

MongoDB Shell
NoSQL
Database
1// Example 1: Restore Specific Collection
2C:\Users\admin> mongorestore -d school -c students C:\backup\school\students.bson
3
4// Example 2: Restore into a Different Database
5C:\Users\admin> mongorestore -d office C:\backup\school
6
7// Example 3: Full Server Restore
8C:\Users\admin> mongorestore --dir c:\backup
9
10// Example 4: Full Restore with Clean Replace (--drop)
11C:\Users\admin> mongorestore --drop --dir c:\backup
Query Result
JSON Output
✅ Example 1: 8 documents restored into 'school.students'.
✅ Example 2: 'school' data imported into 'office' database.
✅ Example 3: Multiple databases restored (school, test, office, etc.).
✅ Example 4: Old data dropped — clean restore completed.

🧭 6️⃣ Summary of Commands

MongoDB Shell
NoSQL
Database
1
2| Action | Command |
3| ----------------------------- | ------------------------------------------------------------------- |
4| 🧾 Backup All Databases | mongodump -o c:\backup |
5| 📦 Backup One Database | mongodump -d school -o c:\backup |
6| 🧮 Backup One Collection | mongodump -d school -c students -o c:\backup |
7| 🔄 Restore All Databases | mongorestore --dir c:\backup |
8| 🏫 Restore One Database | mongorestore -d school c:\backup\school |
9| 📚 Restore One Collection | mongorestore -d school -c students c:\backup\school\students.bson |
10| 🧹 Clean Restore (--drop) | mongorestore --drop --dir c:\backup |
Query Result
JSON Output
✅ Summary table provides quick reference for backup and restore commands.
Keep Going!Course Complete!