首页 > 代码库 > mongodb备份与恢复

mongodb备份与恢复


mongodb中逻辑备份工具mongodump和mongorestore工具可以对当前mongodb数据库进行备份、恢复操作。

当前数据库结构:

MongoDB Enterprise > show dbs
admin   0.000GB
dbking  0.000GB
local   0.000GB
MongoDB Enterprise > use dbking
switched to db dbking
MongoDB Enterprise > show collections
col
test

--使用mongodump工具备份dbking数据库数据:

[root@slave2 dump]# /usr/local/mongodb341/bin/mongodump -h 127.0.0.1 -d dbking -o /mongodb/dump/
2016-12-29T13:13:17.436+0800    writing dbking.col to
2016-12-29T13:13:17.436+0800    writing dbking.test to
2016-12-29T13:13:17.437+0800    done dumping dbking.col (11 documents)
2016-12-29T13:13:17.437+0800    done dumping dbking.test (2 documents)
[root@slave2 dump]# ll
total 4
drwxr-xr-x 2 root root 4096 Dec 29 13:13 dbking
[root@slave2 dump]# ll dbking/
total 16
-rw-r--r-- 1 root root 782 Dec 29 13:13 col.bson
-rw-r--r-- 1 root root 494 Dec 29 13:13 col.metadata.json
-rw-r--r-- 1 root root 125 Dec 29 13:13 test.bson
-rw-r--r-- 1 root root  83 Dec 29 13:13 test.metadata.json

--使用mongorestore工具恢复备份的dbking数据库到dbk库中:

MongoDB Enterprise > show dbs
admin   0.000GB
dbking  0.000GB
local   0.000GB

[root@slave2 dump]# /usr/local/mongodb341/bin/mongorestore -h 127.0.0.1 -d dbk /mongodb/dump/dbking/
2016-12-29T13:14:55.720+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
2016-12-29T13:14:55.721+0800    building a list of collections to restore from /mongodb/dump/dbking dir
2016-12-29T13:14:55.721+0800    reading metadata for dbk.col from /mongodb/dump/dbking/col.metadata.json
2016-12-29T13:14:55.722+0800    reading metadata for dbk.test from /mongodb/dump/dbking/test.metadata.json
2016-12-29T13:14:55.748+0800    restoring dbk.test from /mongodb/dump/dbking/test.bson
2016-12-29T13:14:55.773+0800    restoring dbk.col from /mongodb/dump/dbking/col.bson
2016-12-29T13:14:55.774+0800    no indexes to restore
2016-12-29T13:14:55.774+0800    finished restoring dbk.test (2 documents)
2016-12-29T13:14:55.774+0800    restoring indexes for collection dbk.col from metadata
2016-12-29T13:14:55.809+0800    finished restoring dbk.col (11 documents)
2016-12-29T13:14:55.809+0800    done

MongoDB Enterprise > show dbs
admin   0.000GB
dbk     0.000GB
dbking  0.000GB
local   0.000GB
MongoDB Enterprise > use dbk
switched to db dbk
MongoDB Enterprise > show collections
col
test
MongoDB Enterprise > db.col.find()
{ "_id" : ObjectId("586358cde423fe7088062ab2"), "name" : "dbk", "age" : "28", "job" : "java工程师" }
{ "_id" : ObjectId("586358d7e423fe7088062ab3"), "name" : "dbk", "age" : 30, "job" : "java工程师" }
{ "_id" : ObjectId("586358dbe423fe7088062ab4"), "name" : "dbk", "age" : 31, "job" : "java工程师" }
{ "_id" : ObjectId("58635ec5e423fe7088062ab5"), "name" : "dbk", "age" : "28", "job" : "java工程师" }
{ "_id" : ObjectId("58635ec5e423fe7088062ab6"), "name" : "dbk", "age" : "28", "job" : "java工程师" }
{ "_id" : ObjectId("58635ec5e423fe7088062ab7"), "name" : "dbk", "age" : "28", "job" : "java工程师" }
{ "_id" : ObjectId("58635ec5e423fe7088062ab8"), "name" : "nope", "age" : "28", "job" : "会计" }
{ "_id" : ObjectId("58635ec5e423fe7088062ab9"), "name" : "king", "age" : "35", "job" : "CTO" }
{ "_id" : ObjectId("58635ec5e423fe7088062aba"), "name" : "chavinking", "age" : "35", "job" : "CTO" }
{ "_id" : ObjectId("58635ec5e423fe7088062abb"), "name" : "舒永康", "age" : "35", "job" : "java高级工程师" }
{ "_id" : ObjectId("58635ec6e423fe7088062abc"), "name" : "张芳", "age" : "30", "job" : "集成工程师" }
MongoDB Enterprise > db.test.find()
{ "_id" : ObjectId("58649abfe2e07cc67bf2ac59"), "name" : "chavin", "age" : "28", "job" : "DBA" }
{ "_id" : ObjectId("58649ac5e2e07cc67bf2ac5a"), "name" : "dbk", "age" : "28", "job" : "DBA" }

mongodb备份与恢复