Database Manual / Self-Managed Deployments / Administration / Backup Methods / Restore Sharded Clusters

Back Up a Self-Managed Sharded Cluster with a Database Dump使用数据库转储备份自我管理的分片集群

Starting in MongoDB 7.1 (also available starting in 7.0.2, 6.0.11, and 5.0.22), you can back up data on sharded clusters using mongodump.从MongoDB 7.1开始(从7.0.2、6.0.11和5.0.22开始也可用),您可以使用mongodump备份分片集群上的数据。

About this Task关于此任务

mongodump is a utility that creates a binary export of database content. 是一个创建数据库内容二进制导出的实用程序。You can use the mongodump utility to take self-managed backups of a sharded cluster.您可以使用mongodump实用程序对分片集群进行自我管理备份。

To back up a sharded cluster with mongodump, you must stop the balancer, stop writes, and stop any schema transformation operations on the cluster. This helps reduce the likelihood of inconsistencies in the backup.要使用mongodump备份分片集群,您必须停止平衡器,停止写入,并停止集群上的任何模式转换操作。这有助于降低备份中不一致的可能性。

MongoDB provides backup and restore operations that can run with the balancer and running transactions through the following services:MongoDB提供备份和还原操作,可以与平衡器一起运行,并通过以下服务运行事务:

Stale Backups过时的备份

Backups provide a snapshot of the current state of the database. When you restore from a backup, the restored database doesn't include any changes made after the backup was taken, which can result in data loss.备份提供数据库当前状态的快照。从备份还原时,还原的数据库不包括备份后所做的任何更改,这可能会导致数据丢失。

Before you Begin开始之前

This task uses mongodump to back up a sharded cluster. Ensure that you have a cluster running that contains data in sharded collections.此任务使用mongodump备份分片集群。确保正在运行的群集包含分片集合中的数据。

Version Compatibility版本兼容性

This procedure requires a version of MongoDB that supports fsync locking from mongos.此过程需要一个支持mongos的fsync锁定的MongoDB版本。

Starting in MongoDB 7.1 (also available starting in 7.0.2, 6.0.11, and 5.0.22) the fsync and fsyncUnlock commands can run on mongos to lock and unlock a sharded cluster.从MongoDB 7.1开始(从7.0.2、6.0.11和5.0.22开始也可用),fsyncfsyncUnlock命令可以在mongos上运行,以锁定和解锁分片集群。

Admin Privileges管理员权限

To use this procedure, your MongoDB user must have the fsync authorization, which can be available through a custom role or using the built-in hostManager role.要使用此过程,MongoDB用户必须具有fsync授权,该授权可以通过自定义角色或使用内置的hostManager角色获得。

With this authorization you can run the fsync and fsyncUnlock commands.通过此授权,您可以运行fsyncfsyncUnlock命令。

Steps步骤

To take a self-managed backup of a sharded cluster, complete the following steps:要对分片群集进行自我管理备份,请完成以下步骤:

1

Find a Backup Window查找备份窗口

To find a good time to perform a backup, monitor your application and database usage to find a time when chunk migrations, resharding, and schema transformation operations are unlikely to occur.为了找到一个执行备份的好时机,请监视应用程序和数据库使用情况,以找到不太可能发生块迁移、重新标记和模式转换操作的时间。

Note

These steps can only produce a consistent backup if they are followed exactly and no operations are in progress when you begin.只有严格遵循这些步骤,并且在开始时没有进行任何操作,这些步骤才能产生一致的备份。

For more information, see Schedule Backup Window for a Self-Managed Sharded Cluster.有关更多信息,请参阅自我管理分片群集的计划备份窗口

2

Stop the Balancer停止平衡器

To prevent chunk migrations from disrupting the backup, connect to mongos and use the sh.stopBalancer() method to stop the balancer:为了防止块迁移中断备份,请连接到mongos并使用sh.stopBalancer()方法停止平衡器:

sh.stopBalancer()

If a balancing round is in progress, the operation waits for balancing to complete.如果正在进行平衡回合,则操作将等待平衡完成。

To verify that the balancer is stopped, use the sh.getBalancerState() method:要验证平衡器是否已停止,请使用sh.getBalancerState()方法:

use config
while( sh.isBalancerRunning().mode != "off" ) {
print( "Waiting for Balancer to stop..." );
sleep( 1000 );
}
3

Lock the Cluster锁定集群

The sharded cluster must remain locked during the backup process to protect the database from writes, which may cause inconsistencies in the backup.在备份过程中,分片集群必须保持锁定状态,以保护数据库免受写入,这可能会导致备份不一致。

To lock a sharded cluster, connect to mongos and use the db.fsyncLock() method:要锁定分片集群,请连接到mongos并使用db.fsyncLock()方法:

db.getSiblingDB("admin").fsyncLock()

To confirm the lock, on mongos and the primary mongod of the config servers, run the following aggregation pipeline and ensure that all the shards are locked:要确认锁定,在配置服务器的mongos和主mongod上运行以下聚合管道,并确保所有分片都已锁定:

db.getSiblingDB("admin").aggregate( [
{ $currentOp: { } },
{ $facet: {
"locked": [
{ $match: { $and: [
{ fsyncLock: { $exists: true } }
] } }],
"unlocked": [
{ $match: { fsyncLock: { $exists: false } } }
]
} },
{ $project: {
"fsyncLocked": { $gt: [ { $size: "$locked" }, 0 ] },
"fsyncUnlocked": { $gt: [ { $size: "$unlocked" }, 0 ] }
} }
] )
[ { fsyncLocked: true }, { fsyncUnlocked: false } ]
4

Take Backup备份

To back up the sharded cluster, use mongodump to connect to mongos and perform the backup:要备份分片集群,请使用mongodump连接到mongos并执行备份:

mongodump \
--host mongos.example.net \
--port 27017 \
--username user \
--password "passwd" \
--out /opt/backups/example-cluster-1
5

Unlock the Cluster解锁集群

After the backup completes, you can unlock the cluster to allow writes to resume.备份完成后,您可以解锁群集以允许继续写入。

To unlock the cluster, use the db.fsyncUnlock() method:要解锁集群,请使用db.fsyncUnlock()方法:

db.getSibling("admin").fsyncUnlock()

To confirm the unlock, on mongos and the primary mongod of the config servers, run the following aggregation pipeline and ensure that all shards are unlocked:要确认解锁,请在配置服务器的mongos和主mongod上运行以下聚合管道,并确保所有分片都已解锁:

db.getSiblingDB("admin").aggregate( [
{ $currentOp: { } },
{ $facet: {
"locked": [
{ $match: { $and: [
{ fsyncLock: { $exists: true } }
] } }],
"unlocked": [
{ $match: { fsyncLock: { $exists: false } } }
]
} },
{ $project: {
"fsyncLocked": { $gt: [ { $size: "$locked" }, 0 ] },
"fsyncUnlocked": { $gt: [ { $size: "$unlocked" }, 0 ] }
} }
] )
[ { fsyncLocked: false }, { fsyncUnlocked: true } ]
6

Restart the Balancer重新启动平衡器

To restart the balancer, use the sh.startBalancer() method:要重新启动平衡器,请使用sh.startBalancer()方法:

sh.startBalancer()

To confirm that the balancer is running, use the sh.getBalancerState() method:要确认平衡器正在运行,请使用sh.getBalancerState()方法:

sh.getBalancerState()
true

The command returns true when the balancer is running.平衡器运行时,该命令返回true

Next Steps后续步骤

You can restore data from a mongodump backup using mongorestore.您可以使用mongorestoremongodump备份中还原数据。

  • To migrate to a replica set or standalone server, execute mongorestore against mongod.要迁移到副本集或独立服务器,请对mongod执行mongorestore
  • To restore to a sharded cluster, execute mongorestore against mongos with --nsExclude set to exclude the config database:要还原到分片集群,请对mongos执行mongorestore,并设置--nsExclude以排除配置数据库:

    mongorestore --nsExclude='config.*' /data/backup

Important

mongorestore does not shard restored collections on the destination cluster.不会在目标集群上对已还原的集合进行分片。

To ensure that a restored collection is sharded on the destination cluster, create a collection with the same namespace on the destination cluster then shard the collection before running mongorestore. 为了确保还原的集合在目标集群上被分片,请在目标集群中创建一个具有相同命名空间的集合,然后在运行mongorestore之前对该集合进行分片The balancer will eventually spread the restored documents across the shards of the destination cluster.平衡器最终会将恢复的文档分散到目标集群的各个分片上。

For more information, see Back Up and Restore a Self-Managed Deployment with MongoDB Tools.有关更多信息,请参阅使用MongoDB工具备份和还原自我管理部署