Simple Automated Backups for MongoDB Replica Sets
2015-07-08 18:03
435 查看
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:
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:
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:
And finally, the whole thing can be automated by calling /usr/bin/mongo admin /etc/mongodb-backup.js from a cron job.
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:
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:
相关文章推荐
- mongoDB常用命令
- MongoDB安装和简介
- mongodb的安装和简单命令
- mongodb文档型数据库特点介绍
- 扩展MongoDB C# Driver的QueryBuilder
- centos上的mongodb的安装及php扩展mongodb
- 二、MongoDB的简单增删改查
- 一、MongoDB安装与启动
- thinkphp 配合mongodb
- GridFS:基于MongoDB的分布式文件存储系统
- yum安装mongodb
- mongoDB学习之旅(一)
- MongoDB 那些坑
- mongodb 管理脚本
- MONGODB 集群架构 调整,增加延迟备份节点服务器,删除仲裁节点
- 配置MongoDB3.04集群分片
- MongoDB中对象反序列化的一个小问题
- mongodb C++调用查询DEMO
- mongodb常用操作命令整理
- mongodb aggregate 级联查询