Mongodb自帶了mongodump和mongorestore這兩個工具來實現對數據的備份和恢復。mongodb
mongodump可以在Mongodb運行時進行備份,它的工做原理是對運行的Mongodb作查詢,而後將全部查到的文檔寫入磁盤。可是存在的問題時使用mongodump產生的備份不必定是數據庫的實時快照,若是咱們在備份時對數據庫進行了寫入操做,則備份出來的文件可能不徹底和Mongodb實時數據相等。另外在備份時可能會對其它客戶端性能產生不利的影響。數據庫
mongodump用法以下:json
Shell代碼 app
- [root@localhost mongodb]# ./bin/mongodump --help
- Export MongoDB data to BSON files.
-
- options:
- --help produce help message
- -v [ --verbose ] be more verbose (include multiple times for more
- verbosity e.g. -vvvvv)
- --version print the program's version and exit
- -h [ --host ] arg mongo host to connect to ( <set name>/s1,s2 for
- sets)
- --port arg server port. Can also use --host hostname:port
- --ipv6 enable IPv6 support (disabled by default)
- -u [ --username ] arg username
- -p [ --password ] arg password
- --dbpath arg directly access mongod database files in the given
- path, instead of connecting to a mongod server -
- needs to lock the data directory, so cannot be used
- if a mongod is currently accessing the same path
- --directoryperdb if dbpath specified, each db is in a separate
- directory
- --journal enable journaling
- -d [ --db ] arg database to use
- -c [ --collection ] arg collection to use (some commands)
- -o [ --out ] arg (=dump) output directory or "-" for stdout
- -q [ --query ] arg json query
- --oplog Use oplog for point-in-time snapshotting
- --repair try to recover a crashed database
- --forceTableScan force a table scan (do not use $snapshot)
參數說明:工具
-h:指明數據庫宿主機的IPpost
-u:指明數據庫的用戶名性能
-p:指明數據庫的密碼spa
-d:指明數據庫的名字.net
-c:指明collection的名字rest
-o:指明到要導出的文件名
-q:指明導出數據的過濾條件
具體使用示例以下:
Shell代碼
- [root@localhost mongodb]# ./bin/mongodump -d test -o data/backup
- connected to: 127.0.0.1
- DATABASE: test to data/backup/test
- test.system.indexes to data/backup/test/system.indexes.bson
- 9 objects
- test.users to data/backup/test/users.bson
- 3 objects
- test.games to data/backup/test/games.bson
- 1 objects
- test.blog.post to data/backup/test/blog.post.bson
- 1 objects
- test.lists to data/backup/test/lists.bson
- 1 objects
- test.math to data/backup/test/math.bson
- 1 objects
- test.map to data/backup/test/map.bson
- 8 objects
- test.my_collection to data/backup/test/my_collection.bson
- 0 objects
- test.foo to data/backup/test/foo.bson
- 6 objects
- test.system.users to data/backup/test/system.users.bson
- 1 objects
mongorestore是Mongodb從備份中恢復數據的工具,它主要用來獲取mongodump的輸出結果,並將備份的數據插入到運行的Mongodb中。
mongorestore命令使用方法以下:
Shell代碼
- [root@localhost mongodb]# ./bin/mongorestore --help
- usage: ./bin/mongorestore [options] [directory or filename to restore from]
- options:
- --help produce help message
- -v [ --verbose ] be more verbose (include multiple times for more
- verbosity e.g. -vvvvv)
- --version print the program's version and exit
- -h [ --host ] arg mongo host to connect to ( <set name>/s1,s2 for sets)
- --port arg server port. Can also use --host hostname:port
- --ipv6 enable IPv6 support (disabled by default)
- -u [ --username ] arg username
- -p [ --password ] arg password
- --dbpath arg directly access mongod database files in the given
- path, instead of connecting to a mongod server -
- needs to lock the data directory, so cannot be used
- if a mongod is currently accessing the same path
- --directoryperdb if dbpath specified, each db is in a separate
- directory
- --journal enable journaling
- -d [ --db ] arg database to use
- -c [ --collection ] arg collection to use (some commands)
- --objcheck validate object before inserting
- --filter arg filter to apply before inserting
- --drop drop each collection before import
- --oplogReplay replay oplog for point-in-time restore
- --keepIndexVersion don't upgrade indexes to newest version
參數說明:
-h:指明數據庫宿主機的IP
-u:指明數據庫的用戶名
-p:指明數據庫的密碼
-d:指明數據庫的名字
-c:指明collection的名字
-o:指明到要備份的文件名
-q:指明備份數據的過濾條件
具體使用示例以下:
Shell代碼
- [root@localhost mongodb]# ./bin/mongorestore -d test --drop data/backup/test/
- connected to: 127.0.0.1
- Tue Aug 14 01:18:17 data/backup/test/games.bson
- Tue Aug 14 01:18:17 going into namespace [test.games]
- Tue Aug 14 01:18:17 dropping
- 1 objects found
- Tue Aug 14 01:18:17 data/backup/test/foo.bson
- Tue Aug 14 01:18:17 going into namespace [test.foo]
- Tue Aug 14 01:18:17 dropping
- 6 objects found
- Tue Aug 14 01:18:17 data/backup/test/blog.post.bson
- Tue Aug 14 01:18:17 going into namespace [test.blog.post]
- Tue Aug 14 01:18:17 dropping
- 1 objects found
- Tue Aug 14 01:18:17 data/backup/test/lists.bson
- Tue Aug 14 01:18:17 going into namespace [test.lists]
- Tue Aug 14 01:18:17 dropping
- 1 objects found
- Tue Aug 14 01:18:17 data/backup/test/map.bson
- Tue Aug 14 01:18:17 going into namespace [test.map]
- Tue Aug 14 01:18:17 dropping
- 8 objects found
- Tue Aug 14 01:18:17 data/backup/test/math.bson
- Tue Aug 14 01:18:17 going into namespace [test.math]
- Tue Aug 14 01:18:17 dropping
- 1 objects found
- Tue Aug 14 01:18:17 data/backup/test/system.users.bson
- Tue Aug 14 01:18:17 going into namespace [test.system.users]
- 1 objects found
- Tue Aug 14 01:18:17 data/backup/test/my_collection.bson
- Tue Aug 14 01:18:17 going into namespace [test.my_collection]
- Tue Aug 14 01:18:17 dropping
- Tue Aug 14 01:18:17 file data/backup/test/my_collection.bson empty, skipping
- Tue Aug 14 01:18:17 data/backup/test/users.bson
- Tue Aug 14 01:18:17 going into namespace [test.users]
- Tue Aug 14 01:18:17 dropping
- 3 objects found
- Tue Aug 14 01:18:17 data/backup/test/system.indexes.bson
- Tue Aug 14 01:18:17 going into namespace [test.system.indexes]
- Tue Aug 14 01:18:17 dropping
- Tue Aug 14 01:18:17 { key: { _id: 1 }, ns: "test.users", name: "_id_" }
- Tue Aug 14 01:18:17 { key: { _id: 1 }, ns: "test.games", name: "_id_" }
- Tue Aug 14 01:18:17 { key: { _id: 1 }, ns: "test.blog.post", name: "_id_" }
- Tue Aug 14 01:18:17 { key: { _id: 1 }, ns: "test.lists", name: "_id_" }
- Tue Aug 14 01:18:17 { key: { _id: 1 }, ns: "test.math", name: "_id_" }
- Tue Aug 14 01:18:17 { key: { _id: 1 }, ns: "test.map", name: "_id_" }
- Tue Aug 14 01:18:17 { key: { gps: "2d" }, ns: "test.map", name: "gps_", min: -180.0, max: 181.0 }
- Tue Aug 14 01:18:17 { key: { _id: 1 }, ns: "test.foo", name: "_id_" }
- Tue Aug 14 01:18:17 { key: { _id: 1 }, ns: "test.system.users", name: "_id_" }
- 9 objects found