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
(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.
相關文章
- mongodb replica sets 測試MongoDB
- MongoDB 複製集模式Replica SetsMongoDB模式
- MongoDB系列二:Replica Sets安裝與配置MongoDB
- mongodb複製集(replica sets)+分片(sharding)環境搭建MongoDB
- Replica sets複製集的搭建
- MongoDB系列三:Replica Sets在生產環境中安裝配置的注意事項MongoDB
- 利用Mongodb的複製集搭建高可用分片,Replica Sets + Sharding的搭建過程MongoDB
- 【Mongodb】如何建立mongodb的replica setMongoDB
- Kubernetes Replica Sets 和 Kubernetes Replica Controller的區別Controller
- mongodb 3.0 replica set 配置MongoDB
- [MONGODB]: WHEN ARBITER REQUIRED FOR REPLICA SETMongoDBUI
- mongodb replica set 和 nodejs中使用mongoose連線replicaMongoDBNodeJS
- MongoDB Replica Set 副本集實踐MongoDB
- MongoDB搭建Replica Set複製集MongoDB
- docker 下部署mongodb Replica Set 叢集DockerMongoDB
- 【Mongodb】 Replica set 的讀寫分離MongoDB
- kubernetes概念之四:Replication Controller&Replica Sets&DeploymentsController
- mongodb複製集(replica set)搭建及管理MongoDB
- mongodb叢集shard_replica的搭建方法MongoDB
- 【MongoDB】高可用方案之副本集(Replica Set)MongoDB
- 【Mongodb】Replica Set 的選舉策略之三MongoDB
- 【Mongodb】 Replica set 的 選舉策略之二MongoDB
- 【Mongodb】 Replica set 的選舉策略之一MongoDB
- 【Mongodb】 replica set 新增和刪除節點。MongoDB
- MongoDB副本集replica set (二)--副本集環境搭建MongoDB
- MongoDB系列-解決面試中可能遇到的MongoDB複製集(replica set)問題MongoDB面試
- 小丸子學MongoDB系列之——部署Replica Set+Sharded ClusterMongoDB
- 【Mongodb】 replica set 兩種新增節點方法的日誌分析MongoDB
- three predefined automated maintenance tasksAINaN
- Using mysqldump for backupsMySql
- RMAN Fast Incremental BackupsASTREM
- Monitoring RMAN Backups
- [Developer] Grouping setsDeveloper
- A replica with the same server_uuid/server_id as this replica has connected to the source;ServerUI
- Making Whole Database Backups with RMANDatabase
- Rman Backups When The Directory Structures Are DifferentStruct
- Equivalent Sets(HDU-3836)UI
- SQL grouping sets 子句SQL