Mongo备份与恢复

工具

  1. mongoexport/mongoimport
  2. mongodump/mongorestore
    

应用场景总结:
mongoexport/mongoimport:json csv
1. 异构平台迁移 mysql <—> mongodb
2. 同平台,跨大版本:mongodb 2 —-> mongodb 3
mongodump/mongorestore
日常备份恢复时使用.

mongoexport导出使用

mongoexport具体用法如下所示:
$ mongoexport --help  
参数说明:
-h:指明数据库宿主机的IP
-u:指明数据库的用户名
-p:指明数据库的密码
-d:指明数据库的名字
-c:指明表的名字
-f:指明要导出那些列
-o:指明到要导出的文件名
-q:指明导出数据的过滤条件
--authenticationDatabase admin

1.单表备份至json格式
mongoexport -uroot -proot123 --port 27017 --authenticationDatabase admin -d jcwit -c log -o /mongodb/log.json

注:备份文件的名字可以自定义,默认导出了JSON格式的数据。

2. 单表备份至csv格式
如果我们需要导出CSV格式的数据,则需要使用----type=csv参数:

 mongoexport -uroot -proot123 --port 27017 --authenticationDatabase admin -d test -c log --type=csv -f uid,name,age,date  -o /mongodb/log.csv

mongoimport导入使用

$ mongoimport --help
参数说明:
-h:指明数据库宿主机的IP
-u:指明数据库的用户名
-p:指明数据库的密码
-d:指明数据库的名字
-c:指明表的名字
-f:指明要导入那些列
-j, --numInsertionWorkers=<number>  number of insert operations to run concurrently                                                  (defaults to 1)
//并行
数据恢复:
1.恢复json格式表数据到log1
mongoimport -uroot -proot123 --port 27017 --authenticationDatabase admin -d jcwit -c log1 /mongodb/log.json
2.恢复csv格式的文件到log2
上面演示的是导入JSON格式的文件中的内容,如果要导入CSV格式文件中的内容,则需要通过--type参数指定导入格式,具体如下所示:
错误的恢复

注意:
(1)csv格式的文件头行,有列名字
mongoimport   -uroot -proot123 --port 27017 --authenticationDatabase admin   -d jcwit -c log2 --type=csv --headerline --file  /mongodb/log.csv2)csv格式的文件头行,没有列名字
mongoimport   -uroot -proot123 --port 27017 --authenticationDatabase admin   -d jcwit -c log3 --type=csv -f id,name,age,date --file  /mongodb/log.csv
--headerline:指明第一行是列名,不需要导入。

mysql和mongo导入导出

mysql导出csv已逗号分隔

select * from jcwit.t1 into outfile '/tmp/t1.csv' fields terminated by ',';

mongodb导入参考上面导入方法,mysql导出是没有表头的需要-f指定 或者在导出文件中添加表头

批量导出jcwit库多个表

select table_name ,group_concat(column_name) from columns where table_schema='jcwit' group by table_name;

select * from jcwit.t1 into outfile '/tmp/jcwit_t1.csv' fields terminated by ',';

select concat("select * from ",table_schema,".",table_name ," into outfile '/tmp/",table_schema,"_",table_name,".csv' fields terminated by ',';")
from information_schema.tables where table_schema ='jcwit';

mongodump导出使用

$ mongodump --help
参数说明:
-h:指明数据库宿主机的IP
-u:指明数据库的用户名
-p:指明数据库的密码
-d:指明数据库的名字
-c:指明collection的名字
-o:指明到要导出的文件名
-q:指明导出数据的过滤条件
-j, --numParallelCollections=  number of collections to dump in parallel (4 by default)
--oplog  备份的同时备份oplog

全备

mkdir /mongodb/backup
mongodump  -uroot -proot123 --port 27017 --authenticationDatabase admin -o /mongodb/backup

备份单库jcwit

$ mongodump   -uroot -proot123 --port 27017 --authenticationDatabase admin -d jcwit -o /mongodb/backup/

备份jcwit下的t1集合

$ mongodump   -uroot -proot123 --port 27017 --authenticationDatabase admin -d jcwit -c t1 -o /mongodb/backup/

压缩备份

参数使用  --gzip

mongorestore恢复备份

$ mongorestore   -uroot -proot123 --port 27017 --authenticationDatabase admin -d jcwit  /mongodb/backup/jcwit

mongorestore   -uroot -proot123 --port 27017 --authenticationDatabase admin -d jcwit -c t1  --gzip  /mongodb/backup.bak/jcwit/t1.bson.gz

--drop 强制删除 然后恢复

Mongo备份与恢复
http://www.jcwit.com/article/78/
作者
Carlos
发布于
2019年6月23日
许可协议