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 ...
随机推荐
- Android登陆界面实现-支持输入框清楚和震动效果功能
演示效果 主要代码例如以下 自己定义的一个EditText.用于实现有文字的时候显示能够清楚的button: import android.content.Context; import androi ...
- margin 等高布局
<div id="main"> <div id="left"> 我是左边的内容的啦啦啦啦... .<br> 我是左边的内容的 ...
- sql2005,sql2008,sql2012清空日志语句
原文:sql2005,sql2008,sql2012清空日志语句 sql2005清空日志语句 Backup Log DbName WITH no_log GO DUMP TRANSACTION DbN ...
- java泛型 之 入门(interface)
一:泛型简单介绍: (1)所谓泛型,就是变量类型的參数化. 泛型是JDK1.5中一个最重要的特征.通过引入泛型,我们将获得编译时类型的安全和执行时更小的抛出ClassCastException的可能. ...
- linux它SQL声明简明教程---WHERE
我们并不一定必须注意,每次格里面的信息是完全陷入了.在很多情况下,我们需要有选择性地捕捞数据.对于我们的样本.我们可以只抓住一个营业额超过 $1,000 轮廓. 做这个事情,我们就须要用到 WHERE ...
- AntiXSS - 支持Html同时防止XSS攻击
AntiXSS - 支持Html同时防止XSS攻击 跨站脚本攻击(XSS)已经不是什么新鲜的话题了,甚至很多大公司也为此吃尽苦头.最简单直接的防范方法,就是不允许任何html标签输入,对用户输入进行编 ...
- 【转】Vim学习资料
初学资料:1:一个介绍VIM操作的游戏,十分适合初学者.只是:不要怕英文.vim-adventures.com2:http://blog.csdn.net/niushuai666/article/de ...
- Android的目录结构说明
- SSH协议详解(转)
转发的http://blog.csdn.net/macrossdzh/article/details/5691924 很透彻啊,学习了 一.什么是SSH SSH是英文Secure Shell的简写形式 ...
- iOS很重要的 block回调
刚刚进入ios开发行业,发现开发中要用到大量的block回调,由此可见它的重要性.学习它之前我也是网上找的资料,推荐这篇文章http://blog.csdn.net/mobanchengshuang/ ...