This guide covers essential MongoDB backup and recovery concepts commonly asked in technical interviews. Each question includes detailed answers and practical examples.
MongoDB supports several backup methods:
mongodump implementation for backups:
# Full database backup
mongodump --uri="mongodb://localhost:27017" --out=/backup/$(date +%Y%m%d)
# Specific database backup
mongodump --uri="mongodb://localhost:27017/myapp" --out=/backup/myapp
# Specific collection backup
mongodump --uri="mongodb://localhost:27017/myapp" --collection=users --out=/backup/users
# Compressed backup
mongodump --uri="mongodb://localhost:27017" --gzip --out=/backup/compressed
# Backup with authentication
mongodump --uri="mongodb://user:password@localhost:27017" --out=/backup/auth
# Backup with query filter
mongodump --uri="mongodb://localhost:27017/myapp" \
--query='{"createdAt": {"$gte": ISODate("2024-01-01")}}' \
--out=/backup/filtered
# Backup with oplog
mongodump --uri="mongodb://localhost:27017" \
--oplog \
--out=/backup/oplog
Recovery implementation procedures:
# Full database restore
mongorestore --uri="mongodb://localhost:27017" /backup/20250101
# Specific database restore
mongorestore --uri="mongodb://localhost:27017" --db=myapp /backup/myapp
# Compressed backup restore
mongorestore --uri="mongodb://localhost:27017" --gzip /backup/compressed
# Restore with oplog replay
mongorestore --uri="mongodb://localhost:27017" \
--oplogReplay \
/backup/oplog
# Restore to specific point in time
mongorestore --uri="mongodb://localhost:27017" \
--oplogReplay \
--oplogLimit=1634567890 \
/backup/oplog
# Restore with validation
mongorestore --uri="mongodb://localhost:27017" \
--validate \
/backup/20250101
Automated backup strategy implementation:
#!/bin/bash
# Backup configuration
BACKUP_DIR="/backup/mongodb"
DATE=$(date +%Y%m%d)
RETENTION_DAYS=7
# Create backup
mongodump --uri="mongodb://localhost:27017" \
--gzip \
--out="$BACKUP_DIR/$DATE"
# Compress backup
tar -czf "$BACKUP_DIR/$DATE.tar.gz" "$BACKUP_DIR/$DATE"
# Remove old backups
find "$BACKUP_DIR" -type f -name "*.tar.gz" -mtime +$RETENTION_DAYS -delete
# Upload to cloud storage
aws s3 cp "$BACKUP_DIR/$DATE.tar.gz" "s3://backup-bucket/mongodb/"
# Verify backup integrity
mongorestore --uri="mongodb://localhost:27017" \
--gzip \
--dryRun \
"$BACKUP_DIR/$DATE"
# Check backup size
du -sh "$BACKUP_DIR/$DATE"
# Monitor backup status
db.currentOp().inprog.forEach(function(op) {
if (op.command && op.command.backup) {
printjson(op);
}
})
Follow these backup and recovery best practices:
# Backup schedule configuration
0 0 * * * /scripts/mongodb-backup.sh # Daily backup
0 0 * * 0 /scripts/mongodb-full-backup.sh # Weekly full backup
# Backup retention policy
RETENTION_DAYS=30
BACKUP_TYPES=("daily" "weekly" "monthly")
# Backup verification
mongorestore --uri="mongodb://localhost:27017" \
--gzip \
--dryRun \
"$BACKUP_DIR/$DATE"
# Test recovery procedure
mongorestore --uri="mongodb://localhost:27017" \
--gzip \
--db=test_restore \
"$BACKUP_DIR/$DATE"
# Verify data integrity
db.test_restore.users.find().count()
db.test_restore.orders.find().count()
# Clean up test restore
db.dropDatabase()
Continue your MongoDB interview preparation with: