mongodump关键参数如下所示:
-h,--host :代表远程连接的数据库地址,默认连接本地Mongo数据库;
--port:代表远程连接的数据库的端口,默认连接的远程端口27017;
-u,--username:代表连接远程数据库的账号,如果设置数据库的认证,需要指定用户账号;
-p,--password:代表连接数据库的账号对应的密码;
-d,--db:代表连接的数据库;
-c,--collection:代表连接数据库中的集合;
-o, --out:代表导出的文件输出目录;
-q, --query:代表查询条件;
-j,--numParallelCollections =要并行转储的集合数(默认为4)
--gzip,使用Gzip压缩存档;
--oplog,使用oplog进行时间点快照;
--authenticationDatabase,指定用户鉴定库
mongodump参数实践
全库备份
mongodump -u -p --port 28020 --authenticationDatabase admin -o /tmp/backup
备份指定的库,test库
mongodump -u -p --port 28020 --authenticationDatabase admin -d test -o /tmp/backup
备份test库下的customer集合
mongodump -u -p --port 28020 --authenticationDatabase admin -d test -c customer -o /tmp/backup
压缩备份
压缩备份单库
mongodump -u -p --port 28020 --authenticationDatabase admin -d test -o /tmp/backup --gzip
压缩备份单表
mongodump -u -p --port 28020 --authenticationDatabase admin -d test -c customer -o /tmp/backup --gzip
mongorestore关键参数如下所示
-h,--host :代表远程连接的数据库地址,默认连接本地Mongo数据库;
--port:代表远程连接的数据库的端口,默认连接的远程端口27017;
-u,--username:代表连接远程数据库的账号,如果设置数据库的认证,需要指定用户账号;
-p,--password:代表连接数据库的账号对应的密码;
-d,--db:代表连接的数据库;
-c,--collection:代表连接数据库中的集合;
-o, --out:代表导出的文件输出目录;
--dir = <目录名称>输入目录
--drop导入前删除数据库中集合;
--gzip,解压Gzip压缩存档还原;
--oplog,重放oplog以基于时间点还原;
--oplogFile = <文件名>指定重播oplog的oplog文件
--authenticationDatabase,指定用户鉴定库
mongorestore参数实践
单库恢复
mongorestore -u -p --port 28018 --authenticationDatabase admin -d test /tmp/backup/test
恢复test库下的customer集合
mongorestore -u -p --port 28018 --authenticationDatabase admin -d test -c customer /tmp/backup/test/customer.bson
–drop参数实践恢复
恢复的时候,先删除当前数据,然后恢复备份的数据。就是说,备份后添加修改的数据都会被删除,谨慎使用!!!
# 恢复单库
mongorestore -u -p --port 28018 --authenticationDatabase admin -d test --drop /tmp/backup/test
# 恢复单表
mongorestore -u -p --port 28018 --authenticationDatabase admin -d test -c customer --drop /tmp/backup/test/customer.bson
–gzip参数实践恢复
mongorestore -u -p --port 28018 --authenticationDatabase admin --gzip /tmp/backup