Simple Automated Backups for MongoDB Replica Sets

Simple Automated Backups for MongoDB Replica Sets

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

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:

1
2
3
4
5
6
7
if
(rs.status()[
'myState'
] == 1) {
  
print(
'Host is master (stepping down)'
);
  
rs.stepDown();
  
while
(rs.status()[
'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:

1
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:

1
2
3
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://javaforall.net/117615.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • KONG网关工作流程简介「建议收藏」

    KONG网关工作流程简介「建议收藏」KONG网关工作流程简介KONG网关工作流程图KONG网关工作流程介绍KONG网关工作流程图KONG网关工作流程介绍上面流程图仅仅是一个大致的示意图,不包含cosumer,plugin,先了解kong大致工作路程,再去深究。一、流程route路由器接收到请求后,根据路由规则,把请求转发到相应的service,service根据host、path、或者url属性,把请求直接转发到target或者把请求转发到upstream(bb两句,这里其实upsteam对service来说是透明的,ups

    2025年8月28日
    7
  • 样品GA的良好理解

    样品GA的良好理解

    2021年12月17日
    42
  • bs架构与cs架构的定义和区别_cs架构的优缺点

    bs架构与cs架构的定义和区别_cs架构的优缺点B/S架构是浏览器和服务器架构模式;C/S架构是客户端和服务器架构模式;CS交互性强,响应速度快,安全性强,一般应用于局域网中,对硬件的要求高,但是开发维护成本高;BS交互性相对弱些,响应速度相对慢,安全性相对低,一般应用于广域网中,可以实现跨平台,客户端零维护。所以有些单位日常办公应用BS,在实际生产中使用CS结构。

    2025年10月18日
    4
  • 记忆化搜索(Memory Search)

    记忆化搜索(Memory Search)Question输入n,符合要求的序列为:第一个数为n,第二个数不大于n,从第三个数起小于前两个数的差的绝对值,后面以此类推。求有多少种序列?(数据:n最大为1000)Sampleinput:4/output:7input:5/output:14input:6/output:26Hintn为4时有如下序列:4142434441141242…

    2022年7月26日
    12
  • python读取json文件内容_pythonjson检测新内容

    python读取json文件内容_pythonjson检测新内容教程前面章节曾介绍过JSON格式的数据,这种格式的数据通常会被转换为Python的list列表或dict字典。本节展示的是世界各国历年GDP总和,数据来源于https://datahub.io网站。数据格式如下:[{“CountryCode”:”ARB”,”CountryName”:”ArabWorld”,”Value”:25760683041.0857,”Y…

    2022年10月12日
    2
  • 「从零单排canal 04」 启动模块deployer源码解析

    「从零单排canal 04」 启动模块deployer源码解析

    2020年11月19日
    196

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号