MongoDB之備份與恢復(fù)-創(chuàng)新互聯(lián)

Mong

創(chuàng)新互聯(lián)公司服務(wù)項(xiàng)目包括零陵網(wǎng)站建設(shè)、零陵網(wǎng)站制作、零陵網(wǎng)頁制作以及零陵網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,零陵網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到零陵省份的部分城市,未來相信會繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!oDB數(shù)據(jù)備份
在Mongodb中我們使用mongodump命令來備份MongoDB數(shù)據(jù)。該命令可以導(dǎo)出所有數(shù)據(jù)到指定目錄中。
mongodump命令可以通過參數(shù)指定導(dǎo)出的數(shù)據(jù)量級轉(zhuǎn)存的服務(wù)器。
語法
mongodump命令腳本語法如下:
>mongodump -h dbhost -d dbname -o dbdirectory
  ● -h:
MongDB所在服務(wù)器地址,例如:127.0.0.1,當(dāng)然也可以指定端口號:127.0.0.1:27017
  ● -d:
需要備份的數(shù)據(jù)庫實(shí)例,例如:test
  ● -o:
備份的數(shù)據(jù)存放位置,例如:c:\data\dump,當(dāng)然該目錄需要提前建立,在備份完成后,系統(tǒng)自動在dump目錄下建立一個test目錄,這個目錄里面存放該數(shù)據(jù)庫實(shí)例的備份數(shù)據(jù)。
備份整個數(shù)據(jù)庫
[root@D2-LZY245 ~]# mongodump
2017-08-18T11:06:39.771+0800    writing admin.system.users to
2017-08-18T11:06:39.772+0800    done dumping admin.system.users (3 documents)
2017-08-18T11:06:39.772+0800    writing admin.system.version to
2017-08-18T11:06:39.773+0800    done dumping admin.system.version (2 documents)
2017-08-18T11:06:39.773+0800    writing hr.emps to
2017-08-18T11:06:39.773+0800    writing hr.emps_infos to
2017-08-18T11:06:39.773+0800    writing hr.inventory to
2017-08-18T11:06:39.773+0800    writing hr.emp to
2017-08-18T11:06:39.774+0800    done dumping hr.emps_infos (11 documents)
2017-08-18T11:06:39.774+0800    writing hr.shop to
2017-08-18T11:06:39.774+0800    done dumping hr.emps (11 documents)
2017-08-18T11:06:39.774+0800    writing hr.t_emps_job to
2017-08-18T11:06:39.775+0800    done dumping hr.emp (9 documents)
2017-08-18T11:06:39.775+0800    writing hr.dept to
2017-08-18T11:06:39.775+0800    done dumping hr.shop (7 documents)
2017-08-18T11:06:39.775+0800    writing hr.news to
2017-08-18T11:06:39.775+0800    done dumping hr.t_emps_job (6 documents)
2017-08-18T11:06:39.775+0800    writing hr.depts to
2017-08-18T11:06:39.775+0800    done dumping hr.news (4 documents)
2017-08-18T11:06:39.775+0800    writing hr.loc to
2017-08-18T11:06:39.776+0800    done dumping hr.dept (5 documents)
2017-08-18T11:06:39.776+0800    writing hr.t_emps_sex to
2017-08-18T11:06:39.776+0800    done dumping hr.depts (2 documents)
2017-08-18T11:06:39.776+0800    writing hr.info to
2017-08-18T11:06:39.776+0800    done dumping hr.loc (2 documents)
2017-08-18T11:06:39.776+0800    writing hr.phones to
2017-08-18T11:06:39.776+0800    done dumping hr.info (0 documents)
2017-08-18T11:06:39.776+0800    writing test.fs.chunks to
2017-08-18T11:06:39.777+0800    done dumping hr.t_emps_sex (2 documents)
2017-08-18T11:06:39.777+0800    writing test.fs.files to
2017-08-18T11:06:39.777+0800    done dumping hr.phones (0 documents)
2017-08-18T11:06:39.777+0800    done dumping test.fs.chunks (0 documents)
2017-08-18T11:06:39.777+0800    done dumping hr.inventory (10 documents)
2017-08-18T11:06:39.777+0800    done dumping test.fs.files (0 documents)
備份結(jié)果
[root@D2-LZY245 ~]# ll dump/
total 4
drwxrwxr-x 2 root root  124 Aug 18 11:06 admin
drwxrwxr-x 2 root root 4096 Aug 18 11:06 hr
drwxrwxr-x 2 root root  106 Aug 18 11:06 test
刪除集合
> use hr
switched to db hr
> db.dept.find();
{ "_id" : ObjectId("599504e70184ff511bf02be1"), "deptno" : 11, "dname" : "財(cái)務(wù)部", "loc" : "北京" }
{ "_id" : ObjectId("599504e70184ff511bf02be2"), "deptno" : 12, "dname" : "財(cái)務(wù)部", "loc" : "北京" }
{ "_id" : ObjectId("599504e70184ff511bf02be3"), "deptno" : 13, "dname" : "財(cái)務(wù)部", "loc" : "北京" }
{ "_id" : ObjectId("599504e80184ff511bf02be4"), "deptno" : 14, "dname" : "財(cái)務(wù)部", "loc" : "北京" }
{ "_id" : ObjectId("599505240184ff511bf02be5"), "deptno" : 16, "dname" : "財(cái)務(wù)部", "loc" : "北京" }
> db.dept.drop();
true
> db.dept.find();
>
MongoDB數(shù)據(jù)恢復(fù)
mongodb使用 mongorestore 命令來恢復(fù)備份的數(shù)據(jù)。
語法
mongorestore命令腳本語法如下:
>mongorestore -h <hostname><:port> -d dbname <path>
  ● --host <:port>, -h <:port>:
MongoDB所在服務(wù)器地址,默認(rèn)為: localhost:27017
  ● --db , -d :
需要恢復(fù)的數(shù)據(jù)庫實(shí)例,例如:test,當(dāng)然這個名稱也可以和備份時候的不一樣,比如test2
  ● --drop:
恢復(fù)的時候,先刪除當(dāng)前數(shù)據(jù),然后恢復(fù)備份的數(shù)據(jù)。就是說,恢復(fù)后,備份后添加修改的數(shù)據(jù)都會被刪除,慎用哦!
  ● <path>:
mongorestore 最后的一個參數(shù),設(shè)置備份數(shù)據(jù)所在位置,例如:c:\data\dump\test。
你不能同時指定 <path> 和 --dir 選項(xiàng),--dir也可以設(shè)置備份目錄。
  ● --dir:
指定備份的目錄
恢復(fù)被刪除的集合
[root@D2-LZY245 ~]# mongorestore --db=hr --collection=dept --dir=/root/dump/hr/dept.bson
2017-08-18T11:14:54.397+0800    checking for collection data in /root/dump/hr/dept.bson
2017-08-18T11:14:54.406+0800    reading metadata for hr.dept from /root/dump/hr/dept.metadata.json
2017-08-18T11:14:54.424+0800    restoring hr.dept from /root/dump/hr/dept.bson
2017-08-18T11:14:54.486+0800    no indexes to restore
2017-08-18T11:14:54.486+0800    finished restoring hr.dept (5 documents)
2017-08-18T11:14:54.486+0800    done
刪除數(shù)據(jù)庫
> db.dropDatabase()
{ "dropped" : "hr", "ok" : 1 }
> use hr
switched to db hr
> show collections
>
恢復(fù)被刪除的數(shù)據(jù)庫
[root@D2-LZY245 ~]# mongorestore --db=hr --dir=/root/dump/hr
2017-08-18T11:18:38.570+0800    the --db and --collection args should only be used when restoring from a BSON file. Other uses are deprecated and will not exist in the future; use --nsInclude instead
2017-08-18T11:18:38.570+0800    building a list of collections to restore from /root/dump/hr dir
2017-08-18T11:18:38.571+0800    reading metadata for hr.emps from /root/dump/hr/emps.metadata.json
2017-08-18T11:18:38.572+0800    reading metadata for hr.emp from /root/dump/hr/emp.metadata.json
2017-08-18T11:18:38.572+0800    reading metadata for hr.inventory from /root/dump/hr/inventory.metadata.json
2017-08-18T11:18:38.572+0800    reading metadata for hr.emps_infos from /root/dump/hr/emps_infos.metadata.json
2017-08-18T11:18:38.578+0800    restoring hr.inventory from /root/dump/hr/inventory.bson
2017-08-18T11:18:38.585+0800    restoring hr.emps from /root/dump/hr/emps.bson
2017-08-18T11:18:38.591+0800    restoring hr.emp from /root/dump/hr/emp.bson
2017-08-18T11:18:38.596+0800    restoring indexes for collection hr.inventory from metadata
2017-08-18T11:18:38.596+0800    no indexes to restore
2017-08-18T11:18:38.596+0800    finished restoring hr.emp (9 documents)
2017-08-18T11:18:38.596+0800    no indexes to restore
2017-08-18T11:18:38.596+0800    finished restoring hr.emps (11 documents)
2017-08-18T11:18:38.596+0800    restoring hr.emps_infos from /root/dump/hr/emps_infos.bson
2017-08-18T11:18:38.605+0800    no indexes to restore
2017-08-18T11:18:38.605+0800    finished restoring hr.emps_infos (11 documents)
2017-08-18T11:18:38.605+0800    finished restoring hr.inventory (10 documents)
2017-08-18T11:18:38.605+0800    reading metadata for hr.t_emps_job from /root/dump/hr/t_emps_job.metadata.json
2017-08-18T11:18:38.605+0800    reading metadata for hr.shop from /root/dump/hr/shop.metadata.json
2017-08-18T11:18:38.606+0800    reading metadata for hr.dept from /root/dump/hr/dept.metadata.json
2017-08-18T11:18:38.606+0800    reading metadata for hr.t_emps_sex from /root/dump/hr/t_emps_sex.metadata.json
2017-08-18T11:18:38.610+0800    restoring hr.t_emps_job from /root/dump/hr/t_emps_job.bson
2017-08-18T11:18:38.616+0800    restoring hr.dept from /root/dump/hr/dept.bson
2017-08-18T11:18:38.638+0800    restoring hr.t_emps_sex from /root/dump/hr/t_emps_sex.bson
2017-08-18T11:18:38.639+0800    restoring hr.shop from /root/dump/hr/shop.bson
2017-08-18T11:18:38.639+0800    no indexes to restore
2017-08-18T11:18:38.639+0800    finished restoring hr.dept (5 documents)
2017-08-18T11:18:38.641+0800    no indexes to restore
2017-08-18T11:18:38.641+0800    finished restoring hr.t_emps_job (6 documents)
2017-08-18T11:18:38.641+0800    reading metadata for hr.depts from /root/dump/hr/depts.metadata.json
2017-08-18T11:18:38.651+0800    reading metadata for hr.news from /root/dump/hr/news.metadata.json
2017-08-18T11:18:38.652+0800    restoring hr.depts from /root/dump/hr/depts.bson
2017-08-18T11:18:38.657+0800    restoring hr.news from /root/dump/hr/news.bson
2017-08-18T11:18:38.660+0800    no indexes to restore
2017-08-18T11:18:38.660+0800    finished restoring hr.t_emps_sex (2 documents)
2017-08-18T11:18:38.661+0800    reading metadata for hr.loc from /root/dump/hr/loc.metadata.json
2017-08-18T11:18:38.661+0800    restoring indexes for collection hr.shop from metadata
2017-08-18T11:18:38.661+0800    no indexes to restore
2017-08-18T11:18:38.661+0800    finished restoring hr.depts (2 documents)
2017-08-18T11:18:38.661+0800    reading metadata for hr.phones from /root/dump/hr/phones.metadata.json
2017-08-18T11:18:38.661+0800    restoring indexes for collection hr.news from metadata
2017-08-18T11:18:38.666+0800    restoring hr.loc from /root/dump/hr/loc.bson
2017-08-18T11:18:38.670+0800    finished restoring hr.shop (7 documents)
2017-08-18T11:18:38.670+0800    reading metadata for hr.info from /root/dump/hr/info.metadata.json
2017-08-18T11:18:38.674+0800    finished restoring hr.news (4 documents)
2017-08-18T11:18:38.679+0800    restoring hr.phones from /root/dump/hr/phones.bson
2017-08-18T11:18:38.684+0800    no indexes to restore
2017-08-18T11:18:38.684+0800    finished restoring hr.loc (2 documents)
2017-08-18T11:18:38.687+0800    restoring hr.info from /root/dump/hr/info.bson
2017-08-18T11:18:38.687+0800    restoring indexes for collection hr.phones from metadata
2017-08-18T11:18:38.697+0800    finished restoring hr.phones (0 documents)
2017-08-18T11:18:38.697+0800    no indexes to restore
2017-08-18T11:18:38.697+0800    finished restoring hr.info (0 documents)
2017-08-18T11:18:38.697+0800    done
> show collections
dept
depts
emp
emps
emps_infos
info
inventory
loc
news
phones
shop
t_emps_job
t_emps_sex
> db.dept.find()
{ "_id" : ObjectId("599504e70184ff511bf02be1"), "deptno" : 11, "dname" : "財(cái)務(wù)部", "loc" : "北京" }
{ "_id" : ObjectId("599504e70184ff511bf02be2"), "deptno" : 12, "dname" : "財(cái)務(wù)部", "loc" : "北京" }
{ "_id" : ObjectId("599504e70184ff511bf02be3"), "deptno" : 13, "dname" : "財(cái)務(wù)部", "loc" : "北京" }
{ "_id" : ObjectId("599504e80184ff511bf02be4"), "deptno" : 14, "dname" : "財(cái)務(wù)部", "loc" : "北京" }
{ "_id" : ObjectId("599505240184ff511bf02be5"), "deptno" : 16, "dname" : "財(cái)務(wù)部", "loc" : "北京" }

網(wǎng)站題目:MongoDB之備份與恢復(fù)-創(chuàng)新互聯(lián)
瀏覽地址:http://www.muchs.cn/article42/ceeoec.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、域名注冊定制開發(fā)、自適應(yīng)網(wǎng)站搜索引擎優(yōu)化、面包屑導(dǎo)航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名