首页 > 代码库 > MongoDB整理笔记のDump & Restore

MongoDB整理笔记のDump & Restore

   备份

技术分享
[root@localhost bin]# ./mongodump -d my_mongodb connected to: 127.0.0.1  DATABASE: my_mongodb     to     dump/my_mongodb          my_mongodb.system.indexes to dump/my_mongodb/system.indexes.bson                  1 objects          my_mongodb.user to dump/my_mongodb/user.bson                  2 objects [root@localhost bin]# ll 总计 67648  -rwxr-xr-x 1 root root 7508756 2011-04-06 bsondump drwxr-xr-x 3 root root    4096 04-10 23:54 dump -rwxr-xr-x 1 root root 2978016 2011-04-06 mongo 
View Code

   此时,会在当前目录下创建一个dump目录,用于存放备份出来的文件,当然也可以指定备份存放的目录。

技术分享
root@localhost bin]# ./mongodump -d my_mongodb -o my_mongodb_dump connected to: 127.0.0.1  DATABASE: my_mongodb     to     my_mongodb_dump/my_mongodb         my_mongodb.system.indexes  to  my_mongodb_dump/my_mongodb/system.indexes.bson                  1 objects          my_mongodb.user to my_mongodb_dump/my_mongodb/user.bson           2 objects [root@localhost bin]# 
View Code

   这个例子中将备份文件存在了当前目录下的my_mongodb_dump目录下

   恢复

   由于刚刚已经做了备份,所以我们先讲库my_mongodb删除掉  

技术分享
> use my_mongodb  switched to db my_mongodb > db.dropDatabase() { "dropped" : "my_mongodb", "ok" : 1 } > show dbs  admin   (empty) local   (empty) test    (empty) >   
View Code

   接下来,我们进行数据恢复

技术分享
[root@localhost bin]# ./mongorestore -d my_mongodb my_mongodb_dump/connected to: 127.0.0.1  Wed Apr 11 00:03:03 my_mongodb_dump/my_mongodb/user.bson Wed Apr 11 00:03:03      going into namespace [my_mongodb.user] Wed Apr 11 00:03:03      2 objects found  Wed Apr 11 00:03:03 my_mongodb_dump/my_mongodb/system.indexes.bson Wed Apr 11 00:03:03      going into namespace [my_mongodb.system.indexes] Wed Apr 11 00:03:03 { name: "_id_", ns: "my_mongodb.user", key: { _id: 1 }, v: 0 } Wed Apr 11 00:03:03      1 objects found [root@localhost bin]#
View Code

   经验证数据库又回来了,其实要想恢复数据库,大可不必先删除my_mongodb库,只要说明-drop参数,就可以在恢复的时候先删除表然后再向表中插入数据。

MongoDB整理笔记のDump & Restore