MongoDB Backup & Recovery Interview Questions

Introduction

This guide covers essential MongoDB backup and recovery concepts commonly asked in technical interviews. Each question includes detailed answers and practical examples.

Medium

1. What are the different backup methods in MongoDB?

MongoDB supports several backup methods:

  • MongoDB Cloud Backup (Atlas)
  • MongoDB Ops Manager
  • mongodump
  • Filesystem Snapshots
  • Replica Set Backups
Backup Types:
  • Full Backups
  • Incremental Backups
  • Point-in-Time Backups
  • Continuous Backups
Hard

2. How do you implement mongodump for backups?

mongodump implementation for backups:

1. Basic Backup
# 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
2. Advanced Backup Options
# 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
Hard

3. How do you implement recovery procedures?

Recovery implementation procedures:

1. Using mongorestore
# 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
2. Point-in-Time Recovery
# 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
Hard

4. How do you implement automated backup strategies?

Automated backup strategy implementation:

1. Backup Script
#!/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/"
2. Monitoring and Verification
# 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);
    }
})
Hard

5. What are the backup and recovery best practices?

Follow these backup and recovery best practices:

1. Backup Strategy
# 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"
2. Recovery Testing
# 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()

Next Steps

Continue your MongoDB interview preparation with: