中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

MongoDB之備份與恢復

發布時間:2020-08-14 08:58:09 來源:ITPUB博客 閱讀:141 作者:stonebox1122 欄目:關系型數據庫
MongoDB數據備份
在Mongodb中我們使用mongodump命令來備份MongoDB數據。該命令可以導出所有數據到指定目錄中。
mongodump命令可以通過參數指定導出的數據量級轉存的服務器
語法
mongodump命令腳本語法如下:
>mongodump -h dbhost -d dbname -o dbdirectory
  ● -h:
MongDB所在服務器地址,例如:127.0.0.1,當然也可以指定端口號:127.0.0.1:27017
  ● -d:
需要備份的數據庫實例,例如:test
  ● -o:
備份的數據存放位置,例如:c:\data\dump,當然該目錄需要提前建立,在備份完成后,系統自動在dump目錄下建立一個test目錄,這個目錄里面存放該數據庫實例的備份數據。

備份整個數據庫
[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)

備份結果
[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" : "財務部", "loc" : "北京" }
{ "_id" : ObjectId("599504e70184ff511bf02be2"), "deptno" : 12, "dname" : "財務部", "loc" : "北京" }
{ "_id" : ObjectId("599504e70184ff511bf02be3"), "deptno" : 13, "dname" : "財務部", "loc" : "北京" }
{ "_id" : ObjectId("599504e80184ff511bf02be4"), "deptno" : 14, "dname" : "財務部", "loc" : "北京" }
{ "_id" : ObjectId("599505240184ff511bf02be5"), "deptno" : 16, "dname" : "財務部", "loc" : "北京" }
> db.dept.drop();
true
> db.dept.find();
>


MongoDB數據恢復
mongodb使用 mongorestore 命令來恢復備份的數據。
語法
mongorestore命令腳本語法如下:
>mongorestore -h <hostname><:port> -d dbname <path>
  ● --host <:port>, -h <:port>:
MongoDB所在服務器地址,默認為: localhost:27017
  ● --db , -d :
需要恢復的數據庫實例,例如:test,當然這個名稱也可以和備份時候的不一樣,比如test2
  ● --drop:
恢復的時候,先刪除當前數據,然后恢復備份的數據。就是說,恢復后,備份后添加修改的數據都會被刪除,慎用哦!
  ● <path>:
mongorestore 最后的一個參數,設置備份數據所在位置,例如:c:\data\dump\test。
你不能同時指定 <path> 和 --dir 選項,--dir也可以設置備份目錄。
  ● --dir:
指定備份的目錄

恢復被刪除的集合
[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

刪除數據庫
> db.dropDatabase()
{ "dropped" : "hr", "ok" : 1 }
> use hr
switched to db hr
> show collections
>

恢復被刪除的數據庫
[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" : "財務部", "loc" : "北京" }
{ "_id" : ObjectId("599504e70184ff511bf02be2"), "deptno" : 12, "dname" : "財務部", "loc" : "北京" }
{ "_id" : ObjectId("599504e70184ff511bf02be3"), "deptno" : 13, "dname" : "財務部", "loc" : "北京" }
{ "_id" : ObjectId("599504e80184ff511bf02be4"), "deptno" : 14, "dname" : "財務部", "loc" : "北京" }
{ "_id" : ObjectId("599505240184ff511bf02be5"), "deptno" : 16, "dname" : "財務部", "loc" : "北京" }
向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

沾化县| 吉安县| 调兵山市| 海丰县| 钦州市| 潞西市| 嘉黎县| 桐柏县| 元江| 锡林郭勒盟| 沁水县| 上林县| 图木舒克市| 沾益县| 哈巴河县| 集安市| 郓城县| 茂名市| 达尔| 昌图县| 新田县| 黔江区| 祁连县| 宽甸| 博湖县| 门源| 黄大仙区| 蒙城县| 肇州县| 镇宁| 田东县| 兴山县| 中牟县| 丹巴县| 托克逊县| 鄂托克前旗| 塘沽区| 乡城县| 慈溪市| 杭锦旗| 巫山县|