mongorestore是一个读取mongodump 二进制导出并将数据恢复到MongoDB 部署的工具。使用mongorestore 从mongodump 备份恢复分片集群。
开始之前
mongodump确保您拥有源集群的 备份。要创建一个,请参阅 使用数据库转储备份自管理分片集群。如果您有文件系统快照,请参阅从Atlas 备份快照恢复自管理分片集群。
步骤
1
停止负载均衡器。
为防止数据块迁移中断恢复,请连接到mongos 并停止负载均衡器:
sh.stopBalancer()
要验证您是否已停止负载均衡器,运行:
use config while( sh.isBalancerRunning().mode != "off" ) { print( "Waiting for Balancer to stop..." ); sleep( 1000 ); }
2
目标集群上的预分片集合。
对于源集群中的每个分片的集合,在目标集群上创建该集合,并在恢复数据之前使用相同的分片键对其分片。
要查找每个集合的分片键,查询实时源集群:
use config db.collections.find()
或者,您可以使用bsondump config/collections.bson检查转储目录中的 文件:
bsondump /path/to/dump/config/collections.bson
{ "_id": "<database>.<collection>", ... "key": { "<shardKeyField>": "<key>" }, ... }
注意
有效的分片键键值为 1 或 "hashed"。
对于每个分片的集合,使用相同的分片键在目标集群上对集合分片:
sh.shardCollection( "<database-name>.<collection-name>", { <shardKeyField>: <key> } )
3
恢复集群。
使用 连接到mongorestore mongos并恢复集群。排除config 数据库以保留目标集群的现有配置:
mongorestore \ --host <host> \ --port 27017 \ --username <username> \ --password "<password>" \ --authenticationDatabase admin \ --nsExclude='config.*' \ /path/to/backup
重要
--drop选项会在恢复之前删除该集合。如果在预分片集合后使用--drop ,mongorestore 将删除分片配置。
5
验证集群可访问性。
连接到mongos 并运行sh.status() 以检查整个集群状态:
sh.status()
要确认所有分片均可访问和通信,请将测试文档插入临时集合中:
db.testRestore.insertOne({ test: 1 })
然后连接到每个分片主节点 (primarydb.collection.find() node in the replica set)并运行 以确认该文档存在:
db.testRestore.find()