注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

PostgreSQL 中文网

 
 
 

日志

 
 

MongoDB:恢复数据( mongorestore )  

2012-11-19 15:10:09|  分类: MongoDB |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

 

      上篇学习了使用 mongodump 以二进制备份数据库,接着学习使用对应的 mongorestore
恢复数据库,以下是实验过程:


--1 备份数据库

 [mongo@redhatB tf]$ mongodump -h 127.0.0.1 -d skytf -o skytf.dmp
connected to: 127.0.0.1
Sat Nov 17 17:37:33 DATABASE: skytf      to     skytf.dmp/skytf
Sat Nov 17 17:37:33     skytf.test_1 to skytf.dmp/skytf/test_1.bson
Sat Nov 17 17:37:33 doing snapshot query
Sat Nov 17 17:37:33              1 objects
Sat Nov 17 17:37:33     Metadata for skytf.test_1 to skytf.dmp/skytf/test_1.metadata.json
Sat Nov 17 17:37:33     skytf.things to skytf.dmp/skytf/things.bson
Sat Nov 17 17:37:33 doing snapshot query
Sat Nov 17 17:37:33              30 objects
Sat Nov 17 17:37:33     Metadata for skytf.things to skytf.dmp/skytf/things.metadata.json
Sat Nov 17 17:37:33     skytf.things_1 to skytf.dmp/skytf/things_1.bson
Sat Nov 17 17:37:33 doing snapshot query
Sat Nov 17 17:37:33              30 objects
Sat Nov 17 17:37:33     Metadata for skytf.things_1 to skytf.dmp/skytf/things_1.metadata.json
Sat Nov 17 17:37:33     skytf.test_2 to skytf.dmp/skytf/test_2.bson
Sat Nov 17 17:37:33 doing snapshot query
Sat Nov 17 17:37:33              4 objects
Sat Nov 17 17:37:33     Metadata for skytf.test_2 to skytf.dmp/skytf/test_2.metadata.json

     备注:上述命令备份数据库 skytf,产生的备份文件为目录 skytf.dmp/skytf。
  

--2 删除数据库

 [mongo@redhatB ~]$ mongo
MongoDB shell version: 2.2.1
connecting to: test
> show dbs;
local   (empty)
skytf   0.0625GB
test    0.0625GB

> use skytf;
switched to db skytf

> show collections;
system.indexes
test_1
test_2
things
things_1

> db.dropDatabase();
{ "dropped" : "skytf", "ok" : 1 }

> show collections;

> show dbs;
local   (empty)
skytf   (empty)
test    0.0625GB

       备注:为了测试需要,先删除数据库 skytf。


--3 还原数据库

 [mongo@redhatB tf]$ mongorestore -h 127.0.0.1 -d skytf skytf.dmp/skytf
connected to: 127.0.0.1
Sat Nov 17 17:42:03 skytf.dmp/skytf/test_1.bson
Sat Nov 17 17:42:03     going into namespace [skytf.test_1]
1 objects found
Sat Nov 17 17:42:03     Creating index: { key: { _id: 1 }, ns: "skytf.test_1", name: "_id_" }
Sat Nov 17 17:42:04 skytf.dmp/skytf/test_2.bson
Sat Nov 17 17:42:04     going into namespace [skytf.test_2]
4 objects found
Sat Nov 17 17:42:04     Creating index: { key: { _id: 1 }, ns: "skytf.test_2", name: "_id_" }
Sat Nov 17 17:42:04 skytf.dmp/skytf/things_1.bson
Sat Nov 17 17:42:04     going into namespace [skytf.things_1]
30 objects found
Sat Nov 17 17:42:04     Creating index: { key: { _id: 1 }, ns: "skytf.things_1", name: "_id_" }
Sat Nov 17 17:42:04 skytf.dmp/skytf/things.bson
Sat Nov 17 17:42:04     going into namespace [skytf.things]
30 objects found
Sat Nov 17 17:42:04     Creating index: { key: { _id: 1 }, ns: "skytf.things", name: "_id_" }

     备注:使用 mongorestore 恢复数据库,从上面看到数据库 skytf 已恢复的细节。


--4 再次查看 skytf 库

 [mongo@redhatB ~]$ mongo
MongoDB shell version: 2.2.1
connecting to: test

> show dbs;
local   (empty)
skytf   0.0625GB
test    0.0625GB

> use skytf;
switched to db skytf

> show collections;
system.indexes
test_1
test_2
things
things_1

   备注:从上看出,数据库 skytf 已恢复;还原指定数据库,数据文件需要指定到备份文件下面的
           子目录,如果不指定子目录,会报以下错误。

--5 ERROR

 [mongo@redhatB tf]$ mongorestore -h 127.0.0.1 -d skytf skytf.dmp/
connected to: 127.0.0.1
Sat Nov 17 17:41:15 ERROR: ERROR: root directory must be a dump of a single database
Sat Nov 17 17:41:15 ERROR:        when specifying a db name with --db
   


--6 在恢复前先删除数据库中的对像

 [mongo@redhatB tf]$ mongorestore -h 127.0.0.1 -d skytf --drop skytf.dmp/skytf
connected to: 127.0.0.1
Sat Nov 17 17:45:37 skytf.dmp/skytf/test_1.bson
Sat Nov 17 17:45:37     going into namespace [skytf.test_1]
Sat Nov 17 17:45:37      dropping
1 objects found
Sat Nov 17 17:45:37     Creating index: { key: { _id: 1 }, ns: "skytf.test_1", name: "_id_" }
Sat Nov 17 17:45:37 skytf.dmp/skytf/test_2.bson
Sat Nov 17 17:45:37     going into namespace [skytf.test_2]
Sat Nov 17 17:45:37      dropping
4 objects found
Sat Nov 17 17:45:37     Creating index: { key: { _id: 1 }, ns: "skytf.test_2", name: "_id_" }
Sat Nov 17 17:45:37 skytf.dmp/skytf/things_1.bson
Sat Nov 17 17:45:37     going into namespace [skytf.things_1]
Sat Nov 17 17:45:37      dropping
30 objects found
Sat Nov 17 17:45:37     Creating index: { key: { _id: 1 }, ns: "skytf.things_1", name: "_id_" }
Sat Nov 17 17:45:37 skytf.dmp/skytf/things.bson
Sat Nov 17 17:45:37     going into namespace [skytf.things]
Sat Nov 17 17:45:37      dropping
30 objects found
Sat Nov 17 17:45:37     Creating index: { key: { _id: 1 }, ns: "skytf.things", name: "_id_" }

      备注:使用 --drop 参数可以在恢复前先删除已存在的对像。
  

--7 参考
http://docs.mongodb.org/manual/reference/mongorestore/

  评论这张
 
阅读(26238)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2016