Technology Encyclopedia Home >How does MongoDB complete the backup and restore of sharded cluster instances?

How does MongoDB complete the backup and restore of sharded cluster instances?

MongoDB provides several methods to back up and restore sharded cluster instances, ensuring data consistency and availability. Here’s how it works:

1. Backup Methods

a. Filesystem Snapshots (Recommended for Sharded Clusters)

  • How it works: Takes a snapshot of the data directories on each shard, config server, and mongos (if needed). Requires a filesystem that supports snapshots (e.g., LVM, EBS).
  • Steps:
    1. Stop balancer (sh.stopBalancer()).
    2. Lock each shard (optional, for consistency).
    3. Take filesystem snapshots.
    4. Unlock shards and resume balancer.
  • Example: On Tencent Cloud, you can use Cloud Block Storage (CBS) snapshots to capture the state of each shard’s data volume.

b. mongodump (For Smaller Clusters or Partial Backups)

  • How it works: Dumps data from each shard, config server, and mongos into BSON files. Less efficient for large clusters.
  • Steps:
    1. Run mongodump on each shard and config server.
    2. Use --oplog for point-in-time consistency.
  • Example: mongodump --host <shard-host> --out /backup/path --oplog

c. Cloud-Managed Backups (Tencent Cloud-specific)

  • How it works: Tencent Cloud’s TencentDB for MongoDB offers automated backups for sharded clusters, including full and incremental backups stored in COS (Cloud Object Storage).

2. Restore Methods

a. Restoring from Filesystem Snapshots

  • Steps:
    1. Stop the cluster.
    2. Restore snapshots to each shard and config server.
    3. Restart the cluster and rebalance data.

b. Restoring from mongodump

  • Steps:
    1. Use mongorestore to import data into each shard.
    2. Ensure the balancer is off during restoration.
  • Example: mongorestore --host <shard-host> --dir /backup/path --oplogReplay

c. Tencent Cloud’s Automated Restore

  • How it works: TencentDB for MongoDB allows restoring a sharded cluster from a backup point directly via the console, simplifying the process.

Best Practices

  • Consistency: Use --oplog with mongodump or stop the balancer for filesystem snapshots.
  • Testing: Verify backups by restoring to a staging environment.
  • Automation: Use Tencent Cloud’s scheduled backups for sharded clusters.

For Tencent Cloud users, TencentDB for MongoDB provides a managed solution with automated backups and one-click restore, reducing operational overhead.