MongoDB提供了兩個(gè)命令用來備份(mongodump)和恢復(fù)(mongorestore)數(shù)據(jù),數(shù)據(jù)dump出來的格式是BSON的
1.mongodump 備份
用法:bin/mongodump [options]
舉例:將本機(jī)6666端口的數(shù)據(jù)庫備份到本機(jī)的/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 恢復(fù)
用法:bin/mongorestore [options] [directory or filename to restore from]
舉例:將數(shù)據(jù)庫test刪除,然后從備份目錄恢復(fù)
> 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恢復(fù)
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
查看數(shù)據(jù)庫是否恢復(fù)
> show dbs
admin (empty)
local 4.201171875GB
test 0.203125GB
mongorestore有個(gè)參數(shù)--drop,使用此參數(shù),會(huì)先將集合里的數(shù)據(jù)都刪除了再恢復(fù)
就是說,如果你備份完數(shù)據(jù)后有新增數(shù)據(jù),那么使用--drop,新增的數(shù)據(jù)會(huì)不見
不使用此參數(shù),恢復(fù)備份的時(shí)候新增的數(shù)據(jù)依然存在
本文名稱:mongodb備份和恢復(fù)
本文路徑:http://www.rwnh.cn/article4/ipcgie.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、網(wǎng)站維護(hù)、外貿(mào)建站、虛擬主機(jī)、ChatGPT、網(wǎng)站導(dǎo)航
廣告
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源:
創(chuàng)新互聯(lián)