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)


相关推荐

  • CSDN日报20170304——《令人比较失落的IT圈子-关于华为裁员》[通俗易懂]

    CSDN日报20170304——《令人比较失落的IT圈子-关于华为裁员》[通俗易懂]早在几年前就有人说过程序员在35岁以后如果不做管理就很难混了,如今由于近日的华为事件被炒得沸沸扬扬,显然让这多年前人们的猜测变成了现实,我今年也正好到了这个该“退休”的年龄,所以就想趁机悔恨一番。首先,澄清的一点就是,我并无意诋毁这个IT行业,我只是希望大家可以更加清除的认清这个行业。

  • cnn lstm pytorch_pytorch怎么用

    cnn lstm pytorch_pytorch怎么用LSTM模型结构1、LSTM模型结构2、LSTM网络3、LSTM的输入结构4、Pytorch中的LSTM4.1、pytorch中定义的LSTM模型4.2、喂给LSTM的数据格式4.3、LSTM的output格式5、LSTM和其他网络组合1、LSTM模型结构BP网络和CNN网络没有时间维,和传统的机器学习算法理解起来相差无几,CNN在处理彩色图像的3通道时,也可以理解为叠加多层,图形的三维矩阵当做空间的切片即可理解,写代码的时候照着图形一层层叠加即可。如下图是一个普通的BP网络和CNN网络。图中的隐

  • Insecure Direct Object References

    Insecure Direct Object References目录越权分类危害解决越权访问一些心得InsecureDirectObjectReference不安全对象是怎么直接被引用的?其它可能不安全对象直接引用…

  • Java 枚举活用

    Java 枚举活用

  • SpringBoot项目报错解决:“Error starting ApplicationContext. To display the conditions report re-run …”

    SpringBoot项目报错解决:“Error starting ApplicationContext. To display the conditions report re-run …”SpringBoot项目报错:ErrorstartingApplicationContext.Todisplaytheconditionsreportre-runyourapplicationwith’debug’enabled.以下方案80%可以帮助您解决这些个‘可恶的’问题报错内容和截图如下:ConnectedtothetargetVM,address:’127.0.0.1:4963′,transport:’socket’.____

  • oracle10g在win10上的安装

    oracle10g在win10上的安装一、下载官网下载地址: https://www.oracle.com/downloads/index.html#menu-downloads或者:链接:http://pan.baidu.com/s/1cGr3PW密码:oz8n下载解压后得到:三个安装包:PL/SQLDe

发表回复

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

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