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


相关推荐

  • java uninstall tool_java卸载工具|java卸载工具(JavaUninstallTool)下载v1.1.0.0 – 欧普软件下载…「建议收藏」

    java uninstall tool_java卸载工具|java卸载工具(JavaUninstallTool)下载v1.1.0.0 – 欧普软件下载…「建议收藏」java卸载工具(JavaUninstallTool)是甲骨文官方发布的一款专门用于卸载JAVA软件的工具,能够非常方便快捷以及彻底的卸载掉JAVA,使用很简单,只需要解压缩即可使用,赶快下载使用吧!软件功能1、检测到的Java版本将向用户显示以供删除2、用户可以选择删除全部版本,也可以选择删除特定的Java版本3、适用于Windows操作系统4、检测并允许删除Java版本1.4…

    2022年5月12日
    33
  • 维控PLC学习笔记

    维控PLC学习笔记维控PLC学习笔记目录:1.主要编程思想2.梯形图3.视频教程4.网站和论坛依旧采用我多年的凌乱记忆法:感觉PLC就像一个处理函数的集成电路设备,我用的是老师给的维控PLC官网介绍说它很牛逼,可以兼容三菱FX系列而且比三菱快,价格也比三菱便宜。(这波抄袭很稳),因为我老师有一个公司是做工业包装机的,所以我这里有很多传感器可以使用。最近研究了…

    2022年9月25日
    0
  • java 反编译器_JAVA编译器

    java 反编译器_JAVA编译器xjad反编译工具下载使用反编译时把class文件直接拖拽至工具内即可,如果反编译结果不对时把class文件重新去拿原始的不要编辑打开,或者放在一个文件夹内在试。反编译后的代码没有注释、注解等,反正能用得细心看看调整。点击下载工具http://a.xzfile.com/down2/XJadfanbinayi_downcc.zip…

    2022年8月19日
    5
  • slam技术是什么_技术文小说

    slam技术是什么_技术文小说什么是SLAM?SLAM(simultaneouslocalizationandmapping),也称为CML(ConcurrentMappingandLocalization),即时定位与地图构建,或并发建图与定位。问题可以描述为:将一个机器人放入未知环境中的未知位置,是否有办法让机器人一边逐步描绘出此环境完全的地图,同时一边决定机器人应该往哪个方向行进。例如扫地机器人就是一个很典型的SLAM问题,所谓完全的地图(aconsistentmap)是指不受障碍行进到房间可进入.

    2022年10月1日
    1
  • C语言malloc函数的功能及用法

    C语言malloc函数的功能及用法关于C语言malloc函数函数介绍应用举例1应用举例2函数介绍malloc(memoryallocation) 中文名称:动态内存分配用于申请一块连续的指定大小的内存块区域以void*类型返回分配的内存区域地址,当无法知道内存具体位置的时候,想要绑定真正的内存空间,就需要用到动态的分配内存。应用举例1关于C语言动态申请数组(整形数据类型)空间的应用#include<stdio…

    2022年6月9日
    38
  • 进销存ERP源码 进销存APP源码 带小程序ERP源码

    进销存ERP源码 进销存APP源码 带小程序ERP源码前端开发:uniapp后端开发:PHP数据库:MySql移动端:APP+小程序界面简洁,前端采用Bootstrap以及Bootstrap-table相结合,实现自适应的效果、弹窗等。

    2022年5月31日
    69

发表回复

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

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