Mongodump 命令參考文檔:
https://docs.mongodb.com/v4.2/reference/program/mongodump/#bin.mongodump
1、mongodump --help 詳解:
general options: --help 輸出幫助說明 --version 輸出版本號 verbosity options: -v, --verbose=<level> 增加備份過程中日志詳細程序,例如 -vvvv 打的日志最多
--quiet 備份過程中不輸出日志 connection options: -h, --host=<hostname> 連接地址 (setname/host1,host2 for replica sets) --port=<port> 端口號 可以 --host hostname:port(can also use --host hostname:port) ssl options: --ssl connect to a mongod or mongos that has ssl enabled --sslCAFile=<filename> the .pem file containing the root certificate chain from the certificate authority --sslPEMKeyFile=<filename> the .pem file containing the certificate and key --sslPEMKeyPassword=<password> the password to decrypt the sslPEMKeyFile, if necessary --sslCRLFile=<filename> the .pem file containing the certificate revocation list --sslFIPSMode use FIPS mode of the installed openssl library --tlsInsecure bypass the validation for server's certificate chain and host name authentication options: -u, --username=<username> 用戶名
-p, --password=<password> 密碼
--authenticationDatabase=<database-name> 指定驗證庫 --authenticationMechanism=<mechanism> 指定驗證機制 kerberos options: --gssapiServiceName=<service-name> service name to use when authenticating using GSSAPI/Kerberos (default: mongodb) --gssapiHostName=<host-name> hostname to use when authenticating using GSSAPI/Kerberos (default: <remote server's address>) namespace options: -d, --db=<database-name> 指定庫名 -c, --collection=<collection-name> 指定集合名稱 uri options: --uri=mongodb-uri 以mongodb uri 地址連接 query options: -q, --query= 指定過濾查詢語句, '{"x":{"$gt":1}}' --queryFile= 指定過濾查詢語句文件 (v2 Extended JSON) --readPreference=<string>|<json> specify either a preference mode (e.g. 'nearest') or a preference json object (e.g. '{mode: "nearest", tagSets: [{a: "b"}], maxStalenessSeconds: 123}') --forceTableScan force a table scan (do not use $snapshot or hint _id). Deprecated since this is default behavior on WiredTiger output options: -o, --out=<directory-path> 備份輸出到哪個目錄 (defaults to 'dump') --gzip 壓縮備份文件 --oplog 備份oplog 完成一致性快照備份
--archive=<file-path> 備份成一個歸檔文件,不能和 -o 參數同時使用
--dumpDbUsersAndRoles 備份數據庫的用戶、角色信息
--excludeCollection=<collection-name> 過濾掉哪些集合,多個集合,需要使用多個--excludeCollection 參數
--excludeCollectionsWithPrefix=<collection-prefix> exclude all collections from the dump that have the given prefix (may be specified multiple times to exclude additional prefixes) -j, --numParallelCollections= 並行備份線程個數 (4 by default) --viewsAsCollections 將只讀視圖導出來集合,恢復時,視圖會恢復為集合
1.1 循環寫入測試數據
use czg
for(i=1;i<=100;i++){db.t1.insert({id:i})} for(i=1;i<=100;i++){db.t2.insert({id:i,name:"ceshi2"})} for(i=1;i<=100;i++){db.t3.insert({id:i,name:"ceshi3"})}
for(i=1;i<=100;i++){db.a1.insert({id:i,name:"ceshi4"})}
db.t1.createIndex({id:1})
db.t1.createIndex({name:1})
db.t2.createIndex({id:1})
db.t2.createIndex({name:1})
use czg
db.createUser({user:'ceshi',pwd:'c123456', roles:[{role:'readWrite', db:'czg'}]})
1.2 創建用戶
use czg db.createUser({user:'ceshi',pwd:'c123456', roles:[{role:'readWrite', db:'czg'}]})
1.3 備份所有數據庫
mongodump -h 127.0.0.1:27017 --authenticationDatabase=admin -u root -p c123456 -o /root/mongo_dump/
1.4 備份所有數據庫和oplog
mongodump -h 127.0.0.1:27017 --authenticationDatabase=admin -u root -p c123456 -o /root/mongo_dump/
1.5 備份所有數據庫和oplog,並且進行壓縮
mongodump -h 127.0.0.1:27017 --authenticationDatabase=admin -u root -p c123456 --gzip -o /root/mongo_dump/
1.6 以 uri 地址連接數據庫進行備份所有數據和oplog
mongodump --uri="mongodb://root:c123456@127.0.0.1:27017/" --oplog -o /root/mongo_dump
1.7 備份指定數據庫
mongodump -h 127.0.0.1:27017 --authenticationDatabase=admin -u root -p c123456 -d czg -o /root/mongo_dump/
1.8 備份指定數據庫和庫中的用戶角色信息
mongodump -h 127.0.0.1:27017 --authenticationDatabase=admin -u root -p c123456 -d czg --dumpDbUsersAndRoles -o /root/mongo_dump/
1.9 備份指定集合 t1
mongodump -h 127.0.0.1:27017 --authenticationDatabase=admin -u root -p c123456 -d czg -c t1 -o /root/mongo_dump/
1.10 備份指定集合 t1,並進行條件過濾
mongodump -h 127.0.0.1:27017 --authenticationDatabase=admin -u root -p c123456 -d czg -c t1 -q='{"id":{"$gte":50}}' -o /root/mongo_dump/
1.11 備份整庫,但過濾掉部分集合不備份(示例中過濾掉 t1 t2集合)
mongodump -h 127.0.0.1:27017 --authenticationDatabase=admin -u root -p c123456 -d czg --excludeCollection=t1 --excludeCollection=t2 -o /root/mongo_dump/
1.12 備份整庫到指定文件
mongodump -h 127.0.0.1:27017 --authenticationDatabase=admin -u root -p c123456 -d czg --archive=/root/mongo_dump/mongo.bak
2、mongorestore --help 詳解
參考文檔:
https://docs.mongodb.com/v4.2/reference/program/mongorestore/
--help 打印幫助信息 --version 打印版本號 verbosity options: -v, --verbose=<level> 增加備份過程中日志詳細程序,例如 -vvvv 打的日志最多 --quiet 備份過程中不輸出日志 connection options: -h, --host=<hostname> 連接地址 (setname/host1,host2 for replica sets) --port=<port> 端口號 可以 --host hostname:port(can also use --host hostname:port) ssl options: --ssl connect to a mongod or mongos that has ssl enabled --sslCAFile=<filename> the .pem file containing the root certificate chain from the certificate authority --sslPEMKeyFile=<filename> the .pem file containing the certificate and key --sslPEMKeyPassword=<password> the password to decrypt the sslPEMKeyFile, if necessary --sslCRLFile=<filename> the .pem file containing the certificate revocation list --sslFIPSMode use FIPS mode of the installed openssl library --tlsInsecure bypass the validation for server's certificate chain and host name authentication options: -u, --username=<username> 用戶名 -p, --password=<password> 密碼 --authenticationDatabase=<database-name> 指定驗證庫 --authenticationMechanism=<mechanism> 指定驗證機制 kerberos options: --gssapiServiceName=<service-name> service name to use when authenticating using GSSAPI/Kerberos (default: mongodb) --gssapiHostName=<host-name> hostname to use when authenticating using GSSAPI/Kerberos (default: <remote server's address>) uri options: --uri=mongodb-uri mongodb uri 連接信息 namespace options: -d, --db=<database-name> 如果不指定 -d 參數,會從備份目錄中獲取庫名稱,導入單表時可以指定導入具體庫 -c, --collection=<collection-name> 如果不指定 -c 參數,會從備份目錄中獲取集合名稱,導入單表時可以指定導入集合名稱 --excludeCollection=<collection-name> 參數已經移除。DEPRECATED; collection to skip over during restore (may be specified multiple times to exclude additional collections) --excludeCollectionsWithPrefix=<collection-prefix> 參數已經移除。DEPRECATED; collections to skip over during restore that have the given prefix (may be specified multiple times to exclude additional prefixes) --nsExclude=<namespace-pattern> 排除匹配的命令名空間,例如:"test.myCollection"、 "reporting.*"、"dept*.bar" --nsInclude=<namespace-pattern> 恢復匹配的命令名空間,例如:"test.myCollection"、 "reporting.*"、"dept*.bar" --nsFrom=<namespace-pattern> 重命名命名空間,參數必須包含 nsTo --nsTo=<namespace-pattern> 重命名命名空間,參數必須包含 nsFrom input options: --objcheck 在插入前檢查記錄有效性 --oplogReplay 恢復數據后,重放oplog --oplogLimit=<seconds>[:ordinal] only include oplog entries before the provided Timestamp --oplogFile=<filename> 指定 oplog 文件,用於恢復 oplog 數據 --archive=<filename> 從指定文件進行恢復,如果未指定文件,則從標准輸入中進行恢復 --restoreDbUsersAndRoles 恢復指定數據庫的用戶和角色信息 --dir=<directory-name> 指定恢復目錄 --gzip 從壓縮文件中進行恢復 restore options: --drop 導入集合前先刪掉集合,不會刪除不會備份中的集合 --dryRun view summary without importing anything. recommended with verbosity --writeConcern=<write-concern> write concern options e.g. --writeConcern majority, --writeConcern '{w: 3, wtimeout: 500, fsync: true, j: true}' --noIndexRestore don't restore indexes --convertLegacyIndexes Removes invalid index options and rewrites legacy option values (e.g. true becomes 1). --noOptionsRestore don't restore collection options --keepIndexVersion 防止在恢復數據過程中升級索引到最新版本 --maintainInsertionOrder restore the documents in the order of their appearance in the input source. By default the insertions will be performed in an arbitrary order. Setting this flag also enables the behavior of --stopOnError and restricts NumInsertionWorkersPerCollection to 1. -j, --numParallelCollections= 並行恢復線程數,默認為4 (4 by default) --numInsertionWorkersPerCollection= number of insert operations to run concurrently per collection (1 by default) --stopOnError 強制mongostore 在遇到錯誤時停止還原 --bypassDocumentValidation 啟用在操作期間繞過文檔驗證,這樣台要插入不符合要求的文檔 --preserveUUID preserve original collection UUIDs (off by default, requires drop) --fixDottedHashIndex when enabled, all the hashed indexes on dotted fields will be created as single field ascending indexes on the destination
2.1 恢復所有數據到數據庫
mongorestore -h 127.0.0.1:27017 --authenticationDatabase=admin -u root -p c123456 /root/mongo_dump
2.2 恢復所有數據到數據庫,並應用oplog
mongorestore -h 127.0.0.1:27017 --authenticationDatabase=admin -u root -p c123456 --oplogReplay /root/mongo_dump
2.3 恢復單表數據到數據庫
mongorestore -h 127.0.0.1:27017 --authenticationDatabase=admin -u root -p c123456 /root/mongo_dump/czg/t2.bson
2.4 恢復單表數據到指定數據庫 czg_new
mongorestore -h 127.0.0.1:27017 --authenticationDatabase=admin -u root -p c123456 -d czg_new /root/mongo_dump/czg/t2.bson
2.5 恢復單表數據到指定數據庫 czg_new , 指定集合 t2_new
mongorestore -h 127.0.0.1:27017 --authenticationDatabase=admin -u root -p c123456 -d czg_new -c t2_new /root/mongo_dump/czg/t2.bson
2.6 恢復數據到數據庫,但過濾掉部分表
mongorestore -h 127.0.0.1:27017 --authenticationDatabase=admin -u root -p c123456 --nsExclude="czg.a*" /root/mongo_dump/
2.7 恢復數據到數據庫,只恢復部分表 czg.a* (日常可用於單庫、單表、恢復)
mongorestore -h 127.0.0.1:27017 --authenticationDatabase=admin -u root -p c123456 --nsInclude="czg.a*" /root/mongo_dump/
2.8 恢復數據到數據庫,將 czg.a* 集合,恢復到 czg_new.a* 集合中
mongorestore -h 127.0.0.1:27017 --authenticationDatabase=admin -u root -p c123456 --nsFrom="czg.a*" --nsTo="czg_new.a*" /root/mongo_dump/
2.9 從備份文件進行恢復,同時恢復后庫名為czg_new
mongorestore -h 127.0.0.1:27017 --authenticationDatabase=admin -u root -p c123456 --archive=/root/mongo_dump/mongo.bak --nsFrom='czg.*' --nsTo='czg_new.*'
2.10 從壓縮文件或目錄中恢復,需要加 --gzip 參數
mongorestore -h 127.0.0.1:27017 --authenticationDatabase=admin -u root -p c123456 --gzip /root/mongo_dump