通过mongodump和mongorestore实现Mongodb备份和恢复

Mongodb自带了mongodump和mongorestore这两个工具来实现对数据的备份和恢复。

mongodump能够在Mongodb运行时进行备份,它的工作原理是对运行的Mongodb做查询,然后将所有查到的文档写入磁盘。但是存在的 问题时使用mongodump产生的备份不一定是数据库的实时快照,如果我们在备份时对数据库进行了写入操作,则备份出来的文件可能不完全和 Mongodb实时数据相等。另外在备份时可能会对其它客户端性能产生不利的影响。

mongodump用法如下:

Shell代码   收藏代码
  1. [root@localhost mongodb]# ./bin/mongodump --help  
  2. Export MongoDB data to BSON files.  
  3.   
  4. options:  
  5.   --help                   produce help message  
  6.   -v [ --verbose ]         be more verbose (include multiple times for more   
  7.                            verbosity e.g. -vvvvv)  
  8.   --version                print the program's version and exit  
  9.   -h [ --host ] arg        mongo host to connect to ( <set name>/s1,s2 for   
  10.                            sets)  
  11.   --port arg               server port. Can also use --host hostname:port  
  12.   --ipv6                   enable IPv6 support (disabled by default)  
  13.   -u [ --username ] arg    username  
  14.   -p [ --password ] arg    password  
  15.   --dbpath arg             directly access mongod database files in the given   
  16.                            path, instead of connecting to a mongod  server -   
  17.                            needs to lock the data directory, so cannot be used   
  18.                            if a mongod is currently accessing the same path  
  19.   --directoryperdb         if dbpath specified, each db is in a separate   
  20.                            directory  
  21.   --journal                enable journaling  
  22.   -d [ --db ] arg          database to use  
  23.   -c [ --collection ] arg  collection to use (some commands)  
  24.   -o [ --out ] arg (=dump) output directory or "-" for stdout  
  25.   -q [ --query ] arg       json query  
  26.   --oplog                  Use oplog for point-in-time snapshotting  
  27.   --repair                 try to recover a crashed database  
  28.   --forceTableScan         force a table scan (do not use $snapshot)  

参数说明:

-h:指明数据库宿主机的IP

-u:指明数据库的用户名

-p:指明数据库的密码

-d:指明数据库的名字

-c:指明collection的名字

-o:指明到要导出的文件名

-q:指明导出数据的过滤条件

具体使用示例如下:

Shell代码   收藏代码
  1. [root@localhost mongodb]# ./bin/mongodump -d test -o data/backup  
  2. connected to: 127.0.0.1  
  3. DATABASE: test   to     data/backup/test  
  4.     test.system.indexes to data/backup/test/system.indexes.bson  
  5.          9 objects  
  6.     test.users to data/backup/test/users.bson  
  7.          3 objects  
  8.     test.games to data/backup/test/games.bson  
  9.          1 objects  
  10.     test.blog.post to data/backup/test/blog.post.bson  
  11.          1 objects  
  12.     test.lists to data/backup/test/lists.bson  
  13.          1 objects  
  14.     test.math to data/backup/test/math.bson  
  15.          1 objects  
  16.     test.map to data/backup/test/map.bson  
  17.          8 objects  
  18.     test.my_collection to data/backup/test/my_collection.bson  
  19.          0 objects  
  20.     test.foo to data/backup/test/foo.bson  
  21.          6 objects  
  22.     test.system.users to data/backup/test/system.users.bson  
  23.          1 objects  

mongorestore是Mongodb从备份中恢复数据的工具,它主要用来获取mongodump的输出结果,并将备份的数据插入到运行的Mongodb中。

mongorestore命令使用方法如下:

Shell代码   收藏代码
  1. [root@localhost mongodb]# ./bin/mongorestore --help  
  2. usage: ./bin/mongorestore [options] [directory or filename to restore from]  
  3. options:  
  4.   --help                  produce help message  
  5.   -v [ --verbose ]        be more verbose (include multiple times for more   
  6.                           verbosity e.g. -vvvvv)  
  7.   --version               print the program's version and exit  
  8.   -h [ --host ] arg       mongo host to connect to ( <set name>/s1,s2 for sets)  
  9.   --port arg              server port. Can also use --host hostname:port  
  10.   --ipv6                  enable IPv6 support (disabled by default)  
  11.   -u [ --username ] arg   username  
  12.   -p [ --password ] arg   password  
  13.   --dbpath arg            directly access mongod database files in the given   
  14.                           path, instead of connecting to a mongod  server -   
  15.                           needs to lock the data directory, so cannot be used   
  16.                           if a mongod is currently accessing the same path  
  17.   --directoryperdb        if dbpath specified, each db is in a separate   
  18.                           directory  
  19.   --journal               enable journaling  
  20.   -d [ --db ] arg         database to use  
  21.   -c [ --collection ] arg collection to use (some commands)  
  22.   --objcheck              validate object before inserting  
  23.   --filter arg            filter to apply before inserting  
  24.   --drop                  drop each collection before import  
  25.   --oplogReplay           replay oplog for point-in-time restore  
  26.   --keepIndexVersion      don't upgrade indexes to newest version  

参数说明:

-h:指明数据库宿主机的IP

-u:指明数据库的用户名

-p:指明数据库的密码

-d:指明数据库的名字

-c:指明collection的名字

-o:指明到要备份的文件名

-q:指明备份数据的过滤条件

具体使用示例如下:

Shell代码   收藏代码
  1. [root@localhost mongodb]# ./bin/mongorestore -d test --drop data/backup/test/  
  2. connected to: 127.0.0.1  
  3. Tue Aug 14 01:18:17 data/backup/test/games.bson  
  4. Tue Aug 14 01:18:17      going into namespace [test.games]  
  5. Tue Aug 14 01:18:17      dropping  
  6. 1 objects found  
  7. Tue Aug 14 01:18:17 data/backup/test/foo.bson  
  8. Tue Aug 14 01:18:17      going into namespace [test.foo]  
  9. Tue Aug 14 01:18:17      dropping  
  10. 6 objects found  
  11. Tue Aug 14 01:18:17 data/backup/test/blog.post.bson  
  12. Tue Aug 14 01:18:17      going into namespace [test.blog.post]  
  13. Tue Aug 14 01:18:17      dropping  
  14. 1 objects found  
  15. Tue Aug 14 01:18:17 data/backup/test/lists.bson  
  16. Tue Aug 14 01:18:17      going into namespace [test.lists]  
  17. Tue Aug 14 01:18:17      dropping  
  18. 1 objects found  
  19. Tue Aug 14 01:18:17 data/backup/test/map.bson  
  20. Tue Aug 14 01:18:17      going into namespace [test.map]  
  21. Tue Aug 14 01:18:17      dropping  
  22. 8 objects found  
  23. Tue Aug 14 01:18:17 data/backup/test/math.bson  
  24. Tue Aug 14 01:18:17      going into namespace [test.math]  
  25. Tue Aug 14 01:18:17      dropping  
  26. 1 objects found  
  27. Tue Aug 14 01:18:17 data/backup/test/system.users.bson  
  28. Tue Aug 14 01:18:17      going into namespace [test.system.users]  
  29. 1 objects found  
  30. Tue Aug 14 01:18:17 data/backup/test/my_collection.bson  
  31. Tue Aug 14 01:18:17      going into namespace [test.my_collection]  
  32. Tue Aug 14 01:18:17      dropping  
  33. Tue Aug 14 01:18:17 file data/backup/test/my_collection.bson empty, skipping  
  34. Tue Aug 14 01:18:17 data/backup/test/users.bson  
  35. Tue Aug 14 01:18:17      going into namespace [test.users]  
  36. Tue Aug 14 01:18:17      dropping  
  37. 3 objects found  
  38. Tue Aug 14 01:18:17 data/backup/test/system.indexes.bson  
  39. Tue Aug 14 01:18:17      going into namespace [test.system.indexes]  
  40. Tue Aug 14 01:18:17      dropping  
  41. Tue Aug 14 01:18:17 { key: { _id: 1 }, ns: "test.users", name: "_id_" }  
  42. Tue Aug 14 01:18:17 { key: { _id: 1 }, ns: "test.games", name: "_id_" }  
  43. Tue Aug 14 01:18:17 { key: { _id: 1 }, ns: "test.blog.post", name: "_id_" }  
  44. Tue Aug 14 01:18:17 { key: { _id: 1 }, ns: "test.lists", name: "_id_" }  
  45. Tue Aug 14 01:18:17 { key: { _id: 1 }, ns: "test.math", name: "_id_" }  
  46. Tue Aug 14 01:18:17 { key: { _id: 1 }, ns: "test.map", name: "_id_" }  
  47. Tue Aug 14 01:18:17 { key: { gps: "2d" }, ns: "test.map", name: "gps_", min: -180.0, max: 181.0 }  
  48. Tue Aug 14 01:18:17 { key: { _id: 1 }, ns: "test.foo", name: "_id_" }  
  49. Tue Aug 14 01:18:17 { key: { _id: 1 }, ns: "test.system.users", name: "_id_" }  
  50. 9 objects found  

猜你喜欢

转载自zzc1684.iteye.com/blog/2224697