“當需要進行Redis實例或集群數據遷移時,我們可以采用導出/導入的方式進行數據遷移,但當需要做數據異地災備或雙活時,再使用傳統的方式就不合適了,我們需要借助工具(如redis-port/redis-shake)來完成。
”
redis-shake介紹
redis-shake就是一款非常好用的工具,可以支持備份、恢復、解析、同步等功能,主要的功能有:
- decode,對RDB文件進行讀取,并以json格式存儲
- restore,將RDB文件恢復到目的Redis服務器
- dump,將源Redis服務器的數據通過RDB文件的方式保存下來
- sync,支持源Redis和目的Redis的數據同步,通過模擬成Slave(使用psync),支持全量和增量數據的同步,對單節點、主從、集群環境之間進行同步(2.8-5.0版本,codis環境),也支持云上環境
- rump,使用scan和restore命令進行數據遷移,對不支持psync命令的環境可以使用這種方式,僅支持全量的數據遷移
數據遷移原理
redis-shake通過模擬成一個從節點加入到源Redis,進行全量拉取并回放,然后進行增量的拉取。
其對大key的同步支持分批拉取,同時提供監控數據。
遷移演練
如從云上Redis遷移到線下Redis:
./redis-shake.linux?\
-type?sync?\
-conf?redis-shake.conf
參考配置文件:
#?id
id?=?redis-shake
#?parallel?routines?number?used?in?RDB?file?syncing.
parallel?=?4
#?source?redis?configuration.
#?used?in?`dump`?and?`sync`.
#?ip:port
source.address?=?172.17.134.223:6379
#?password.
source.password_raw?=?xxx
#?auth?type,?don't?modify?it
source.auth_type?=?auth
#?version?number,?default?is?6?(6?for?Redis?Version?<=?3.0.7,?7?for?>=3.2.0)
source.version?=?6
#?target?redis?configuration.?used?in?`restore`?and?`sync`.
#?used?in?`restore`?and?`sync`.
#?ip:port
target.address?=?10.0.2.23:6379
#?password.
target.password_raw?=?xx
#?auth?type,?don't?modify?it
target.auth_type?=?auth
#?version?number,?default?is?6?(6?for?Redis?Version?<=?3.0.7,?7?for?>=3.2.0)
target.version?=?6
#?all?the?data?will?come?into?this?db.?#?used?in?`restore`?and?`sync`.
target.db?=?-1#?big?key?threshold,?the?default?is?500?*?1024?*?1024.?The?field?of?the?big?key?will?be?split?in?processing.
big_key_threshold?=?524288000#?use?psync?command.#?used?in?`sync`.#?默認使用sync命令,啟用將會使用psync命令
psync?=?false
同時也提供遷移的狀態監控:
curl?127.0.0.1:9320/metric|jq
數據一致性校驗
遷移后數據的一致性使用redis-full-check進行檢查,通過多次對比的方式進行源端和目的端的數據校驗,每次都會抓取源和目的端的數據進行差異化比較,記錄不一致的數據(存儲在sqlite3中)進入下一輪對比,支持對key、value或兩者的一致性進行校驗。
云上服務之間我們可以使用DTS來進行,畢竟是付費服務,相對來說更簡單,需要在Redis服務上創建復制類型的賬號。