代码改变世界

MongoDB数据的备份与导出

2019-07-26 17:09  握瑾怀瑜  阅读(317)  评论(0编辑  收藏  举报

0  备份所有数据库  mongodump -o /root/dump1     -o 指定备份文件存在的路径

[root@localhost bin]# mongodump -o /root/dump1
2019-07-26T16:37:48.830+0800    writing admin.system.version to 
2019-07-26T16:37:48.831+0800    done dumping admin.system.version (1 document)
2019-07-26T16:37:48.831+0800    writing admin.test to 
2019-07-26T16:37:48.831+0800    writing admin.testback to 
2019-07-26T16:37:48.832+0800    writing testcopy.order to 
2019-07-26T16:37:48.832+0800    writing testcopy.songbin to 
2019-07-26T16:37:48.834+0800    done dumping testcopy.songbin (1 document)
2019-07-26T16:37:48.847+0800    done dumping admin.testback (8 documents)
2019-07-26T16:37:48.847+0800    done dumping testcopy.order (1 document)
2019-07-26T16:37:48.890+0800    done dumping admin.test (10006 documents)
View Code

1  单一数据库的备份,  -d 参数可以更明智的选择要备份的数据库  

   备份的数据默认在bin目录下的dump目录中(dump最初不存在 执行备份的时候自动创建)

[root@localhost bin]# mongodump -d testcopy
2019-07-26T16:14:08.636+0800    writing testcopy.songbin to 
2019-07-26T16:14:08.639+0800    done dumping testcopy.songbin (1 document)

2  集合级别的备份  由于备份的需求是不同的,有的集合需要每周备份一次 有的集合需要每小时备份一次,

   mongodump -d testcopy -c order     -c 参数让用户可以单独实现特定集合的备份 

[root@localhost bin]# mongodump -d testcopy -c order
2019-07-26T16:26:33.296+0800    writing testcopy.order to 
2019-07-26T16:26:33.298+0800    done dumping testcopy.order (1 document)
View Code

   mongodump -d testcopy -c order -o /root/dump       -o 指定备份文件存在的路径

[root@localhost bin]# mongodump -d testcopy -c order -o /root/dump
2019-07-26T16:32:40.058+0800    writing testcopy.order to 
2019-07-26T16:32:40.059+0800    done dumping testcopy.order (1 document)
View Code

3  导出mongodb数据  -d 数据库 ; -c 集合 ;-o 输出的文件名字

-- 导出数据1w条 加上了--limit限制 
/usr/bin/mongoexport -d zt_terminal -c dataModel --limit=10000 -o /dataModel 
-- 导出所有数据
/usr/bin/mongoexport -d zt_terminal -c historyTimeModel -o /historyTimeModel

4  导入mongodb数据

       -d :数据库名
            -c :collection名
            --type :导入的格式默认json
            -f :导入的字段名
            --headerline :如果导入的格式是csv,则可以使用第一行的标题作为导入的字段
            --file :要导入的文件

-d :数据库名   -c :collection名  --file :要导入的文件
[root@node13 dta]# /usr/bin/mongoimport -d zt_terminal -c dataModel --file /GZ-data/dta/dataModel 2021-08-27T12:14:26.268+0800 connected to: localhost 2021-08-27T12:14:26.570+0800 imported 10000 documents