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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 一些人人的代替[通俗易懂]

    一些人人的代替[通俗易懂]人人影视:http://www.yyets.com/  丫丫下载站:http://www.yayaxz.com/ 人人影视国际版:http://yzys.tv/   字幕大家也不用担心,推几个网站 http://subom.net/ 2.http://www.opensubtitles.org/   3.http://subsce…

    2022年7月15日
  • floor报错注入原理_sql报错注入

    floor报错注入原理_sql报错注入floor()报错注入准确地说应该是floor,count,groupby冲突报错是当这三个函数在特定情况一起使用产生的错误。首先看经典的floor注入语句:andselect1from(selectcount(*),concat(database(),floor(rand(0)*2))xfrominformation_schema.tablesgroupbyx)a)第一眼看…

    2022年9月27日
  • 做计算机项目的流程图,软件工程实验三(程序流程图和PAD图)[通俗易懂]

    做计算机项目的流程图,软件工程实验三(程序流程图和PAD图)[通俗易懂]《软件工程实验三(程序流程图和PAD图)》由会员分享,可在线阅读,更多相关《软件工程实验三(程序流程图和PAD图)(2页珍藏版)》请在人人文库网上搜索。1、软件工程实验三(程序流程图和PAD图)SHANGHAIUNIVERSITY实验报告学院计算机工程与科学学院学姓号名10122030王杰陈圣波2013.12指导老师日期实验三下面为模块“产生毕业典礼学生表”的功能描述…

    2022年8月13日
  • 排序算法最坏情况下的比较次数怎么算_选择排序最坏情况

    排序算法最坏情况下的比较次数怎么算_选择排序最坏情况在最坏情况下,冒泡排序、直接插入排序与简单选择排序法均需要比较n(n-1)/2次。希尔排序需要比较n^1.5次,堆排序需要比较的次数最少,为nlog2n。

    2022年8月23日
  • Linux tomcat部署War包,Linux在Tomcat部署JavaWeb项目,Linux部署War包

    Linux tomcat部署War包,Linux在Tomcat部署JavaWeb项目,Linux部署War包>>>>>©Copyright 蕃薯耀2017年3月6日http://fanshuyao.iteye.com/ 一、Linux快速部署War包操作,暂时是最简单的操作1、先关闭Tomcat/home/java/tomcat7/bin/shutdown.sh 2、进入War包存放目录(可以通过工具:SSHSecureShellClient把War传到Linux服务器)cd/home/projec

    2022年5月29日
  • osquery+kolide fleet安装[通俗易懂]

    osquery+kolide fleet安装[通俗易懂]osquery是用于记录本机的一些系统信息,如passwd文件的改变,用户的增加等,有了它,我们可以在系统发生改变后,能够及时查询出来。curl-Lhttps://pkg.osquery.io/rpm/GPG|tee/etc/pki/rpm-gpg/RPM-GPG-KEY-osqueryyum-config-manager–add-repohttps://pkg.osqu…

    2022年6月12日

发表回复

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

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