设为首页 收藏本站
查看: 1234|回复: 0

[经验分享] MongoDB的备份和恢复

[复制链接]

尚未签到

发表于 2018-10-26 12:20:53 | 显示全部楼层 |阅读模式
  1、备份数据文件
  在MongoDB运行时复制数据目录不***全,所以先把服务器关了,再复制数据目录。数据库目录中就是关闭那一刻数据的快照,在服务器重新启动之前,可以复制目录作为备份。
  2、mongodump和mongorestore
  2.1、mongodump
  mongodump是一种能在运行时备份的方法。它对运行的mongodb做查询,然后将所有查到的文档写入磁盘。它使用普通的查询机制,所以产生的备份不一定是服务器数据的实时快照,服务器在备份过程中处理写入时尤为明显。
  另外,mongodump还带来个问题,备份时的查询会对其它客户端的性能产生不利的影响。
  [root@gflinux102 ~]# mongodump --help
  Export MongoDB data to BSON files.
  Options:
  --help                                produce help message
  -v [ --verbose ]                      be more verbose (include multiple times
  for more verbosity e.g. -vvvvv)
  --quiet                               silence all non error diagnostic
  messages
  --version                             print the program's version and exit
  -h [ --host ] arg                     mongo host to connect to ( /s1,s2 for sets)
  --port arg                            server port. Can also use --host
  hostname:port
  --ipv6                                enable IPv6 support (disabled by
  default)
  -u [ --username ] arg                 username
  -p [ --password ] arg                 password
  --authenticationDatabase arg          user source (defaults to dbname)
  --authenticationMechanism arg (=MONGODB-CR)
  authentication mechanism
  --gssapiServiceName arg (=mongodb)    Service name to use when authenticating
  using GSSAPI/Kerberos
  --gssapiHostName arg                  Remote host name to use for purpose of
  GSSAPI/Kerberos authentication
  --dbpath arg                          directly access mongod database files
  in the given path, instead of
  connecting to a mongod  server - needs
  to lock the data directory, so cannot
  be used if a mongod is currently
  accessing the same path
  --directoryperdb                      each db is in a separate directory
  (relevant only if dbpath specified)
  --journal                             enable journaling (relevant only if
  dbpath specified)
  -d [ --db ] arg                       database to use
  -c [ --collection ] arg               collection to use (some commands)
  -o [ --out ] arg (=dump)              output directory or "-" for stdout
  -q [ --query ] arg                    json query
  --oplog                               Use oplog for point-in-time
  snapshotting
  --repair                              try to recover a crashed database
  --forceTableScan                      force a table scan (do not use
  $snapshot)
  --dumpDbUsersAndRoles                 Dump user and role definitions for the
  given database
  备份测试数据库
  [root@gflinux102 ~]# mongodump --port 10000 -d test -o backup
  connected to: 127.0.0.1:10000
  2015-02-13T09:51:05.506+0800 DATABASE: test to backup/test
  2015-02-13T09:51:05.508+0800 test.system.indexes to backup/test/system.indexes.bson
  2015-02-13T09:51:05.510+0800  2 documents
  2015-02-13T09:51:05.511+0800 test.rgf to backup/test/rgf.bson
  2015-02-13T09:51:05.512+0800  16 documents
  2015-02-13T09:51:05.512+0800 Metadata for test.rgf to backup/test/rgf.metadata.json
  [root@gflinux102 ~]# ls  -R backup/
  backup/:
  test
  backup/test:
  rgf.bson  rgf.metadata.json  system.indexes.bson
  [root@gflinux102 ~]#
  会在当前目录下产生backup目录,之后产生备份数据库目录。
  2.2、mongorestore
  [root@gflinux102 ~]# mongorestore --help
  Import BSON files into MongoDB.
  usage: mongorestore [options] [directory or filename to restore from]
  Options:
  --help                                produce help message
  -v [ --verbose ]                      be more verbose (include multiple times
  for more verbosity e.g. -vvvvv)
  --quiet                               silence all non error diagnostic
  messages
  --version                             print the program's version and exit
  -h [ --host ] arg                     mongo host to connect to ( /s1,s2 for sets)
  --port arg                            server port. Can also use --host
  hostname:port
  --ipv6                                enable IPv6 support (disabled by
  default)
  -u [ --username ] arg                 username
  -p [ --password ] arg                 password
  --authenticationDatabase arg          user source (defaults to dbname)
  --authenticationMechanism arg (=MONGODB-CR)
  authentication mechanism
  --gssapiServiceName arg (=mongodb)    Service name to use when authenticating
  using GSSAPI/Kerberos
  --gssapiHostName arg                  Remote host name to use for purpose of
  GSSAPI/Kerberos authentication
  --dbpath arg                          directly access mongod database files
  in the given path, instead of
  connecting to a mongod  server - needs
  to lock the data directory, so cannot
  be used if a mongod is currently
  accessing the same path
  --directoryperdb                      each db is in a separate directory
  (relevant only if dbpath specified)
  --journal                             enable journaling (relevant only if
  dbpath specified)
  -d [ --db ] arg                       database to use
  -c [ --collection ] arg               collection to use (some commands)
  --objcheck                            validate object before inserting
  (default)
  --noobjcheck                          don't validate object before inserting
  --filter arg                          filter to apply before inserting
  --drop                                drop each collection before import
  --oplogReplay                         replay oplog for point-in-time restore
  --oplogLimit arg                      include oplog entries before the
  provided Timestamp (seconds[:ordinal])
  during the oplog replay; the ordinal
  value is optional
  --keepIndexVersion                    don't upgrade indexes to newest version
  --noOptionsRestore                    don't restore collection options
  --noIndexRestore                      don't restore indexes
  --restoreDbUsersAndRoles              Restore user and role definitions for
  the given database
  --w arg (=0)                          minimum number of replicas per write
  [root@gflinux102 ~]#
  恢复数据库:
  [root@gflinux102 ~]# mongorestore --port 10000 -d test --drop backup/test/
  connected to: 127.0.0.1:10000
  2015-02-13T09:58:19.104+0800 backup/test/rgf.bson
  2015-02-13T09:58:19.104+0800 going into namespace [test.rgf]
  2015-02-13T09:58:19.105+0800  dropping
  16 objects found
  2015-02-13T09:58:19.119+0800 Creating index: { key: { _id: 1 }, name: "_id_", ns: "test.rgf" }
  2015-02-13T09:58:19.120+0800 Creating index: { key: { name: 1 }, name: "name_1", ns: "test.rgf" }
  [root@gflinux102 ~]#
  -d指定了要恢复的数据库,这个选项可以将备份恢复到与原来不同命的数据库中。--drop代表在恢复前删除集合(若存在),否则,数据就会与现有集合数据合并,可能会覆盖一些文档。
  3、fsync和锁
  虽然用mongodump和mongorestore能不停机备份,但是却失去了获取实时数据视图的能力。fsync命令能在mongodb运行时复制数据目录还不会损坏数据库。
  fsync命令会强制服务器将所有缓冲区写入磁盘,还可以选择上锁阻止对数据库的进一步写入,直到释放锁为止。写入锁是让fsync在备份时发挥作用的关键。
  3.1、强制执行并获得写入锁:
  > use admin
  switched to db admin
  > db.runCommand({"fsync":1,"lock":1})
  {
  "info" : "now locked against writes, use db.fsyncUnlock() to unlock",
  "seeAlso" : "http://dochub.mongodb.org/core/fsynccommand",
  "ok" : 1
  }
  >
  3.2、开始备份
  [root@gflinux102 ~]# mongodump --port 10000 -d test -o /opt/backup
  connected to: 127.0.0.1:10000
  2015-02-13T10:11:09.731+0800 DATABASE: test to /opt/backup/test
  2015-02-13T10:11:09.734+0800 test.system.indexes to /opt/backup/test/system.indexes.bson
  2015-02-13T10:11:09.736+0800  2 documents
  2015-02-13T10:11:09.736+0800 test.rgf to /opt/backup/test/rgf.bson
  2015-02-13T10:11:09.738+0800  16 documents
  2015-02-13T10:11:09.738+0800 Metadata for test.rgf to /opt/backup/test/rgf.metadata.json
  [root@gflinux102 ~]# ls -R /opt/backup/
  /opt/backup/:
  test
  /opt/backup/test:
  rgf.bson  rgf.metadata.json  system.indexes.bson
  至此,数据目录的数据就是一致的,且为数据的实时快照。因为上了写入锁,可以安全地将数据目录副本用做备份。要是数据库运行在有快照功能的文件系统上,这个会非常快。
  [root@gflinux102 ~]#
  3.3、解锁
  > use admin
  switched to db admin
  > db.fsyncUnlock()
  { "ok" : 1, "info" : "unlock completed" }
  > db.currentOp()
  { "inprog" : [ ] }
  >
  运行currentop()是为了确保已经解锁。
  有了fsync,就能灵活地备份,不用停掉服务器,也不用牺牲备份的实时特性,要付出的代价就是一些写入操作被暂时阻塞。
  唯一不耽误读写还能保证实时快照的备份方式就是通过从服务器备份。
  4、从属备份
  当以复制方式运行mongodb时,备份技术不仅能用在主服务器上,还能用在从服务器上,用在从服务器上的效果会更好,从服务器的数据几乎与主服务器同步,因为不太在乎从服务器的性能或是能不能读写,关停、存储和恢复工具或fsync命令。在从服务器上备份是mongoDB推荐的备份方式。
  5、修复数据库
  5.1、修复简介
  修复所有数据库最简单的方式就是在启动时加上--repair:
  mongod --repair
  修复原理:将所有的文档导出然后马上导入,忽略那些无效的文档,完成以后,会重新建立索引。
  数据量大的时候,会花费很多时间,因为所有的数据都要验证,所有索引都要重建。修复后可能会比修复前少些文档,因为损毁的文档被丢弃了。
  修复数据库还能起到压缩数据的作用,闲置的空间(比如删除体积较大的集合或删除大量文档后腾出的空间)在修复后被重新回收。
  5.2、修复运行中的数据库
  修复运行中的数据库,要在shell中用repairDatabase。
  例如,修复test数据库:
  > use test;
  switched to db test
  > db.repairDatabase()
  { "ok" : 1 }
  >
  要是不通过shell而是通过驱动程序,可以用repairDatabase来完成相同的事情:
  {"repairDatabase":1}
  修复损坏的数据是不得已。尽可能稳妥地停掉服务器,利用复制功能实现故障恢复,经常做备份,这些才是最有效的管理数据的手段。


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-626768-1-1.html 上篇帖子: Announced MongoDB 3.0 下篇帖子: 【MongoDB学习笔记28】MongoDB的GridFS存储机制
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表