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()