《云计算》-mongoDB:数据导入导出/备份/恢复

3 案例3:数据导入导出/备份/恢复
3.1 问题

要求如下:
练习数据导入导出
练习数据备份恢复

3.2 步骤

实现此案例需要按照如下步骤进行。

步骤一:数据备份与恢复

1)数据备份

 [root@mongodb51 ~]# mongodump --host  192.168.4.51 --port 27077
//不指定备份哪个库,默认备份所有,不指定目录,自动生成dump目录,备份的数据在这个里面
2018-09-28T18:14:12.585+0800    writing admin.system.version to 
2018-09-28T18:14:12.586+0800    done dumping admin.system.version (1 document)
2018-09-28T18:14:12.586+0800    writing ddsdb.t1 to 
2018-09-28T18:14:12.586+0800    writing test.t1 to 
2018-09-28T18:14:12.586+0800    writing ddsdb.col to 
2018-09-28T18:14:12.587+0800    done dumping ddsdb.t1 (17 documents)
2018-09-28T18:14:12.588+0800    done dumping test.t1 (1 document)
2018-09-28T18:14:12.588+0800    done dumping ddsdb.col (0 documents)
[root@mongodb51 ~]# ls
dump
[root@mongodb51 ~]# bsondump dump/ddsdb/t1.bson        //查看bson文件内容
{"_id":{"$oid":"5badf71520cdd1574b851f16"},"name":"bob","work":null}
{"_id":9.0,"name":"jerry","work":null}
...
...
{"_id":{"$oid":"5badfd626827555e3fd86817"},"lname":"html","codecript":{"$code":"function (){/*...*/}"}}
{"_id":{"$oid":"5badfd6a6827555e3fd86818"},"lname":"html","codecript":{"$code":"function (){/*\u003chtml\u003e\u003ch1\u003eabc\u003c/h1\u003e\u003c/html\u003e*/}"}}
2018-09-28T18:15:45.948+0800    17 objects found

备份时指定备份的库和备份目录

[root@mongodb51 ~]# mongodump --host  192.168.4.51 --port 27077  -d  ddsdb -o /root/bbsdb
2018-09-28T18:23:30.389+0800    writing ddsdb.t1 to 
2018-09-28T18:23:30.389+0800    writing ddsdb.col to 
2018-09-28T18:23:30.391+0800    done dumping ddsdb.t1 (17 documents)
2018-09-28T18:23:30.391+0800    done dumping ddsdb.col (0 documents) 
//-d备哪个库,-o指定备份的目录,备份bbsdb库里的所有到/root/bbsdb

只备份ddsdb库里的集合t1

[root@mongodb51 ~]#  mongodump --host  192.168.4.51 --port 27077  -d  ddsdb -c t1 -o /root/bbsdb.t 
2018-09-28T18:19:00.210+0800    writing ddsdb.t1 to 
2018-09-28T18:19:00.211+0800    done dumping ddsdb.t1 (17 documents)

2)恢复数据

[root@mongodb51 ~]# mongo --host 192.168.4.51 --port 27077
> show tables;
col
t1
> db.t1.remove({})
WriteResult({ "nRemoved" : 17 })
>exit
[root@mongodb51 ~]# mongorestore --host  192.168.4.51 --port 27077  -d  ddsdb  /root/bbsdb.t/ddsdb/
//-d  ddsdb恢复到数据库的目录,从/root/bbsdb.t1/ddsdb/目录恢复
2018-09-28T18:26:16.889+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
2018-09-28T18:26:16.890+0800    building a list of collections to restore from /root/bbsdb.t/ddsdb dir
2018-09-28T18:26:16.891+0800    reading metadata for ddsdb.t1 from /root/bbsdb.t/ddsdb/t1.metadata.json
2018-09-28T18:26:16.891+0800    restoring ddsdb.t1 from /root/bbsdb.t/ddsdb/t1.bson
2018-09-28T18:26:16.893+0800    no indexes to restore
2018-09-28T18:26:16.893+0800    finished restoring ddsdb.t1 (17 documents)
2018-09-28T18:26:16.893+0800    done

步骤二:数据的导入导出

1)导出

用csv的格式导出

 [root@mongodb51 ~]# mongoexport  --host  192.168.4.51 --port 27077  -d  ddsdb  -c t1 -f name --type=csv    -o /root/lig1.csv
//导出csv格式,必须要指定导出的字段名 ,导出name字段 
2018-09-28T18:29:24.653+0800    connected to: 192.168.4.51:27077
2018-09-28T18:29:24.654+0800    exported 17 records
[root@mongodb51 ~]# cat lig1.csv
name
bob
jerry
zhangsan
lisi
alice
lilei
hehe
呵呵
bobo
jerry
yaya
[root@mongodb51 ~]# mongoexport  --host  192.168.4.51 --port 27077  -d  ddsdb  -c t1 -q '{name:"bob"}' -f name,age --type=csv    -o /root/lig2.csv
//从ddsdb的它1里导出名字为bob的name字段和age字段
2018-09-28T18:31:25.627+0800    connected to: 192.168.4.51:27077
2018-09-28T18:31:25.628+0800    exported 1 record

用json的格式导出

[root@mongodb51 ~]# mongoexport  --host  192.168.4.51 --port 27077 -d ddsdb -c t1 --type=json    -o /root/lig3.json
//导出json格式
2018-09-28T18:33:13.349+0800    connected to: 192.168.4.51:27077
2018-09-28T18:33:13.350+0800    exported 17 records
[root@mongodb51 ~]# mongoexport  --host  192.168.4.51 --port 27077 -d ddsdb -c t1 -f name --type=json    -o /root/lig4.json
//指定列名导出,导出name字段
2018-09-28T18:33:35.914+0800    connected to: 192.168.4.51:27077
2018-09-28T18:33:35.915+0800    exported 17 records

2)导入

[root@mongodb51 ~]# mongo --host 192.168.4.51 --port 27077
> use ddsdb
switched to db ddsdb
> show tables;
col
t1
> db.t1.remove({})
WriteResult({ "nRemoved" : 17 })
> exit

用json的格式导入:表里要没有数据,不然导入不成功

[root@mongodb51 ~]# mongoimport --host  192.168.4.51 --port 27077 -d ddsdb -c t1 --type=json       /root/lig3.json
2018-09-28T18:35:22.341+0800    connected to: 192.168.4.51:27077
2018-09-28T18:35:22.343+0800    imported 17 documents
[root@mongodb51 ~]# mongo --host 192.168.4.51 --port 27077 
> use ddsdb
switched to db ddsdb
> db.t1.count({})
17

用csv的格式导入:表里可以有数据

[root@mongodb51 ~]# mongoimport --host  192.168.4.51 --port 27077  -d ddsdb -c t1   --headerline  --type=csv /root/lig1.csv
//必须指定文件的列名,不然不成功 -f和--headerline不能一起用  --headerline:把第一行的字段隐藏即去掉文件列的标题name,不然标题也会导进去,导入时t1表可以不存在
2018-09-28T18:37:36.778+0800    connected to: 192.168.4.51:27077
2018-09-28T18:37:36.779+0800    imported 11 documents
发布了222 篇原创文章 · 获赞 43 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/xie_qi_chao/article/details/104721998