mongodump备份工具
mongodump能够在Mongodb运行时进行备份,它的工作原理是对运行的Mongodb做查询,然后将所有查到的文档写入磁盘。
mongodump的参数与mongoexport的参数基本一致
正文
1.1 mongodump关键参数如下所示:
mongodump --help 查看帮助命令
-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,指定用户鉴定库
1.2 mongodump参数实践
全库备份
mongodump -u -p --port 28020 --authenticationDatabase admin -o /tmp/backup
备份指定的库,test库
mongodump --port 27017 --authenticationDatabase admin -d flybycloud -o /tmp/backup
mongorestore --port 27017 --authenticationDatabase admin -d flybycloud ./flybycloud
备份test库下的customer集合
mongodump --port 27017 --authenticationDatabase admin -d flybycloud -c channelHotelImg -o /tmp/backup
mongorestore --port 27017 --authenticationDatabase admin -d test -c channelHotelImg /tmp/backup/flybycloud/channelHotelImg.bson
压缩备份
压缩备份单库
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
2.mongorestore恢复工具
2.1 mongorestore关键参数如下所示:
mongodump --help 查看帮助命令
-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,指定用户鉴定库
2.2 mongorestore参数实践
单库恢复
mongorestore -u -p --port 28018 --authenticationDatabase admin -d test /tmp/backup/test
[root@test-node6 test]
2023-05-05T23:44:33.113+0800 the --db and --collection args should only be used when restoring from a BSON file. Other uses are deprecated and will not exist in the future; use --nsInclude instead
2023-05-05T23:44:33.113+0800 building a list of collections to restore from /data/tmp/dump/test dir
2023-05-05T23:44:33.115+0800 reading metadata for test.qunar_hote_rating from /data/tmp/dump/test/qunar_hote_rating.metadata.json.gz
2023-05-05T23:44:33.115+0800 reading metadata for test.qunar_hotel_rating from /data/tmp/dump/test/qunar_hotel_rating.metadata.json.gz
2023-05-05T23:44:33.115+0800 reading metadata for test.dida_hotel_description from /data/tmp/dump/test/dida_hotel_description.metadata.json.gz
2023-05-05T23:44:33.115+0800 reading metadata for test.elong_rp from /data/tmp/dump/test/elong_rp.metadata.json.gz
2023-05-05T23:44:33.120+0800 restoring test.qunar_hote_rating from /data/tmp/dump/test/qunar_hote_rating.bson.gz
2023-05-05T23:44:33.138+0800 restoring test.qunar_hotel_rating from /data/tmp/dump/test/qunar_hotel_rating.bson.gz
2023-05-05T23:44:33.144+0800 restoring test.dida_hotel_description from /data/tmp/dump/test/dida_hotel_description.bson.gz
2023-05-05T23:44:33.157+0800 restoring test.elong_rp from /data/tmp/dump/test/elong_rp.bson.gz
2023-05-05T23:44:36.129+0800 [........................] test.qunar_hote_rating 12.1MB/891MB (1.4%)
2023-05-05T23:44:36.129+0800 [........................] test.qunar_hotel_rating 11.0MB/376MB (2.9%)
2023-05-05T23:44:36.129+0800 [
2023-05-05T23:44:36.129+0800 [
2023-05-05T23:44:36.129+0800
2023-05-05T23:44:39.112+0800 [........................] test.qunar_hote_rating 22.7MB/891MB (2.5%)
2023-05-05T23:44:39.112+0800 [
2023-05-05T23:44:39.112+0800 [
2023-05-05T23:44:39.112+0800 [
2023-05-05T23:44:39.112+0800
复制
恢复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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了