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:
if
["myState"]
== 1) {
print("Host
is master (stepping down)");
rs.stepDown();
while
["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:
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:
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.
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/18703.html
摘要:序本文主要介紹一下如何使用連接遠(yuǎn)程數(shù)據(jù)庫這個里頭的就包含了后面不傳參數(shù)則會在命令行提示輸入查看空間占用所有數(shù)據(jù)的總大小所有數(shù)據(jù)占的磁盤大小以為單位以為單位查看集合查詢 序 本文主要介紹一下如何使用mongo shell連接遠(yuǎn)程數(shù)據(jù)庫 install curl -o mongodb-linux-x86_64-3.2.4.tgz https://fastdl.mongodb.org/lin...
摘要:數(shù)據(jù)模型取決于數(shù)據(jù)庫類型。僅支持位浮點(diǎn)數(shù),所以位整數(shù)會被自動轉(zhuǎn)換為位浮點(diǎn)數(shù)。位浮點(diǎn)數(shù)中的數(shù)字都是這種類型。數(shù)字只能表示為雙精度數(shù)位浮點(diǎn)數(shù)的另外一個問題是,有些位的整數(shù)并不能精確地表示為位浮點(diǎn)數(shù)。 MongoDB學(xué)習(xí)筆記(1)- MongoDB簡介及數(shù)據(jù)類型 本文所使用的MongoDB版本為 4.0.10 > db.version(); 4.0.10 一、MongoDB 介紹 1. Mo...
摘要:對于多文檔操作來說,并未提供多文檔事物或隔離。在間隔過期之前,不會回滾或撤消已做出的修改。僅在將數(shù)據(jù)提交到后才告知客戶端。為了降低操作延遲,也增加了它提交數(shù)據(jù)至日志的頻率。 本文翻譯自 write concern。 注意,本文內(nèi)容適用于MongoDB Manual 3.0.2及以上版本。 Whats write concern 所謂的寫關(guān)注就是當(dāng)向客戶端報告寫操作的成功...
摘要:容器訪問以為例,在原始的文件中,如下上面的配置,本地主機(jī)是無法訪問容器的,我們至少需要暴露出一個端口。查看,的默認(rèn)端口其實(shí)是,而這里寫成也是有原因的。 任何事情的成功都需要掐準(zhǔn)時間 上一節(jié)mongo EOF中,關(guān)于容器的配置,只是粗略的使用了Docker-Compose-MongoDB-Replica-Set項(xiàng)目提供好的docker-compose.yml文件。在使用過程中,我發(fā)現(xiàn)這個...
摘要:活動之圖文直播現(xiàn)在,在現(xiàn)場為大家發(fā)來報道大會在主持人甜美的聲音中開場,來自,作為中文社區(qū)的官方合作伙伴,是這次大會的特別支持。 11月22日,MongoDB 2014大會在北京拉開帷幕,這是一場程序員,架構(gòu)師和運(yùn)維工程師齊聚的技術(shù)盛會,會上,MongoDB頂級專家對MongoDB性能調(diào)試與擴(kuò)展、模式設(shè)計藝術(shù)、集群管理及遷移以及大規(guī)模的使用等方面做了詳細(xì)講解,有200多開發(fā)者參與了此次盛...
閱讀 3585·2023-04-26 01:43
閱讀 2972·2021-10-14 09:42
閱讀 5404·2021-09-30 09:59
閱讀 2172·2021-09-04 16:40
閱讀 1208·2019-08-30 15:52
閱讀 822·2019-08-29 17:09
閱讀 1993·2019-08-26 13:37
閱讀 3432·2019-08-26 10:20