资讯专栏INFORMATION COLUMN

Simple Automated Backups for MongoDB Replica Sets

iamyoung001 / 2652人阅读

There are a bunch of different methods you can use to back up your MongoDB data, but if you want to avoid downtime and/or potential performance degradation, the most common advice seems to be that you should simply do all your backups on a slave. This makes sense, since most of your queries will be hitting the primary server anyway. Unfortunately, picking a slave isn’t so simple when dealing with replica sets, because (due to automated failover) you can never really be sure which servers in the set are slaves. My workaround is to simply pick any server and then force it to be a slave before running a backup.
I do this by sticking all my backup commands in a JavaScript file (e.g. /etc/mongodb-backup.js) which starts with something like this:

  

if

  

["myState"]
== 1) {

  

print("Host
is master (stepping down)");

  

rs.stepDown();

  

while

  

["myState"]
!= 2) {

  

sleep(1000);

  

}

  

}

This snippet uses the rs.status() replica set command to check the current state of the local machine. If the myStatefield is “1,” then we know the machine is a primary, so we execute rs.stepDown() and wait until myState is “2” (which means the server has successfully made the transition from primary to secondary).
Next come the actual backup commands. For mongodump, something like this will work:

  

runProgram("mongodump",
">-o",
"/mnt/backups/mongodb/");

Alternatively, it’s possible to take backups of the raw data files. Notice that in this case, we need to sync the files to disk and disable writes first, then re-enable writes once the backup completes:

  

db.runCommand({fsync:1,lock:1});
//
sync and lock

  

runProgram("rsync",
"-avz",
"--delete",
"/var/lib/mongodb/",
"/mnt/backups/mongodb/");

  

db.$cmd.sys.unlock.findOne();
//unlock

And finally, the whole thing can be automated by calling /usr/bin/mongo admin /etc/mongodb-backup.js from a cron job.

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/18703.html

相关文章

  • 使用mongo shell远程连接数据库

    摘要:序本文主要介绍一下如何使用连接远程数据库这个里头的就包含了后面不传参数则会在命令行提示输入查看空间占用所有数据的总大小所有数据占的磁盘大小以为单位以为单位查看集合查询 序 本文主要介绍一下如何使用mongo shell连接远程数据库 install curl -o mongodb-linux-x86_64-3.2.4.tgz https://fastdl.mongodb.org/lin...

    roundstones 评论0 收藏0
  • MongoDB学习笔记(1)- MongoDB简介、数据类型及帮助命令

    摘要:数据模型取决于数据库类型。仅支持位浮点数,所以位整数会被自动转换为位浮点数。位浮点数中的数字都是这种类型。数字只能表示为双精度数位浮点数的另外一个问题是,有些位的整数并不能精确地表示为位浮点数。 MongoDB学习笔记(1)- MongoDB简介及数据类型 本文所使用的MongoDB版本为 4.0.10 > db.version(); 4.0.10 一、MongoDB 介绍 1. Mo...

    nihao 评论0 收藏0
  • MongoDB - write concern concept

    摘要:对于多文档操作来说,并未提供多文档事物或隔离。在间隔过期之前,不会回滚或撤消已做出的修改。仅在将数据提交到后才告知客户端。为了降低操作延迟,也增加了它提交数据至日志的频率。 本文翻译自 write concern。 注意,本文内容适用于MongoDB Manual 3.0.2及以上版本。 Whats write concern 所谓的写关注就是当向客户端报告写操作的成功...

    callmewhy 评论0 收藏0
  • mongo EOF(二)

    摘要:容器访问以为例,在原始的文件中,如下上面的配置,本地主机是无法访问容器的,我们至少需要暴露出一个端口。查看,的默认端口其实是,而这里写成也是有原因的。 任何事情的成功都需要掐准时间 上一节mongo EOF中,关于容器的配置,只是粗略的使用了Docker-Compose-MongoDB-Replica-Set项目提供好的docker-compose.yml文件。在使用过程中,我发现这个...

    dreambei 评论0 收藏0
  • MongoDB 2014 大会亮相北京,SegmentFault 给力支持现场吸睛

    摘要:活动之图文直播现在,在现场为大家发来报道大会在主持人甜美的声音中开场,来自,作为中文社区的官方合作伙伴,是这次大会的特别支持。 11月22日,MongoDB 2014大会在北京拉开帷幕,这是一场程序员,架构师和运维工程师齐聚的技术盛会,会上,MongoDB顶级专家对MongoDB性能调试与扩展、模式设计艺术、集群管理及迁移以及大规模的使用等方面做了详细讲解,有200多开发者参与了此次盛...

    ThinkSNS 评论0 收藏0

发表评论

0条评论

iamyoung001

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<