iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

Backups

MongoDB backups in production - logical dumps for portability, snapshots for speed, oplog tailing for point-in-time recovery.

Backup strategies

EXAMPLE
// 1. mongodump - logical, portable
// Good for: small datasets, migrating across versions, exporting a subset
mongodump \
  --uri='mongodb://app:$PASS@host:27017/myapp' \
  --gzip \
  --out=/backup/$(date +%Y%m%d)

// Restore
mongorestore --uri='mongodb://...' --gzip /backup/20260620

// Limits: dumps lock collections briefly; slow for big DBs; not PITR.

// 2. Filesystem snapshots - fast
// Good for: large self-hosted clusters, EBS/Persistent Disk volumes
// Steps:
//   1) db.fsyncLock() to flush + lock writes
//   2) Trigger volume snapshot (AWS, GCP, Azure, ZFS)
//   3) db.fsyncUnlock()
// Or use cloud-native consistent snapshots that handle this for you.

// 3. Atlas continuous backups
// On dedicated clusters: PITR + scheduled snapshots
// Restore to a new cluster from any moment in the retention window

// 4. Oplog tailing for PITR (self-managed)
// Snapshot + ongoing oplog capture = point-in-time recovery
// Tools: Percona Backup for MongoDB (PBM), Ops Manager

// 5. Encryption + offsite
// Always encrypt backups at rest (KMS-backed bucket)
// Keep at least one copy in a different region from the primary

// 6. Backup verification
// A backup you have not restored is hope, not safety
// Run a restore drill every quarter:
//   - Spin up a sandbox cluster
//   - Restore the latest snapshot
//   - Run sanity queries against a known-good dataset
//   - Document time-to-restore - this is your RTO

// 7. Retention + compliance
// Define retention to match RPO and any legal hold requirements
// Common: 7 daily, 4 weekly, 12 monthly, 7 yearly

// 8. Schedule + monitoring
// Cron or a managed scheduler
// Alert on missed runs AND on backup size delta > threshold
// (sudden size shrinkage often means a partial backup)

Why it matters

Backups are not optional. Choose mongodump for portability, snapshots for speed, and continuous oplog tailing for true PITR. Test restores quarterly - your RTO is only as good as your last drill.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
// Atlas — point-in-time backups.
// Self-hosted — mongodump / mongorestore.
Try it Yourself »

Discussion

Loading…