mongorestore is a tool that reads mongodump binary exports and restores the data to a MongoDB deployment. Use mongorestore to restore a sharded cluster from a mongodump backup.
始める前に
Ensure you have a mongodump backup of your source cluster. To create one, see Back Up a Self-Managed Sharded Cluster with a Database Dump. If you have file system snapshots, see Restore a Self-Managed Sharded Cluster from Snapshots.
手順
バランサーを停止します。
チャンクの移行によって復元が中断されないようにするには、 mongos に接続してバランサーを停止します。
sh.stopBalancer()
バランサーが停止したことを確認するには、次のコマンドを実行します。
use config while( sh.isBalancerRunning().mode != "off" ) { print( "Waiting for Balancer to stop..." ); sleep( 1000 ); }
宛先クラスターでコレクションを事前シャードします。
ソースクラスター内の各シャーディングされたコレクションについては、データを復元する前に対象クラスター上にコレクションを作成し、同じシャードキーを使用してシャードします。
各コレクションのシャードキーを見つけるには、ライブソースクラスターをクエリします。
use config db.collections.find()
Alternatively, you can use bsondump to inspect the config/collections.bson file from your dump directory:
bsondump /path/to/dump/config/collections.bson
{ "_id": "<database>.<collection>", ... "key": { "<shardKeyField>": "<key>" }, ... }
注意
有効なシャードキー値は 1 または "hashed" です。
シャーディングされたコレクションごとに、同じシャードキーを使用して対象クラスターでコレクションをシャードします。
sh.shardCollection( "<database-name>.<collection-name>", { <shardKeyField>: <key> } )
クラスターを復元します。
Use mongorestore to connect to mongos and restore the cluster. Exclude the config database to preserve the destination cluster's existing configuration:
mongorestore \ --host <host> \ --port 27017 \ --username <username> \ --password "<password>" \ --authenticationDatabase admin \ --nsExclude='config.*' \ /path/to/backup
重要
The --drop option drops the collection before restoring. If you use --drop after pre-sharding collections, mongorestore removes the sharding configuration.
クラスターのアクセス可能性を検証します。
mongos に接続し、sh.status() を実行してクラスター全体のステータスを確認します。
sh.status()
すべてのシャードがアクセス可能であり、通信していることを確認するため、一時的なコレクションにテスト ドキュメントを挿入します。
db.testRestore.insertOne({ test: 1 })
次に、各シャード プライマリに接続し、db.collection.find() を実行してドキュメントが存在することを確認します。
db.testRestore.find()