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.

版权声明:本文博客原创文章,博客,未经同意,不得转载。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/117615.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • pycharm安装pyqt5-tools_pycharm如何导入pygame模块

    pycharm安装pyqt5-tools_pycharm如何导入pygame模块1.根据自己的系统和python版本下载安装,我用的是:PyQt5-5.6-gpl-Py3.5-Qt5.6.0-x32-2.exepython-3.5.4.exepycharm装的是激活成功教程版以上按次序依次安装,都按照默认路径安装即可。2.打开pycharm2.因为我用来写了一个串口

  • 玫瑰花(C语言代码)

    玫瑰花(C语言代码)代码用到图形库,有音乐,音乐名为BadApple.mp3,可以找一首mp3形式的歌,歌名改成BadApple.mp3,与.exe文件放在同一个文件夹下,程序执行时会播放名字为BadApple.mp3音乐(会玩的可以去看程序自己改),不同编译器颜色不同左vc,右Dev,颜色也可以通过RGB()进行修改。加个图形库(EasyX)的链接(根据提示来就行):https://blog.csdn…

  • as安装HAXM报错

    as安装HAXM报错解决办法:1.。查看电脑bios中是否开启intelvirtualtechnology2。打开sdkmanager安装intelx86RmulatorAccelerator(HAXM)

  • 海贼王

    海贼王★海贼王简介  日本国民级超人气动漫作品《ONEPIECE》,简称OP(1997-现在)  【原名】ONEPIECE——十多年来日本的统治级漫画,单行本十年销量冠军,初版销量已超1亿5千8百万册,09年4月再版销量1千7百万册,总销量1亿7500万册,成为日本史上漫画单行本销量最高和初版销量最高的纪录最快到本书一亿保持者,本书销量第一。  【译名】海贼王、航海王、海盗路飞

  • hackbar 使用教程_HackMan:打造Chrome下的HackBar !「建议收藏」

    hackbar 使用教程_HackMan:打造Chrome下的HackBar !「建议收藏」本Chrome插件基于PostMan插件二次开发,感谢作者。请不要说闲的JJ疼,都有了HackBar为毛还要在造轮子……我比较喜欢Chrome的API,就写了由于Chrome自身限制,无法做成想Hackbar一样。由于二次开发,面向的用户不一样,就改名为HackMan版权当然还是作者的,勿怪!功能说明左边栏为历史记录和收藏请求顶栏分别为普通模式请求和BasicAuth,DigestAuth,…

  • LARS回归算法的几何意义

    LARS回归算法的几何意义LARS算法的几何意义 1   LARS算法简介  Efron于2004年发表在AnnalsofStatistics的文章LEASTANGLEREGRESSION中提出LARS算法,其核心思想是提出一种新的solutionpath(求解路径),即在已经入选的变量中,寻找一个新的路径,使得在这个路径上前进时,当前残差与已入选变量的相关系数都是相同的,直到找出新的比当前残差相

发表回复

您的电子邮箱地址不会被公开。

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