MongoDB数据库逻辑备份(mongodump)以及恢复(mongorestore)

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值