1.mongodump 備份 用法:bin/mongodump [options] 舉例:將本機6666端口的數據庫備份到本機的/data/6666目錄下 bin/mongodump --port 6666 --directoryperdb -o /data/6666 connected to: 127.0.0.1:6666 all dbs DATABASE: test to /data/6666/test test.system.indexes to /data/6666/test/system.indexes.bson 1 objects test.ints to /data/6666/test/ints.bson 4 objects DATABASE: admin to /data/6666/admin
2.mongorestore 恢復 用法:bin/mongorestore [options] [directory or filename to restore from] 舉例:將數據庫test刪除,然后從備份目錄恢復 > show dbs admin (empty) local 4.201171875GB test 0.203125GB > use test switched to db test > db.dropDatabase() { "dropped" : "test", "ok" : 1 } > show dbs admin (empty) local 4.201171875GB 使用mongorestore恢復 bin/mongorestore -host 127.0.0.1:6666 --directoryperdb /data/6666/ connected to: 127.0.0.1:6666 Wed Jul 6 16:37:33 /data/6666/test/ints.bson Wed Jul 6 16:37:33 going into namespace [test.ints] Wed Jul 6 16:37:33 4 objects found Wed Jul 6 16:37:33 /data/6666/test/system.indexes.bson Wed Jul 6 16:37:33 going into namespace [test.system.indexes] Wed Jul 6 16:37:33 { name: "_id_", ns: "test.ints", key: { _id: 1 }, v: 0 } Wed Jul 6 16:37:33 1 objects found 查看數據庫是否恢復 > show dbs admin (empty) local 4.201171875GB test 0.203125GB mongorestore有個參數--drop,使用此參數,會先將集合里的數據都刪除了再恢復 就是說,如果你備份完數據后有新增數據,那么使用--drop,新增的數據會不見 不使用此參數,恢復備份的時候新增的數據依然存在