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


相关推荐

  • 树莓派4B摄像头的详细使用教程(拍照+录像+监控)

    树莓派4B摄像头的详细使用教程(拍照+录像+监控)树莓派4B摄像头的详细使用教程(拍照+录像+监控)本篇博文将介绍树莓派摄像头是如何在树莓派开发板上从安装到使用的,博主过程中参考了许多帖子,现将整理的比较全面的过程分享出来,供大家参考使用。排线连接硬件连接时我们首先需要使用树莓派摄像头FFC排线,连接树莓派摄像头与树莓派开发板。其中排线连接的接口被称为CSI(CameraSerialInterface)接口。树莓派开发板的CSI接口位于USB和以太网接口旁边。我们先将CSI接口的黑色挡板拔开,之后将排线蓝色一端正对以太网接口方向插入,之后按下黑

    2022年6月3日
    60
  • linux中如何给文件重命名_ppt重命名怎么恢复

    linux中如何给文件重命名_ppt重命名怎么恢复Linux下文件重命名、创建、删除、修改及保存文件一、重命名(更名)linux给文件改名的命令是mv命令mv命令来为文件或目录改名或将文件由一个目录移入另一个目录中。该命令等同于DOS系统下的ren和move命令的组合。它的使用权限是所有用户。格式mv[options]源文件或目录目标文件或目录。主要参数[options]-i:交互方式操作。如果mv操作将导致对已存在的目标文…

    2025年8月27日
    6
  • 腾讯会议obs推流教程_流式编程 前端

    腾讯会议obs推流教程_流式编程 前端 **RTSP视频流开发**RTSP视频开发:1.使用VLC播放RTSP视频流,然后使用wireshark抓VLC的包,找到RTSP交互报文2.报文如下: OPTIONSrtsp://192.168.0.212:554/testStreamRTSP/1.0//客户端向服务器发送,说RTSP服务你又那些操作 CSeq:2 User-Age…

    2022年10月17日
    2
  • postman调用rpc接口_postman测试本地接口

    postman调用rpc接口_postman测试本地接口一般使用的接口类型都是http协议传输,第一次遇到RPC类型,使用postman进行测试。请求方式get或者post并不影响,URL是在域名后面加了/rpc,例如,https://www.baidu.com/rpcheaders:写了常规请求时的内容(头为空也不影响)body选择raw,{“jsonrpc”:“2.0”,“method”:“eth_getBalance”,“para…

    2022年10月10日
    2
  • 永磁直流无刷电机驱动器_永磁直流无刷电机的优缺点

    永磁直流无刷电机驱动器_永磁直流无刷电机的优缺点现实生活中我们接触的电机包括很多种类,除部分特殊种类外,永磁电机均是利用定子与转子磁场相互作用的原理制成。其中,使用直流电源驱动的电机称为直流电机,直流电机又可细分为直流有刷电机和直流无刷电机(BLDC)。电刷,是区分“有刷”与“无刷”电机的关键,它是与换向器组合使用的电机组件,常见材质为金属和碳。带有换向器和电刷的电机称为有刷电机,使用电子电路实现换向功能的电机称为无刷电机。直流有刷电…

    2022年10月21日
    1
  • readprocessmemory函数分析_memory的用法及形式

    readprocessmemory函数分析_memory的用法及形式函数功能:该函数从指定的进程中读入内存信息,被读取的区域必须具有访问权限。函数原型:BOOLReadProcessMemory(HANDLEhProcess,LPCVOIDlpBaseAddress,LPVOIDlpBuffer,DWORDnSize,LPDWORDlpNumberOfBytesRead);参数:hProcess:进程句柄

    2022年10月4日
    3

发表回复

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

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