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)
上一篇 2022年1月2日 下午10:00
下一篇 2022年1月2日 下午10:00


相关推荐

  • jsonobject转string数组_jsonobject.parsearray

    jsonobject转string数组_jsonobject.parsearray1.String转JSONObjectStringjsonMessage=”{\”语文\”:\”88\”,\”数学\”:\”78\”,\”计算机\”:\”99\”}”;JSONObject myJson=JSONObject.fromObject(jsonMessage);2.String转JSONArrayStringjsonMessage=”[{‘nu

    2022年10月4日
    4
  • java 泛型方法 类型_Java泛型方法

    java 泛型方法 类型_Java泛型方法一 泛型方法如果在调用方法的时候方法的参数类型不确定 或者是方法的返回值类型不确定 那么我们可以将这个方法定义为泛型方法 一般在一些工具类中会经常使用到 现在只是给你作为语法提出来 你要明白 深入的体会是需要后面在你自己设计工具类的时候你才能体会其真正的意义 DEMO 定义泛型方法以上的泛型方法没有返回值 如果返回值也不确定呢 那么我们要定义返回值也是泛型的方法 DEMO 定义泛型方法 返回值也是

    2026年3月17日
    2
  • NTP校时设置

    NTP校时设置一、WindowsServer2008–TimeServer前言:国家时间与频率标准实验室 &&NTP服务器 也可以忽略1~6直接跳7 如果已改过机码请使用 1    Cmd:2     netstopw32time3     w32tm/unregister4     w32tm/register…

    2022年6月24日
    70
  • Jenkins(4)docker容器内部修改jenkins容器时间「建议收藏」

    Jenkins(4)docker容器内部修改jenkins容器时间「建议收藏」前言用docker搭建的Jenkins环境时间显示和我们本地时间相差8个小时,需修改容器内部的系统时间查看时间查看系统时间date-R进入docker容器内部,查看容器时间dockere

    2022年7月28日
    9
  • c++入门教程–-2基本语法

    c++入门教程–-2基本语法

    2021年3月12日
    110
  • Python ( )、[ ]、{}的区别「建议收藏」

    Python ( )、[ ]、{}的区别「建议收藏」python语言最常见的括号有三种,分别是:小括号()、中括号[]和大括号也叫做花括号{},分别用来代表不同的python基本内置数据类型。如果要创建一个字典列表,如下:

    2022年7月3日
    27

发表回复

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

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