mongodb的備份與恢復

openkk 12年前發布 | 31K 次閱讀 MongoDB NoSQL數據庫

mongodb提供了兩個命令來備份(mongodump )和恢復(mongorestore )數據庫。

1.備份(mongodump )

用法 :

[root@web3 3]# mongodump --help
options:
--help                   produce help message
-v [ --verbose ]         be more verbose (include multiple times for more 
verbosity e.g. -vvvvv)
-h [ --host ] arg        mongo host to connect to ("left,right" for pairs)
-d [ --db ] arg          database to use
-c [ --collection ] arg  collection to use (some commands)
-u [ --username ] arg    username
-p [ --password ] arg    password
--dbpath arg             directly access mongod data files in the given path,
instead of connecting to a mongod instance - 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
-o [ --out ] arg (=dump) output directory


例子:

[root@web3 ~]# mongodump -h 192.168.1.103 -d citys -o /backup/mongobak/3
connected to: 192.168.1.103
DATABASE: citys  to     /backup/mongobak/3/citys
citys.building to /backup/mongobak/3/citys/building.bson
13650 objects
citys.system.indexes to /backup/mongobak/3/citys/system.indexes.bson
1 objects


備份出來的數據是二進制的,已經經過壓縮。比實際數據庫要小很多,我的數據庫顯示占用了260多M,備份后只有2M。

2.恢復(mongorestore )

用法:

[root@web3 3]# mongorestore --help
usage: 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)
-h [ --host ] arg       mongo host to connect to ("left,right" for pairs)
-d [ --db ] arg         database to use
-c [ --collection ] arg collection to use (some commands)
-u [ --username ] arg   username
-p [ --password ] arg   password
--dbpath arg            directly access mongod data files in the given path, 
instead of connecting to a mongod instance - 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
--drop                  drop each collection before import
--objcheck              validate object before inserting

--drop參數可以在導入之前把collection先刪掉。</pre>


例子:

[root@web3 3]# mongorestore -h 127.0.0.1 --directoryperdb /backup/mongobak/3/
connected to: 127.0.0.1 /backup/mongobak/3/citys/building.bson going into namespace [citys.building] 13667 objects /backup/mongobak/3/citys/system.indexes.bson going into namespace [citys.system.indexes] 1 objects</pre>


</span></span>

另外mongodb還提供了mongoexport 和 mongoimport 這兩個命令來導出或導入數據,導出的數據是json格式的。也可以實現備份和恢復的功能。

例:

mongoexport -d mixi_top_city_prod -c building_45 -q '{ "uid" : "10832545" }' > mongo_10832545.bson

mongoimport -d mixi_top_city -c building_45 --file mongo_10832545.bson</pre>

 本文由用戶 openkk 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!