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 <hostname><:port> -d <database> -o <path>

-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 <hostname><:port> -d <database> <path>

恢复之前,我们先将所有的记录都删掉,然后插入一条新的记录,之后看一下恢复之后的状态:
> 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" }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值