MongoDB之備份與恢復

stonebox1122發表於2017-08-24
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" : "北京" }

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28536251/viewspace-2144111/,如需轉載,請註明出處,否則將追究法律責任。

相關文章