Simple Automated Backups for MongoDB Replica Sets
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
'myState' ] print( 'Host ); rs.stepDown(); while
'myState' ] 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}); // 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.
版权声明:本文博客原创文章,博客,未经同意,不得转载。
Simple Automated Backups for MongoDB Replica Sets的更多相关文章
- 转 Warning:MongoDB Replica Sets配置注意事项
我们知道,MongoDB不提供单机的数据安全性,取而代之的是提供了Replica Sets的高可用方案.官方文档中提到的案例是三个节点组成的Replica Sets,这样在其中任何一个节点宕机后都会自 ...
- MongoDB整理笔记のReplica Sets
MongoDB支持在多个机器中通过异步复制达到故障转移和实现冗余.多机器中同一时刻只有一台机器是用于写操作,正因为如此,MongoDB提供了数据一致性的保障.而担当primary角色的机器,可以把读的 ...
- MongoDB 学习笔记(三) MongoDB (replica set) 集群配置
MongoDB Replica Sets的结构类似于以集群,完全可以把他当成一个集群,因为他确实与集群实现的作用是一样的:如果其中一个节点出现故障,其他的节点会马上将业务接管过来.而无需停机操作 Mo ...
- 管理维护Replica Sets
1.读写分离 有一些第三方的工具,提供了一些可以让数据库进行读写分离的工具.我们现在是否有一个疑问,从库要是能进行查询就更好了,这样可以分担主库的大量的查询请求. 1. 先向主库中插入一条测试数据 2 ...
- MongoDB Replica Set搭建集群
MongoDB做集群,版本3.2官网推荐的集群方式Replica Set 准备服务器3台 两个standard节点(这两个节点直接可以互切primary secondary). 一个arbiter节点 ...
- Mongo的Replica Sets (复制集)的配置全过程和心得体会
http://blog.csdn.net/bloggongchang/article/details/7272403 一.MongoDB Replica Sets(副本集)简单的说就是有自动故障恢复功 ...
- 利用Mongodb的复制集搭建高可用分片,Replica Sets + Sharding的搭建过程
参考资料 reference: http://mongodb.blog.51cto.com/1071559/740131 http://docs.mongodb.org/manual/tutori ...
- Mongodb集群搭建之 Sharding+ Replica Sets集群架构
1.本例使用1台Linux主机,通过Docker 启动三个容器 IP地址如下: docker run -d -v `pwd`/data/master:/mongodb -p 27017:27017 d ...
- MongoDB高可用架构:Replica Sets+Sharding
MongoDB的sharding解决了海量存储和动态扩容的问题.但是遇到单点故障就显得无能为力了.MongoDB的副本集可以很好的解决单点故障的问题.所以就有了Sharding+Replica Set ...
随机推荐
- UNIX 网络编程之线程
概述: 实现并发服务器一般都是父进程accept一个连接,然后fork一个子进程,该子进程处理与该连接对端的客户之间的通信.但是fork是昂贵,耗资源和时间.而线程是轻量级线程,它的创建比进程的创建块 ...
- Graphviz 绘制流程图
凝视说明非常具体.不再详述. digraph G{ //dot 是一种画图语言,它能够方便你採用图形的方式高速.直观地表达一些想法, //比方描写叙述某个问题的解决方式,构思一个程序的流程,澄清一堆貌 ...
- iphone开发中数据持久化之——模型对象归档(二)
在Cocoa世界中,术语“归档”是指另一种形式的序列化,它可以实现对任何对象的序列化.使用对模型对象进行归档的技术可以轻松将复杂的对象写入文件,然后再从中读取它们.只要在类中实现的每个属性都是标量(如 ...
- ActionBar本部分适用述评
http://note.youdao.com/share/?id=7f213cb64069bad221f4581507707294&type=note 因为把图片拿进来太麻烦,所以我给了一个直 ...
- hdu3485(递推)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3485 分析: a[i]表示长度为i,第i位为0的,符合情况的个数. b[i]表示长度为i,第i位为1的 ...
- hdu 4925 贪心 自己从小到大做数据找方法规律
http://acm.hdu.edu.cn/showproblem.php?pid=4925 自己逐个做数据找规律.提供下我的找的: 1 2 1 3 2 2 2 3 3 3 然后发现这种矩阵是最优的: ...
- 新西兰gap year_百度百科
新西兰gap year_百度百科 新西兰gap year Working Holiday Visa,即打工度假签证.它允许旅行者出于补贴旅行费用的目的而在签证颁发国边打工边旅行.用来鼓励双方国家 ...
- hdu 折线切割平面 (java)
问题: 仅仅要找到规律问题就攻克了,在做题时应该细致去发现数与数之间的联系. 折线切割平面 Time Limit: 2000/1000 MS (Java/Others) Memory Limit ...
- 【转向Javascript系列】深入理解Web Worker
本文首发在alloyteam团队博客,链接地址http://www.alloyteam.com/2015/11/deep-in-web-worker/ 上一篇文章<从setTimeout说事件循 ...
- MYSQL经常使用命令列表
MYSQL经常使用命令列表 1.系统管理 mysql -h主机地址 -uusername -p 连接MYSQL(在mysql/bin) exit 退出MYSQL命令 mysqladmin -uuser ...