客戶要將生產(chǎn)環(huán)境上一套副本集架構(gòu)的 MongoDB 進(jìn)行遷移,數(shù)據(jù)量 240GB 左右。經(jīng)過測(cè)試,全量備份耗時(shí) 3.5 小時(shí),恢復(fù)耗時(shí) 4.5小時(shí)。
為了減少割接時(shí)間,采取全量 + 增量 Oplog 的遷移方式。提前一天進(jìn)行全備,割接當(dāng)天只需備份增量的 Oplog 恢復(fù)即可,可大幅減少割接窗口。
檢查并評(píng)估生產(chǎn)環(huán)境 Oplog 的產(chǎn)生信息,以防全量和增量備份期間產(chǎn)生的 Oplog 被覆蓋掉。
mongo> db.getReplicationInfo(){"logSizeMB" : 20480,"usedMB" : 20374.38,"timeDiff" : 7074665,"timeDiffHours" : 1965.18,"tFirst" : "Fri Feb 24 2023 18:36:32 GMT+0800 (CST)","tLast" : "Wed May 17 2023 15:47:37 GMT+0800 (CST)","now" : "Wed May 17 2023 15:47:43 GMT+0800 (CST)"}
可以看出在 1965.18h 的運(yùn)行中,產(chǎn)生了 20374.38MB 大小的 Oplog。
全量備份并拷貝備份期間產(chǎn)生的 Oplog 用來增量還原。
#!/bin/bashuser=adminpassword=123host=127.0.0.1port=27017outputdir=/data/mongobak_`date +%F`authenticationdatabase=adminstart_time=`date +%s`mongodump -u$user --host $host --port $port -p$password --authenticationDatabase $authenticationdatabase --oplog --gzip -o $outputdirstop_time=`date +%s`duration=$((stop_time-start_time)) echo "Spend times: $duration seconds"
利用全備進(jìn)行數(shù)據(jù)恢復(fù)。
#!/bin/bashstart_time=`date +%s`user=adminpassword=123host=127.0.0.1port=27017authenticationdatabase=adminmongorestore -u$user --host $host --port $port -p$password --authenticationDatabase $authenticationdatabase --oplogReplay --gzip /data/mongobak_2023-07-17stop_time=`date +%s`duration=$((stop_time-start_time)) echo "Spend times: $duration seconds"
全備備份出來的 Oplog,可以利用 bsondump 工具將 bson 轉(zhuǎn)換為 json 格式,查看備份時(shí)間產(chǎn)生的最后的 Oplog 的時(shí)間戳,根據(jù)此時(shí)間戳來進(jìn)行增量的 Oplog 備份。
shell> cd /data/ mongobak_2023-07-17shell> mv oplog.bson oplog.bson.gzshell> gzip -d oplog.bson.gzshell> bsondump --pretty oplog.bson > op.json
查看 op.json 文件,找出增量備份開始的時(shí)間點(diǎn)。
"ts": { "$timestamp": { "t": 1686669429, "i": 4 }},
備份 Oplog(時(shí)間戳大于上一次全備結(jié)束時(shí)的時(shí)間)。
#!/bin/bashuser=adminpassword=123host=127.0.0.1port=27017outputdir=/tmp/oplog_`date +%F`authenticationdatabase=adminstart_time=`date +%s`mongodump -u$user --host $host --port $port -p$password --authenticationDatabase $authenticationdatabase -d local -c oplog.rs -q '{"ts":{"$gt": {"$timestamp":{"t":1686669429, "i":4}}}}' -o $outputdirstop_time=`date +%s`duration=$((stop_time-start_time)) echo "Spend times: $duration seconds"
#!/bin/bashuser=adminpassword=123host=127.0.0.1port=27017authenticationdatabase=adminstart_time=`date +%s`mongorestore -u$user --host $host --port $port -p$password --authenticationDatabase $authenticationdatabase --oplogReplay /data/oplog_2023-07-17stop_time=`date +%s`duration=$((stop_time-start_time)) echo "Spend times: $duration seconds"
分別在源端和目標(biāo)端運(yùn)行腳本,檢查遷移完成后業(yè)務(wù)數(shù)據(jù)庫下文檔數(shù)量是否一致。
#!/bin/bashuser=adminpassword=123host=127.0.0.1port=27017authenticationdatabase=adminmpid=`pidof mongod`tooldir=`dirname $(ls -l /proc/$mpid/exe | awk '{print $11}')`database=$(echo "show dbs" | $tooldir/mongo -uadmin --host $host --port $port -p$password --authenticationDatabase $authenticationdatabase --quiet |awk '{print $1}'| sed -E '/^admin$|^config$|^local$/d')for db in $databasedo collections=$(echo -e "use $db/n show collections" | $tooldir/mongo -u $user --host $host --port $port -p $password $authenticationdatabase --quiet | sed '/switched to db/d') for table in $collections do count=$(echo -e "use $db/n db.$table.count()" | $tooldir/mongo -u $user --host $host --port $port -p $password --authenticationDatabase $authenticationdatabase --quiet | sed '/switched to db/d') echo "$db.$table have $count documents" donedone
圖片
圖片
本文鏈接:http://www.www897cc.com/showinfo-26-34896-0.html一則 MongoDB 副本集遷移實(shí)操案例
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。郵件:2376512515@qq.com
上一篇: 如何用 AI 做好會(huì)議紀(jì)要?看這一篇就夠了!
下一篇: 六種常見負(fù)載均衡算法