MongoDB数据恢复mongorestore命令

本文将介绍怎样使用 mongorestore 命令实现 mongodb 数据恢复。

在使用 mongorestore 命令恢复数据之前,我们可以去了解一下 mongodump 数据备份命令。本文将使用上篇文章备份的数据,详细数据恢复如下:

mongorestore命令介绍

mongodb 使用 mongorestore 命令来恢复备份的数据。语法如下:

mongorestore -h <hostname><:port> -d dbname <path>

参数说明:

  • --host <:port>, -h <:port>:MongoDB所在服务器地址,默认为:localhost:27017。例如:192.168.238.201:27017

  • --db , -d :需要恢复的数据库实例,例如:test,当然这个名称也可以和备份时候的不一样。

  • --drop:恢复的时候,先删除当前数据,然后恢复备份的数据。注意:会导致你备份后新添加到mongodb的数据丢失,慎用!

  • <path>:mongorestore 最后的一个参数,设置备份数据所在位置,例如:c:datadump est。

  • --dir:指定备份的目录。注意:你不能同时指定 <path> 和 --dir 选项。


实例

我们将 D:dump est 目录下面的备份数据恢复到本地 mongodb 的 test 数据中。如下:

(1)先登录到本地 mongodb,看看 test 数据库中有哪些集合。然后看看集合文档:

> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
test    0.000GB

# 使用 test 数据
> use test
switched to db test

# 查看有哪些集合
> show tables
users

# 查看集合数据
> db.users.find()
{ "_id" : ObjectId("5e5d002352b5f0e6ef3933a6"), "name" : "Tom", "age" : 28 }
{ "_id" : ObjectId("5e5d0dddc6153eba074011a8"), "name" : "Bill", "age" : 22 }

(2)执行 mongorestore 命令,恢复数据。如下: 

D:mongodb-v4.2.3in>mongorestore -d test d:dump	est
2020-03-02T21:45:26.602+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
2020-03-02T21:45:26.642+0800    building a list of collections to restore from d:dump	est dir
2020-03-02T21:45:26.644+0800    reading metadata for test.test from d:dump	est	est.metadata.json
2020-03-02T21:45:26.657+0800    restoring test.test from d:dump	est	est.bson
2020-03-02T21:45:26.659+0800    no indexes to restore
2020-03-02T21:45:26.659+0800    finished restoring test.test (1 document, 0 failures)
2020-03-02T21:45:26.659+0800    1 document(s) restored successfully. 0 document(s) failed to restore.

(3)再次查看 mongodb 中的数据情况。如下:

> show tables
test
users
> db.test.find()
{ "_id" : ObjectId("5e5b884159de83945fb5d3c6"), "name" : "test", "version" : "1.1.1" }
> db.users.find()
{ "_id" : ObjectId("5e5d002352b5f0e6ef3933a6"), "name" : "Tom", "age" : 28 }
{ "_id" : ObjectId("5e5d0dddc6153eba074011a8"), "name" : "Bill", "age" : 22 }

从上面中我们可以看到多出了一个 test 集合,该集合中仅有一条数据。

如果上面已经恢复数据成功了,我们再次执行数据恢复操作。会出现什么现象呢?

D:mongodb-v4.2.3in>mongorestore -h 127.0.0.1:27017 -d test d:dump	est
2020-03-02T21:51:36.653+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
2020-03-02T21:51:36.699+0800    building a list of collections to restore from d:dump	est dir
2020-03-02T21:51:36.700+0800    restoring to existing collection test.test without dropping
2020-03-02T21:51:36.701+0800    reading metadata for test.test from d:dump	est	est.metadata.json
2020-03-02T21:51:36.701+0800    restoring test.test from d:dump	est	est.bson
2020-03-02T21:51:36.703+0800    continuing through error: E11000 duplicate key error collection: test.test index: _id_ dup key: { _id: ObjectId('5e5b884159de83945fb5d3c6') }
2020-03-02T21:51:36.703+0800    no indexes to restore
2020-03-02T21:51:36.703+0800    finished restoring test.test (0 documents, 1 failure)
2020-03-02T21:51:36.704+0800    0 document(s) restored successfully. 1 document(s) failed to restore.

看见没有,出现了 duplicate key 错误,key 重复了。

如果你在恢复备份的命令中添加了 --drop 参数,效果会是怎样呢?

(1)向 test 集合写入测试数据,用于查看恢复后对 test 集合的影响,如下:

> db.test.insert({name:"test"})
WriteResult({ "nInserted" : 1 })
> db.test.find();
{ "_id" : ObjectId("5e5b884159de83945fb5d3c6"), "name" : "test", "version" : "1.1.1" }
{ "_id" : ObjectId("5e5d1069c6153eba074011a9"), "name" : "test" }

(2)执行恢复语句,注意:我们备份时,test数据库只有 test 集合,且该集合只有一条数据,数据为:{"name" : "test", "version" : "1.1.1" }

D:mongodb-v4.2.3in>mongorestore -h 127.0.0.1:27017 --drop -d test d:dump	est
2020-03-02T21:53:53.272+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
2020-03-02T21:53:53.319+0800    building a list of collections to restore from d:dump	est dir
2020-03-02T21:53:53.327+0800    reading metadata for test.test from d:dump	est	est.metadata.json
2020-03-02T21:53:53.339+0800    restoring test.test from d:dump	est	est.bson
2020-03-02T21:53:53.343+0800    no indexes to restore
2020-03-02T21:53:53.343+0800    finished restoring test.test (1 document, 0 failures)
2020-03-02T21:53:53.344+0800    1 document(s) restored successfully. 0 document(s) failed to restore.

(3)查看恢复后数据库中数据情况

> show tables
test
users
> db.test.find()
{ "_id" : ObjectId("5e5b884159de83945fb5d3c6"), "name" : "test", "version" : "1.1.1" }

仔细对比(1)和(3)的数据,你会发现你丢失了刚刚插入的 name="test" 数据。并且,users 集合并没有被删除。这个说明 mongorestore 使用 --drop 参数只会将备份中存在的集合删除掉。


注意:--drop 选项一定要谨慎使用,可能导致不可挽回的错误。

睡眠和休息丧失了时间,却取得了明天工作的精力。 —— 毛泽东
0 不喜欢
说说我的看法 -
全部评论(
没有评论
关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号