前面两篇文章介绍了mongodb的搭建与使用:http://msiyuetian.blog.51cto.com/8637744/1720559

以及mongodb的副本集部署:http://msiyuetian.blog.51cto.com/8637744/1722406

下面我们来介绍mongodb的备份和恢复


一、MongoDB备份


1、准备工作

1)创建库、集合

> use mydb                                                             //创建库

switched to db mydb

> db.createCollection('test1')                                   //创建集合test1

{ "ok" : 1 }

> show dbs                                                             //查看所有库

local  0.078GB

mydb   0.078GB

> show tables                                                          //查看当前库中的集合

system.indexes

test1

> db.test1.insert({id:1,name:"tpp",age:20})                //向test1集合中插入数据

WriteResult({ "nInserted" : 1 })

> db.test1.insert({id:2,name:"linux",age:15})

WriteResult({ "nInserted" : 1 })

> db.test1.find()                                                      //查看test1集合中的数据

{ "_id" : ObjectId("566de4441b2778d299e8f3a5"), "id" : 1, "name" : "tpp", "age" : 20 }

{ "_id" : ObjectId("566de45a1b2778d299e8f3a6"), "id" : 2, "name" : "linux", "age" : 15 }

同理我们再创建一个mydb2库,这里省略这个过程。


2、备份指定库

语法:mongodump -h ip -d dbname -o dir

参数说明

-h:后跟服务器ip

-u:后跟数据库用户

-p:后跟用户密码

-d:后跟 database 名字,不加则备份所有库

-o:后指备份到哪里,它是个目录

示例

[root@centos ~]# mongodump -d mydb -o /tmp/mongodbbackup

2015-12-14T05:42:06.867+0800    writing mydb.test1 to /tmp/mongodbbackup/mydb/test1.bson

2015-12-14T05:42:06.867+0800    writing mydb.system.indexes to /tmp/mongodbbackup/mydb/system.indexes.bson

2015-12-14T05:42:06.869+0800    writing mydb.test1 metadata to /tmp/mongodbbackup/mydb/test1.metadata.json

2015-12-14T05:42:06.871+0800    done dumping mydb.test1 (2 documents)

[root@centos ~]# ls /tmp/mongodbbackup/

mydb

[root@centos ~]# ls /tmp/mongodbbackup/mydb/

system.indexes.bson  test1.bson  test1.metadata.json

注意:test1.bson是二进制文件,是数据库文件;test1.metadata.json是集合文件


2、备份所有库

语法:mongodump -h ip -o dir

示例

[root@centos ~]# mongodump -o /tmp/mongodbbackup_2

2015-12-14T05:57:48.456+0800    writing mydb.system.indexes to /tmp/mongodbbackup_2/mydb/system.indexes.bson

2015-12-14T05:57:48.456+0800    writing mydb.test1 to /tmp/mongodbbackup_2/mydb/test1.bson

2015-12-14T05:57:48.457+0800    writing mydb2.system.indexes to /tmp/mongodbbackup_2/mydb2/system.indexes.bson

2015-12-14T05:57:48.465+0800    writing mydb2.test2 to /tmp/mongodbbackup_2/mydb2/test2.bson

2015-12-14T05:57:48.467+0800    writing mydb.test1 metadata to /tmp/mongodbbackup_2/mydb/test1.metadata.json

2015-12-14T05:57:48.469+0800    done dumping mydb.test1 (2 documents)

2015-12-14T05:57:48.473+0800    writing mydb2.test2 metadata to /tmp/mongodbbackup_2/mydb2/test2.metadata.json

2015-12-14T05:57:48.475+0800    done dumping mydb2.test2 (0 documents)

[root@centos ~]# ls /tmp/mongodbbackup_2/

mydb  mydb2

[root@centos ~]# ls /tmp/mongodbbackup_2/mydb2/

system.indexes.bson  test2.bson  test2.metadata.json


3、备份指定集合

语法:mongodump -h ip -d dbname -c collectionname -o dir

示例

[root@centos ~]# mongodump -d mydb -c test1 -o /tmp/mongodbbackup_3

2015-12-14T06:08:25.404+0800    writing mydb.test1 to /tmp/mongodbbackup_3/mydb/test1.bson

2015-12-14T06:08:25.405+0800    writing mydb.test1 metadata to /tmp/mongodbbackup_3/mydb/test1.metadata.json

2015-12-14T06:08:25.408+0800    done dumping mydb.test1 (2 documents)

[root@centos ~]# ls /tmp/mongodbbackup_3/

mydb

[root@centos ~]# ls /tmp/mongodbbackup_3/mydb/

test1.bson  test1.metadata.json

[root@centos ~]# cat /tmp/mongodbbackup_3/mydb/test1.metadata.json

{"options":{},"indexes":[{"v":1,"key":{"_id":1},"name":"_id_","ns":"mydb.test1"}]}


4、导出集合为json文件

[root@centos ~]# mongoexport -d mydb -c test1 -o /tmp/mongodbbackup_4/test1.json

2015-12-14T06:14:53.735+0800    connected to: localhost

2015-12-14T06:14:53.736+0800    exported 2 records

[root@centos ~]# cat /tmp/mongodbbackup_4/test1.json

{"_id":{"$oid":"566de4441b2778d299e8f3a5"},"id":1.0,"name":"tpp","age":20.0}

{"_id":{"$oid":"566de45a1b2778d299e8f3a6"},"id":2.0,"name":"linux","age":15.0}


二、MongoDB恢复


恢复之前我们要先删除之前建立的两个库:

> show dbs

local  0.078GB

mydb   0.078GB

mydb2  0.078GB

> use mydb

switched to db mydb

> db.dropDatabase()

{ "dropped" : "mydb", "ok" : 1 }

> use mydb2

switched to db mydb2

> db.dropDatabase()

{ "dropped" : "mydb2", "ok" : 1 }

> show dbs

local  0.078GB


1、恢复指定库

语法:mongorestore -d dbname dir/

说明:-d 跟要恢复的库名字;dir 就是该库备份时所在的目录

[root@centos ~]# ls /tmp/mongodbbackup_2/mydb/

system.indexes.bson  test1.bson           test1.metadata.json

[root@centos ~]# mongorestore -d mydb /tmp/mongodbbackup_2/mydb/

2015-12-14T07:22:12.821+0800    building a list of collections to restore from /tmp/mongodbbackup_2/mydb/ dir

2015-12-14T07:22:12.825+0800    reading metadata file from /tmp/mongodbbackup_2/mydb/test1.metadata.json

2015-12-14T07:22:12.826+0800    restoring mydb.test1 from file /tmp/mongodbbackup_2/mydb/test1.bson

2015-12-14T07:22:13.163+0800    restoring indexes for collection mydb.test1 from metadata

2015-12-14T07:22:13.163+0800    finished restoring mydb.test1 (2 documents)

2015-12-14T07:22:13.163+0800    done


2、恢复所有库

语法:mongorestore --drop dir/

说明:dir 是备份所在库的目录名字;--drop 可选,意思是当恢复之前先把现在数据库的数据删除,不建议使用(不过我们可以再次备份现在数据库中的数据库,然后使用--drop参数)。

[root@centos ~]# mongorestore /tmp/mongodbbackup_2/

2015-12-14T07:25:17.126+0800    building a list of dbs and collections to restore from /tmp/mongodbbackup_2/ dir

2015-12-14T07:25:17.131+0800    reading metadata file from /tmp/mongodbbackup_2/mydb/test1.metadata.json

2015-12-14T07:25:17.131+0800    restoring mydb.test1 from file /tmp/mongodbbackup_2/mydb/test1.bson

2015-12-14T07:25:17.133+0800    reading metadata file from /tmp/mongodbbackup_2/mydb2/test2.metadata.json

2015-12-14T07:25:17.133+0800    restoring mydb2.test2 from file /tmp/mongodbbackup_2/mydb2/test2.bson

2015-12-14T07:25:17.133+0800    restoring indexes for collection mydb2.test2 from metadata

2015-12-14T07:25:17.193+0800    error: E11000 duplicate key error index: mydb.test1.$_id_ dup key: { : ObjectId('566de4441b2778d299e8f3a5') }

2015-12-14T07:25:17.193+0800    restoring indexes for collection mydb.test1 from metadata

2015-12-14T07:25:17.193+0800    finished restoring mydb.test1 (2 documents)

2015-12-14T07:25:17.502+0800    finished restoring mydb2.test2 (0 documents)

2015-12-14T07:25:17.502+0800    done

注意:上面有个错误,是因为刚我们已经恢复了mydb库,要想不报错,加上--drop参数即可,如下:

[root@centos ~]# mongorestore --drop /tmp/mongodbbackup_2/

2015-12-14T07:25:35.373+0800    building a list of dbs and collections to restore from /tmp/mongodbbackup_2/ dir

2015-12-14T07:25:35.376+0800    reading metadata file from /tmp/mongodbbackup_2/mydb/test1.metadata.json

2015-12-14T07:25:35.377+0800    restoring mydb.test1 from file /tmp/mongodbbackup_2/mydb/test1.bson

2015-12-14T07:25:35.630+0800    restoring indexes for collection mydb.test1 from metadata

2015-12-14T07:25:35.631+0800    reading metadata file from /tmp/mongodbbackup_2/mydb2/test2.metadata.json

2015-12-14T07:25:35.631+0800    restoring mydb2.test2 from file /tmp/mongodbbackup_2/mydb2/test2.bson

2015-12-14T07:25:35.631+0800    finished restoring mydb.test1 (2 documents)

2015-12-14T07:25:35.889+0800    restoring indexes for collection mydb2.test2 from metadata

2015-12-14T07:25:35.890+0800    finished restoring mydb2.test2 (0 documents)

2015-12-14T07:25:35.890+0800    done


3、恢复集合

语法:mongorestore -d dbname -c collectionname dir/dbname/collectionname.bson

说明:-c跟要恢复的集合名字;dir是备份dbname库时生成文件所在路径,这里是一个bson文件的路径

[root@centos ~]# mongorestore --drop -d mydb /tmp/mongodbbackup_2/mydb/test1.bson

2015-12-14T07:30:29.023+0800    checking for collection data in /tmp/mongodbbackup_2/mydb/test1.bson

2015-12-14T07:30:29.028+0800    reading metadata file from /tmp/mongodbbackup_2/mydb/test1.metadata.json

2015-12-14T07:30:29.028+0800    restoring mydb.test1 from file /tmp/mongodbbackup_2/mydb/test1.bson

2015-12-14T07:30:29.093+0800    restoring indexes for collection mydb.test1 from metadata

2015-12-14T07:30:29.093+0800    finished restoring mydb.test1 (2 documents)

2015-12-14T07:30:29.093+0800    done


4、导入集合

语法:mongoimport -d dbname -c collectionname --file /tmp/collectionname.json

[root@centos ~]# mongoimport --drop -d mydb -c test1 --file /tmp/mongodbbackup_4/test1.json

2015-12-14T07:33:52.671+0800    connected to: localhost

2015-12-14T07:33:52.672+0800    dropping: mydb.test1

2015-12-14T07:33:52.677+0800    imported 2 documents