Simple Automated Backups for MongoDB Replica Sets
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Simple Automated Backups for MongoDB Replica Sets,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2055字,纯文字阅读大概需要3分钟。
内容图文
![Simple Automated Backups for MongoDB Replica Sets](/upload/InfoBanner/zyjiaocheng/452/2626ca7b4d0e40579909a992c6038bc8.jpg)
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.
版权声明:本文博客原创文章,博客,未经同意,不得转载。
Simple Automated Backups for MongoDB Replica Sets
标签:
本文系统来源:http://www.cnblogs.com/yxwkf/p/4630971.html
内容总结
以上是互联网集市为您收集整理的Simple Automated Backups for MongoDB Replica Sets全部内容,希望文章能够帮你解决Simple Automated Backups for MongoDB Replica Sets所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。