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.
01💾 1️⃣ Why We Use Backup
Example01
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.
02⚙️ 2️⃣ MongoDB Backup Database Concept
Example02
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).
03💻 3️⃣ mongodump Command Line Examples
Example03
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.
04💡 4️⃣ MongoDB Restore Database Concept
Example04
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.
05⚙️ 5️⃣ mongorestore Command Line Examples
Example05
MongoDB Shell
NoSQL
Database
1// Example 1: Restore Specific Collection
2C:\Users\admin> mongorestore -d school -c students C:\backup\school\students.bson