Mongodb从0到1系列八: 备份与恢复

Mongodb从0到1系列一:下载、安装、启动、停止

Mongodb从0到1系列二:数据库与集合操作文档、增删改查

Mongodb从0到1系列三: 条件查询、大小写

Mongodb从0到1系列四: Limit与Skip方法、排序、索引

Mongodb从0到1系列五: 主从复制

Mongodb从0到1系列六: 复制集

Mongodb从0到1系列七: 分片


13. 备份与恢复


13.1 备份

Mongodb中使用mongodump命令来备份MongoDB数据,常用语法如下:
mongodump -h <:port> -d -o

-h MongDB所在服务器地址
-d:需要备份的数据库实例,例如:test1
-o:备份的数据存放位置,需要提前创建好

假设有以下数据库test1:
> use test1
> db.student.insert({name:'Zhangsan',age:22,course:'Chinese'})
> db.student.insert({name:'Lisi',age:23,course:'computer'})
> db.student.insert({name:'Wangwu',age:24,course:'Chinese'})
> db.student.insert({name:'Zhaoliu',age:25,course:'Chinese'})
> db.student.insert({name:'Liuneng',age:26,course:'English'})


> db.teacher.insert({name:'Wang',class:10,course:'math'})
> db.teacher.insert({name:'Li',age:36,location:'Beijing',course:'English'})

备份命令如下:
db2a:~ # mkdir /data/backup
db2a:~ # mongodump -h db2a:27017 -d test1 -o /data/backup/test1
2017-07-23T14:43:35.316+0800    writing test1.student to 
2017-07-23T14:43:35.316+0800    writing test1.teacher to 
2017-07-23T14:43:35.319+0800    done dumping test1.student (5 documents)
2017-07-23T14:43:35.319+0800    done dumping test1.teacher (2 documents)

db2a:~ # ls /data/backup/test1/test1
student.bson  student.metadata.json  teacher.bson  teacher.metadata.json

13.2 恢复

mongodb使用 mongorestore 命令来恢复备份的数据。语法如下
mongorestore -h <:port> -d

恢复之前,我们先将所有的记录都删掉,然后插入一条新的记录,之后看一下恢复之后的状态:
> use test1
> db.student.remove({})
> db.student.insert({name:"Newname"})

db2a:~ # mongorestore -h db2a:27017 -d test1 /data/backup/test1/test1
2017-07-23T14:49:31.271+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
2017-07-23T14:49:31.272+0800    building a list of collections to restore from /data/backup/test1/test1 dir
2017-07-23T14:49:31.276+0800    reading metadata for test1.student from /data/backup/test1/test1/student.metadata.json
2017-07-23T14:49:31.277+0800    restoring test1.student from /data/backup/test1/test1/student.bson
2017-07-23T14:49:31.278+0800    reading metadata for test1.teacher from /data/backup/test1/test1/teacher.metadata.json
2017-07-23T14:49:31.284+0800    restoring test1.teacher from /data/backup/test1/test1/teacher.bson
2017-07-23T14:49:31.289+0800    no indexes to restore
2017-07-23T14:49:31.289+0800    finished restoring test1.student (5 documents)
2017-07-23T14:49:31.291+0800    error: multiple errors in bulk operation:
  - E11000 duplicate key error collection: test1.teacher index: _id_ dup key: { : ObjectId('5974453479dfde64877bf984') }
  - E11000 duplicate key error collection: test1.teacher index: _id_ dup key: { : ObjectId('5974453879dfde64877bf985') }

2017-07-23T14:49:31.292+0800    no indexes to restore
2017-07-23T14:49:31.292+0800    finished restoring test1.teacher (2 documents)
2017-07-23T14:49:31.292+0800    done

恢复时之所以有"duplicate key error",是因为teacher集合中的文档并没有被删除,和备份中的是一样的。
完成恢复之后,我们再看一下数据的内容: 

> use test1
switched to db test1
> db.student.find()
{ "_id" : ObjectId("5974462d5c9c97773721c5f5"), "name" : "Newname" }
{ "_id" : ObjectId("5974449d79dfde64877bf97e"), "name" : "Zhangsan", "age" : 22, "course" : "Chinese" }
{ "_id" : ObjectId("5974449d79dfde64877bf97f"), "name" : "Lisi", "age" : 23, "course" : "computer" }
{ "_id" : ObjectId("5974449d79dfde64877bf980"), "name" : "Wangwu", "age" : 24, "course" : "Chinese" }
{ "_id" : ObjectId("5974449d79dfde64877bf981"), "name" : "Zhaoliu", "age" : 25, "course" : "Chinese" }
{ "_id" : ObjectId("5974449d79dfde64877bf982"), "name" : "Liuneng", "age" : 26, "course" : "English" }
> db.teacher.find()
{ "_id" : ObjectId("5974453479dfde64877bf984"), "name" : "Wang", "class" : 10, "course" : "math" }
{ "_id" : ObjectId("5974453879dfde64877bf985"), "name" : "Li", "age" : 36, "location" : "Beijing", "course" : "English" }

这里可以看到,mongorestore并没有将原来的数据库删掉。所以,在备份之后新添加的文档在恢复之后仍然存在。如果想单纯地恢复到备份时的状态,可以在mongorestore时加上--drop选项,表示恢复前将数据库删除:

db2a:~ # mongorestore -h db2a:27017 -d test1 /data/backup/test1/test1 --drop
2017-07-23T14:56:48.773+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
2017-07-23T14:56:48.774+0800    building a list of collections to restore from /data/backup/test1/test1 dir
2017-07-23T14:56:48.778+0800    reading metadata for test1.student from /data/backup/test1/test1/student.metadata.json
2017-07-23T14:56:48.779+0800    reading metadata for test1.teacher from /data/backup/test1/test1/teacher.metadata.json
2017-07-23T14:56:48.804+0800    restoring test1.student from /data/backup/test1/test1/student.bson
2017-07-23T14:56:48.835+0800    no indexes to restore
2017-07-23T14:56:48.835+0800    finished restoring test1.student (5 documents)
2017-07-23T14:56:48.841+0800    restoring test1.teacher from /data/backup/test1/test1/teacher.bson
2017-07-23T14:56:48.846+0800    no indexes to restore
2017-07-23T14:56:48.846+0800    finished restoring test1.teacher (2 documents)
2017-07-23T14:56:48.846+0800    done

db2a:~ # mongo
> use test1
switched to db test1
> db.student.find()
{ "_id" : ObjectId("5974449d79dfde64877bf97e"), "name" : "Zhangsan", "age" : 22, "course" : "Chinese" }
{ "_id" : ObjectId("5974449d79dfde64877bf97f"), "name" : "Lisi", "age" : 23, "course" : "computer" }
{ "_id" : ObjectId("5974449d79dfde64877bf980"), "name" : "Wangwu", "age" : 24, "course" : "Chinese" }
{ "_id" : ObjectId("5974449d79dfde64877bf981"), "name" : "Zhaoliu", "age" : 25, "course" : "Chinese" }
{ "_id" : ObjectId("5974449d79dfde64877bf982"), "name" : "Liuneng", "age" : 26, "course" : "English" }
> db.teacher.find()
{ "_id" : ObjectId("5974453479dfde64877bf984"), "name" : "Wang", "class" : 10, "course" : "math" }
{ "_id" : ObjectId("5974453879dfde64877bf985"), "name" : "Li", "age" : 36, "location" : "Beijing", "course" : "English" }

你可能感兴趣的:(mongodb)