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


相关推荐

  • log4j.properties 详解与配置步骤[通俗易懂]

    log4j.properties 详解与配置步骤[通俗易懂]一、入门实例1.新建一个JAva工程,导入包log4j-1.2.17.jar,整个工程最终目录如下2、src同级创建并设置log4j.properties###设置###log4j.rootLogger=debug,stdout,D,E###输出信息到控制抬###log4j.appender.stdout=org.apache.log4j.ConsoleAp…

    2022年9月30日
    0
  • bat批处理命令大全_文件批处理命令

    bat批处理命令大全_文件批处理命令批处理文件(batchfile)包含一系列DOS命令,通常用于自动执行重复性任务。用户只需双击批处理文件便可执行任务,而无需重复输入相同指令。编写批处理文件非常简单,但难点在于确保一切按顺序执行。编写严谨的批处理文件可以极大程度地节省时间,在应对重复性工作时尤其有效在Windows中善用批处理可以简化很多重复工作批处理? 批处理(Batch),也称为批处理脚本。顾名思义,批处理就是对某对象进行批量的处理。批处理文件的扩展名为bat 目前比较常见的批处理包含两类: DOS批

    2022年8月22日
    5
  • 让我郁闷的第一次做站[通俗易懂]

    让我郁闷的第一次做站[通俗易懂]我是今年7月份毕业的,我在学校学的软件专业,但是在学校的时候很贪玩,没学到多少东西,毕业后找本专业的工作处处碰壁找不到,后来去了个seo公司,他们是做英文的,这也是我第一次接触这个行业,原来不知道seo的存在。这个公司很小的,其实主要的业务都是给别人代发外链,我也就成了外链专员。因为刚接触连seo是什么都不知道,我就在网上到处找相关的论坛视频教程看,发现很多教程都是要收费的,不收费的讲的太潦草,有

    2022年5月17日
    35
  • 报错sqlSessionFactory「建议收藏」

    报错sqlSessionFactory「建议收藏」Exceptionencounteredduringcontextinitialization-cancellingrefreshattempt:org.springframework.beans.factory.BeanCreationException:Errorcreatingbeanwithname’sqlSessionFactory’definedinclasspathresource[applicationContext.xml]:Invocation

    2022年5月1日
    208
  • django数据库迁移命令_布局输出到模型的命令

    django数据库迁移命令_布局输出到模型的命令迁移命令makemigrations:将模型生成迁移脚本。模型所在的app,必须放在settings.py中的INSTALLED_APPS中。这个命令有以下几个常用选项:app_label:后面可

    2022年8月7日
    1
  • VCS仿真VHDL VERILOG混合脚本「建议收藏」

    VCS仿真VHDL VERILOG混合脚本「建议收藏」#!/bin/csh#虚拟路径.PHONY:comsimcovcleandebug#DEFINEALL_DEFINE=+define+DUMP_VPD #预编译宏定义,本例程没有用到宏定义#OUTPUHTOUTPUT=simv #输出文件的文件名#Codecoveragecommand#覆盖率检查CM=-cmline+cond+fsm+branch+tgl#收集的代码覆盖率类型CM_NAME=-c..

    2022年9月21日
    0

发表回复

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

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