mongodump/mongorestore命令详解
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 --oplog -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