MongoDB之备份与恢复

发布时间:2020-08-14 08:58:09 作者:stonebox1122
来源:ITPUB博客 阅读:141
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" : "北京" }
推荐阅读:
  1. MongoDB备份恢复笔记
  2. mongodb之备份和恢复介绍

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

mongodb 备份 恢复

上一篇:电脑qq提示音开了但是不响怎么办_电脑qq提示音开了但是不响的解决方法

下一篇:网络相关以及防火墙

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》