1.下载
https://www.mongodb.com/try/download/database-tools
curl -O https://fastdl.mongodb.org/tools/db/mongodb-database-tools-rhel70-x86_64-100.6.1.tgz
2.解压
tar -zxvf mongodb-database-tools-rhel70-x86_64-100.6.1.tgz
3.实践
3.1备份
[root@10-0-42-211 bin]# ./mongodump --host 10.0.42.211 --port 27017 --out /data/backup/
2022-11-22T20:47:54.070+0800 writing admin.system.users to /data/backup/admin/system.users.bson
2022-11-22T20:47:54.071+0800 done dumping admin.system.users (1 document)
2022-11-22T20:47:54.071+0800 writing admin.system.version to /data/backup/admin/system.version.bson
2022-11-22T20:47:54.072+0800 done dumping admin.system.version (2 documents)
2022-11-22T20:47:54.074+0800 writing db01.db01 to /data/backup/db01/db01.bson
2022-11-22T20:47:54.075+0800 writing config.external_validation_keys to /data/backup/config/external_validation_keys.bson
2022-11-22T20:47:54.076+0800 writing config.tenantMigrationDonors to /data/backup/config/tenantMigrationDonors.bson
2022-11-22T20:47:54.076+0800 writing config.tenantMigrationRecipients to /data/backup/config/tenantMigrationRecipients.bson
2022-11-22T20:47:54.077+0800 done dumping config.tenantMigrationDonors (0 documents)
2022-11-22T20:47:54.078+0800 done dumping config.external_validation_keys (0 documents)
2022-11-22T20:47:54.079+0800 done dumping db01.db01 (1 document)
2022-11-22T20:47:54.079+0800 done dumping config.tenantMigrationRecipients (0 documents)
3.2 恢复
[root@10-0-42-211 bin]# ./mongorestore --host 10.0.42.211 --port 27017 /data/backup/
2022-11-22T20:51:03.347+0800 preparing collections to restore from
2022-11-22T20:51:03.349+0800 reading metadata for config.external_validation_keys from /data/backup/config/external_validation_keys.metadata.json
2022-11-22T20:51:03.350+0800 reading metadata for config.tenantMigrationDonors from /data/backup/config/tenantMigrationDonors.metadata.json
2022-11-22T20:51:03.350+0800 reading metadata for config.tenantMigrationRecipients from /data/backup/config/tenantMigrationRecipients.metadata.json
2022-11-22T20:51:03.350+0800 reading metadata for db01.db01 from /data/backup/db01/db01.metadata.json
2022-11-22T20:51:03.351+0800 restoring to existing collection config.external_validation_keys without dropping
2022-11-22T20:51:03.351+0800 restoring config.external_validation_keys from /data/backup/config/external_validation_keys.bson
2022-11-22T20:51:03.352+0800 restoring to existing collection db01.db01 without dropping
2022-11-22T20:51:03.352+0800 restoring db01.db01 from /data/backup/db01/db01.bson
2022-11-22T20:51:03.353+0800 restoring to existing collection config.tenantMigrationDonors without dropping
2022-11-22T20:51:03.353+0800 restoring config.tenantMigrationDonors from /data/backup/config/tenantMigrationDonors.bson
2022-11-22T20:51:03.354+0800 restoring to existing collection config.tenantMigrationRecipients without dropping
2022-11-22T20:51:03.354+0800 restoring config.tenantMigrationRecipients from /data/backup/config/tenantMigrationRecipients.bson
2022-11-22T20:51:03.366+0800 finished restoring config.external_validation_keys (0 documents, 0 failures)
2022-11-22T20:51:03.366+0800 finished restoring db01.db01 (1 document, 0 failures)
2022-11-22T20:51:03.368+0800 finished restoring config.tenantMigrationDonors (0 documents, 0 failures)
2022-11-22T20:51:03.385+0800 finished restoring config.tenantMigrationRecipients (0 documents, 0 failures)
2022-11-22T20:51:03.385+0800 restoring users from /data/backup/admin/system.users.bson
2022-11-22T20:51:03.473+0800 no indexes to restore for collection db01.db01
2022-11-22T20:51:03.473+0800 restoring indexes for collection config.tenantMigrationDonors from metadata
2022-11-22T20:51:03.473+0800 index: &idx.IndexDocument{Options:primitive.M{"expireAfterSeconds":0, "name":"TenantMigrationDonorTTLIndex", "v":2}, Key:primitive.D{primitive.E{Key:"expireAt", Value:1}}, PartialFilterExpression:primitive.D(nil)}
2022-11-22T20:51:03.473+0800 restoring indexes for collection config.tenantMigrationRecipients from metadata
2022-11-22T20:51:03.473+0800 index: &idx.IndexDocument{Options:primitive.M{"expireAfterSeconds":0, "name":"TenantMigrationRecipientTTLIndex", "v":2}, Key:primitive.D{primitive.E{Key:"expireAt", Value:1}}, PartialFilterExpression:primitive.D(nil)}
2022-11-22T20:51:03.474+0800 restoring indexes for collection config.external_validation_keys from metadata
2022-11-22T20:51:03.474+0800 index: &idx.IndexDocument{Options:primitive.M{"expireAfterSeconds":0, "name":"ExternalKeysTTLIndex", "v":2}, Key:primitive.D{primitive.E{Key:"ttlExpiresAt", Value:1}}, PartialFilterExpression:primitive.D(nil)}
2022-11-22T20:51:03.476+0800 1 document(s) restored successfully. 0 document(s) failed to restore.
参考文档
https://www.mongodb.com/docs/database-tools/