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:
- Stop balancer (
sh.stopBalancer()).
- Lock each shard (optional, for consistency).
- Take filesystem snapshots.
- 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:
- Run
mongodump on each shard and config server.
- 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:
- Stop the cluster.
- Restore snapshots to each shard and config server.
- Restart the cluster and rebalance data.
b. Restoring from mongodump
- Steps:
- Use
mongorestore to import data into each shard.
- 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.