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" }