3.MongoDB的备份与恢复
数据准备:
[mongod@nova-02 ~]$ mongo 10.0.0.212/admin
MongoDB shell version: 3.2.8
connecting to: 10.0.0.212/admin
> show dbs;
admin 0.001GB
local 0.000GB
meng 0.000GB
ming 0.000GB
test 0.000GB
> use meng
switched to db meng
> db.getCollectionNames()
[ "a", "b", "meng" ]
> db.meng.drop()
true
> db.getCollectionNames()
[ "a", "b" ]
> db.c.insert({name:'meng'});
WriteResult({ "nInserted" : 1 })
> db.getCollectionNames()
[ "a", "b", "c" ]
> db.c.insert({name:'ming'});
WriteResult({ "nInserted" : 1 })
> db.getCollectionNames()
[ "a", "b", "c" ]
> db.a.find()
> db.b.find()
> db.c.find()
{ "_id" : ObjectId("5af02d84bf3bef3588597703"), "name" : "meng" }
{ "_id" : ObjectId("5af02d90bf3bef3588597704"), "name" : "ming" }
[mongod@nova-02 ~]$ netstat -lntup|grep 'mongod'
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 1783/mongod
[root@nova-02 ~]# echo 'export PATH=/application/mongodb/bin:$PATH' >>/etc/profile
[root@nova-02 ~]# source /etc/profile
[root@nova-02 ~]# mongo
mongo mongodump mongofiles mongooplog mongorestore mongostat
mongod mongoexport mongoimport mongoperf mongos mongotop
MongoDB的备份与恢复
1.MongoDB的常用命令:
(1)导出工具mongoexport
Mongodb中的mongoexport工具可以把一个collection导出成JSON格式或CSV格式的文件。可以通过参数指定导出的数据项,也可以根据指定的条件导出数据。
a.备份meng库下的c集合:
[root@nova-02 ~]# mongoexport -h 10.0.0.212:27017 --authenticationDatabase admin -d meng -c c -o /opt/c_s.dat
[root@nova-02 ~]# ll /opt/c_s.dat
-rw-r--r--. 1 root root 116 May 7 19:01 /opt/c_s.dat
[root@nova-02 ~]# mongoexport -h 10.0.0.212:27017 -uroot -proot --authenticationDatabase admin -d meng -c c -o /opt/c_s.dat //有密码情况。
[root@nova-02 ~]# ll /opt/c_s.dat
-rw-r--r--. 1 root root 116 May 7 18:59 /opt/c_s.dat
[root@nova-02 ~]# cat /opt/c_s.dat
{"_id":{"$oid":"5af02d84bf3bef3588597703"},"name":"meng"}
{"_id":{"$oid":"5af02d90bf3bef3588597704"},"name":"ming"}
该命令的参数如下:
-h:指明数据库宿主机的IP
-u:指明数据库的用户名
-p:指明数据库的密码
-d:指明数据库的名字
-c:指明collection的名字
-f:指明要导出哪些列
-o:指明到要导出的文件名
-q:指明导出数据的过滤条件
--type:指定文件类型
--authenticationDatabase:验证数据的名称
注:备份文件的名字可以自定义,默认导出了JSON格式的数据。
b.导出CSV格式的数据:
[root@nova-02 ~]# mongoexport -h 10.0.0.212:27017 --authenticationDatabase admin -d meng -c c --type=csv -f id,name -o /opt/c_csv.dat
[root@nova-02 ~]# ll /opt/c_csv.dat
-rw-r--r--. 1 root root 20 May 7 19:04 /opt/c_csv.dat
[root@nova-02 ~]# cat /opt/c_csv.dat
id,name
,meng
,ming
(2)导入工具mongoimport
Mongodb中的mongoimport工具可以把一个特定格式文件中的内容导入到指定的collection中。该工具可以导入JSON格式数据,也可以导入CSV格式数据。
a.将之前恢复的数据导入:
[root@nova-02 ~]# cat /opt/c_i.dat
{"_id":{"$oid":"5af02d84bf3bef3588597703"},"name":"meng"}
{"_id":{"$oid":"5af02d90bf3bef3588597704"},"name":"ming"}
{"_id":{"$oid":"5af02d90bf3bef3588597705"},"name":"ying"}
[root@nova-02 ~]# mongoimport -h 10.0.0.212:27017 --authenticationDatabase admin -d meng -c c --drop /opt/c_i.dat
2018-05-07T19:14:18.718+0800 connected to: 10.0.0.212:27017
2018-05-07T19:14:18.728+0800 dropping: meng.c
2018-05-07T19:14:18.814+0800 imported 3 documents
登录查看:
> db.c.find()
{ "_id" : ObjectId("5af02d84bf3bef3588597703"), "name" : "meng" }
{ "_id" : ObjectId("5af02d90bf3bef3588597704"), "name" : "ming" }
{ "_id" : ObjectId("5af02d90bf3bef3588597705"), "name" : "ying" }
b.将之前恢复的CSV格式数据导入:
[root@nova-02 ~]# cat /opt/c_csvi.dat
id,name
,meng
,ming
,sheng
[root@nova-02 ~]# mongoimport -h 10.0.0.212:27017 --authenticationDatabase admin -d meng -c c --type=csv --headerline --file /opt/c_csvi.dat
2018-05-07T19:17:21.658+0800 connected to: 10.0.0.212:27017
2018-05-07T19:17:21.659+0800 imported 3 documents
登录查看:
> db.c.find()
{ "_id" : ObjectId("5af02d84bf3bef3588597703"), "name" : "meng" }
{ "_id" : ObjectId("5af02d90bf3bef3588597704"), "name" : "ming" }
{ "_id" : ObjectId("5af02d90bf3bef3588597705"), "name" : "ying" }
{ "_id" : ObjectId("5af035c14ec0fcd21dd21437"), "id" : "", "name" : "meng" }
{ "_id" : ObjectId("5af035c14ec0fcd21dd21438"), "id" : "", "name" : "ming" }
{ "_id" : ObjectId("5af035c14ec0fcd21dd21439"), "id" : "", "name" : "sheng" }
该命令的参数如下:
-h:指明数据库宿主机的IP
-u:指明数据库的用户名
-p:指明数据库的密码
-d:指明数据库的名字
-c:指明collection的名字
-f:指明要导出那些列
-o:指明到要导出的文件名
-q:指明导出数据的过滤条件
--drop:插入之前先删除原有的
--headerline:指明第一行是列名,不需要导入。
-j:同时运行的插入操作数(默认为1),并行
--authenticationDatabase:验证数据的名称
(3)mongodump备份工具
a.全库备份
[root@nova-02 ~]# mongodump -h 10.0.0.212:27017 -uroot -proot --authenticationDatabase admin -o /opt/full
[root@nova-02 ~]# ll /opt/full/
total 16
drwxr-xr-x. 2 root root 4096 May 7 19:26 admin
drwxr-xr-x. 2 root root 4096 May 7 19:26 meng
drwxr-xr-x. 2 root root 4096 May 7 19:26 ming
drwxr-xr-x. 2 root root 4096 May 7 19:26 test
b.备份meng库
[root@nova-02 ~]# mongodump -h 10.0.0.212:27017 -uroot -proot --authenticationDatabase admin -d meng -o /opt/backup/
[root@nova-02 ~]# ll /opt/backup/meng/
total 16
-rw-r--r--. 1 root root 0 May 7 19:28 a.bson
-rw-r--r--. 1 root root 78 May 7 19:28 a.metadata.json
-rw-r--r--. 1 root root 0 May 7 19:28 b.bson
-rw-r--r--. 1 root root 78 May 7 19:28 b.metadata.json
-rw-r--r--. 1 root root 250 May 7 19:28 c.bson
-rw-r--r--. 1 root root 78 May 7 19:28 c.metadata.json
c.备份meng库下的c集合
[root@nova-02 ~]# mongodump -h 10.0.0.212:27017 -uroot -proot --authenticationDatabase admin -d meng -c c -o /opt/backup/
2018-05-07T19:30:15.549+0800 writing meng.c to
2018-05-07T19:30:15.550+0800 done dumping meng.c (6 documents)
[root@nova-02 ~]# ll /opt/backup/meng/c.
c.bson c.metadata.json
d.压缩备份库
[root@nova-02 ~]# mongodump -h 10.0.0.212:27017 -uroot -proot --authenticationDatabase admin -d meng -o /opt/backup/ --gzip
[root@nova-02 ~]# ll /opt/backup/meng/
total 40
-rw-r--r--. 1 root root 0 May 7 19:28 a.bson
-rw-r--r--. 1 root root 23 May 7 19:31 a.bson.gz
-rw-r--r--. 1 root root 78 May 7 19:28 a.metadata.json
-rw-r--r--. 1 root root 93 May 7 19:31 a.metadata.json.gz
-rw-r--r--. 1 root root 0 May 7 19:28 b.bson
-rw-r--r--. 1 root root 23 May 7 19:31 b.bson.gz
-rw-r--r--. 1 root root 78 May 7 19:28 b.metadata.json
-rw-r--r--. 1 root root 93 May 7 19:31 b.metadata.json.gz
-rw-r--r--. 1 root root 250 May 7 19:30 c.bson
-rw-r--r--. 1 root root 121 May 7 19:31 c.bson.gz
-rw-r--r--. 1 root root 78 May 7 19:30 c.metadata.json
-rw-r--r--. 1 root root 93 May 7 19:31 c.metadata.json.gz
e.压缩备份c单表
[root@nova-02 ~]# mongodump -h 10.0.0.212:27017 -uroot -proot --authenticationDatabase admin -d meng -c c -o /opt/backup/ --gzip
[root@nova-02 ~]# ll /opt/backup/meng/c.*
-rw-r--r--. 1 root root 250 May 7 19:30 /opt/backup/meng/c.bson
-rw-r--r--. 1 root root 121 May 7 19:33 /opt/backup/meng/c.bson.gz
-rw-r--r--. 1 root root 78 May 7 19:30 /opt/backup/meng/c.metadata.json
-rw-r--r--. 1 root root 93 May 7 19:33 /opt/backup/meng/c.metadata.json.gz
mongodump的参数与mongoexport的参数基本一致
参数说明:
-h:指明数据库宿主机的IP
-u:指明数据库的用户名
-p:指明数据库的密码
-d:指明数据库的名字
-c:指明collection的名字
-o:指明到要导出的文件名
-q:指明导出数据的过滤条件
--authenticationDatabase:验证数据的名称
--gzip:备份时压缩
--oplog:use oplog for taking a point-in-time snapshot
(4)mongorestore恢复实践
a.全库备份中恢复单库(基于之前的全库备份)
[root@nova-02 ~]# mongorestore -h 10.0.0.212:27017 -uroot -proot --authenticationDatabase admin -d meng --drop /opt/full/meng/
> show dbs;
b.恢复meng库
[root@nova-02 ~]# mongorestore -h 10.0.0.212:27017 -uroot -proot --authenticationDatabase admin -d meng --drop /opt/backup/meng/
c.恢复meng库下的c集合
[root@nova-02 ~]# mongorestore -h 10.0.0.212:27017 -uroot -proot --authenticationDatabase admin -d meng -c c --drop /opt/backup/meng/c.bson
> db.c.find()
mongorestore与mongoimport参数类似
参数说明:
-h:指明数据库宿主机的IP
-u:指明数据库的用户名
-p:指明数据库的密码
-d:指明数据库的名字
-c:指明collection的名字
-o:指明到要导出的文件名
-q:指明导出数据的过滤条件
--authenticationDatabase:验证数据的名称
--gzip:备份时压缩
--oplog:use oplog for taking a point-in-time snapshot
--drop:恢复的时候把之前的集合drop掉
(5) mongoexport/mongoimport与mongodump/mongorestore的对比
mongoexport/mongoimport导入/导出的是JSON格式,而mongodump/mongorestore导入/导出的是BSON格式。
JSON可读性强但体积较大,BSON则是二进制文件,体积小但对人类几乎没有可读性。
在一些mongodb版本之间,BSON格式可能会随版本不同而有所不同,所以不同版本之间用mongodump/mongorestore可能不会成功,具体要看版本之间的兼容性。当无法使用BSON进行跨版本的数据迁移的时候,使用JSON格式即mongoexport/mongoimport是一个可选项。跨版本的mongodump/mongorestore并不推荐,实在要做请先检查文档看两个版本是否兼容(大部分时候是的)。
JSON虽然具有较好的跨版本通用性,但其只保留了数据部分,不保留索引,账户等其他基础信息。使用时应该注意。
说明:mongodb不支持一次导出多个collection的数据,只能通过脚本实现。
例如批量导出meng数据库的a和c集合。
数据准备:
> db.a.find()
{ "_id" : ObjectId("5af03e26bf3bef3588597705"), "name" : "a01" }
{ "_id" : ObjectId("5af03e2bbf3bef3588597706"), "name" : "a02" }
{ "_id" : ObjectId("5af03e2ebf3bef3588597707"), "name" : "a03" }
>
> db.c.find()
{ "_id" : ObjectId("5af02d90bf3bef3588597704"), "name" : "ming" }
{ "_id" : ObjectId("5af02d90bf3bef3588597705"), "name" : "ying" }
{ "_id" : ObjectId("5af035c14ec0fcd21dd21437"), "id" : "", "name" : "meng" }
{ "_id" : ObjectId("5af035c14ec0fcd21dd21438"), "id" : "", "name" : "ming" }
{ "_id" : ObjectId("5af035c14ec0fcd21dd21439"), "id" : "", "name" : "sheng" }
{ "_id" : ObjectId("5af02d84bf3bef3588597703"), "name" : "meng" }
编写脚本如下:
[root@nova-02 opt]# cat mongo-col.sh
#!/bin/bash
db=meng
collection_list="a c"
host=10.0.0.212
port=27017
out_prefix=/opt/many-col/
for collection in $collection_list; do
echo $collection
out_dir="${out_prefix}/${db}_${collection}/"
mkdir -p ${out_dir}
mongodump --host $host --port $port --collection $collection --db $db --out ${out_dir}
done
执行后查看结果:
[root@nova-02 opt]# sh mongo-col.sh
a
2018-05-07T20:45:34.504+0800 writing meng.a to
2018-05-07T20:45:34.505+0800 done dumping meng.a (3 documents)
c
2018-05-07T20:45:34.515+0800 writing meng.c to
2018-05-07T20:45:34.516+0800 done dumping meng.c (6 documents)
[root@nova-02 opt]# ll /opt/many-col/
total 8
drwxr-xr-x. 3 root root 4096 May 7 20:45 meng_a
drwxr-xr-x. 3 root root 4096 May 7 20:45 meng_c
[root@nova-02 many-col]# ll meng_a/meng/
total 8
-rw-r--r--. 1 root root 108 May 7 20:45 a.bson
-rw-r--r--. 1 root root 78 May 7 20:45 a.metadata.json
[root@nova-02 many-col]# ll meng_c/meng/
total 8
-rw-r--r--. 1 root root 250 May 7 20:45 c.bson
-rw-r--r--. 1 root root 78 May 7 20:45 c.metadata.json
浙公网安备 33010602011771号