簡單來講,就是把自已假裝成 slave, 欺騙master來達到數據流同步的目地。mysql
發送sync命令->接收rdb->解析rdb->過濾->回放rdb->回放master推送的同步數據。git
1. 同步時有兩個 redis 參數須要注意。github
repl-backlog-size redis
同步buffer的大小,默認1mb,根據當前數據量大小適當調整,好比10mb。sql
client-output-buffer-limit併發
client slave 級別的buffer也要調整,好比 client-output-buffer-limit slave 256mb 128mb 60 。工具
2. restore 操做只要目標集羣存在指定Key,就會fatal,若是沒問題能夠在代碼中去掉err檢測。spa
3. 使用假裝slave的機制,redis-port必定要輪流同步,同時bgsave可很差玩。代理
4. 代碼修改爲通用的工具,每次改代碼邏輯容易出問題,from和target必定要確認好。rest
5. 同步時因爲源數據量太大,可能工具會中斷,調整redis-port併發數和redis上面提到的兩個buffer就好。
安裝
1、安裝redis-port
下載地址: https://github.com/CodisLabs/redis-port/releases
# go get github.com/CodisLabs/redis-port/releases
默認會下載在/usr/local/src/github.com/CodisLabs下,或者能夠在github.com上下載redis-port的新版本。
安裝依賴
https://github.com/cupcake/rdb
# go get github.com/cupcake/rdb
將下載的版本解壓重命名爲redis-port後,放到codis安裝的同級目錄下,例如個人codis安裝在
/usr/local/go/src/github.com/CodisLabs,那麼將redis-port放到這個目錄下便可。
# cd /usr/local/go/src/github.com/CodisLabs/redis-port/ # make # ll /usr/local/go/src/github.com/CodisLabs/redis-port/bin/ total 4920 -rwxr-xr-x. 1 root root 5037040 May 12 11:08 redis-port
redis-port參數說明:
-n N, --ncpu=N
set runtime.GOMAXPROCS to N
-p M, --parallel=M
set number of parallel routines
-i INPUT, --input=INPUT
use INPUT as input file, or if it is not given, redis-port reads from stdin (means '/dev/stdin')
-o OUTPUT, --output=OUTPUT
use OUTPUT as output file, or if it is not given, redis-port writes to stdout (means '/dev/stdout')
-m MASTER, --master=MASTER
specify the master redis
-t TARGET, --target=TARGET
specify the slave redis (or target redis)
-P PASSWORD, --password=PASSWORD
specify the redis auth password
-A AUTH, --auth=AUTH
specify the auth password for target
-e, --extra
dump or restore following redis backlog commands
--redis
target is normal redis instance, default value is false
--codis
target is codis proxy, default value is true
--filterdb=DB
filter specifed db number, default value is '*'
DECODE dumped payload to human readable format (hex-encoding)
redis-port decode [--ncpu=N] [--parallel=M] [--input=INPUT] [--output=OUTPUT]
RESTORE rdb file to target redis
redis-port restore [--ncpu=N] [--parallel=M] [--input=INPUT] --target=TARGET [--codis|--redis] [--auth=AUTH] [--extra] [--faketime=FAKETIME] [--filterdb=DB]
DUMP rdb file from master redis
redis-port dump [--ncpu=N] [--parallel=M] --from=MASTER [--password=PASSWORD] [--output=OUTPUT] [--extra]
SYNC data from master to slave
redis-port sync [--ncpu=N] [--parallel=M] --from=MASTER [--password=PASSWORD] [--psync]
[--filterdb=DB] --target=TARGET [--auth=AUTH] [--codis|--redis] [--sockfile=FILE [--filesize=SIZE]]
2、使用redis-port
1)同步原生redis 192.168.34.6:7000 到codis 192.168.34.6:19000,目標是 codis-proxy。
# ./redis-port sync --from=192.168.34.6:7000 --codis --target=192.168.34.6:19000 -n 1
2015/05/12 17:54:11 [INFO] set ncpu = 1, parallel = 1
2015/05/12 17:54:11 [INFO] sync from '192.168.34.6:7000' to '192.168.34.6:19000'
2015/05/12 17:54:11 [INFO] rdb file = 345
2015/05/12 17:54:11 [INFO] total=345 - 345 [100%] entry=38
2015/05/12 17:54:11 [INFO] sync rdb done
2015/05/12 17:54:12 [INFO] sync: +forward=0 +nbypass=0 +nbytes=0
2015/05/12 17:54:13 [INFO] sync: +forward=0 +nbypass=0 +nbytes=0
2015/05/12 17:54:14 [INFO] sync: +forward=0 +nbypass=0 +nbytes=14
2)使用redis-port decode redis/codis rdb文件。
# cat /test/dump.rdb |./redis-port decode 2> /dev/null
{"db":0,"type":"string","key":"a","value":"hello"}
{"db":1,"type":"string","key":"a","value":"9"}
{"db":0,"type":"hash","key":"c","field":"hello","value":"world"}
{"db":0,"type":"expire","key":"c","expireat":1487663341422}
{"db":0,"type":"list","key":"b","index":0,"value":"hello"}
{"db":0,"type":"list","key":"b","index":1,"value":"world"}
將rdb文件decode到文件中。
# cat dump99.rdb |./redis-port decode -o save99.log -n 1 2> /dev/null
3)使用redis-port restore rdb文件到codis。
# ./redis-port restore -i dump.rdb --codis --target=192.168.34.6:19000 -n 1
2015/05/13 12:01:29 [INFO] set ncpu = 1, parallel = 1
2015/05/13 12:01:29 [INFO] restore from '/test/dump.rdb' to '192.168.34.6:19000'
2015/05/13 12:01:29 [INFO] total = 363 - 363 [100%] entry=40
2015/05/13 12:01:29 [INFO] restore: rdb done
將redis實例中的rdb文件轉成其餘格式文件。
#./redis-port dump -f 192.168.34.6:7000 | tee save.rdb | ./redis-port decode -o save.log -n 8 2>/dev/null
2017/02/22 14:30:17 main.go:178: [INFO] set ncpu = 1, parallel = 1
2017/02/22 14:30:17 dump.go:29: [INFO] dump from '192.168.34.6:7000' to '/dev/stdout'
2017/02/22 14:30:17 main.go:178: [INFO] set ncpu = 1, parallel = 1
2017/02/22 14:30:17 decode.go:45: [INFO] decode from '/dev/stdin' to 'save.log'
2017/02/22 14:30:17 dump.go:42: [INFO] rdb file = 147
2017/02/22 14:30:17 dump.go:94: [INFO] total = 147 - 147 [100%]
2017/02/22 14:30:17 dump.go:96: [INFO] dump: rdb done
2017/02/22 14:30:17 decode.go:114: [INFO] decode: total = 147 write=325 entry=5
2017/02/22 14:30:17 decode.go:116: [INFO] decode: done
4)使用redis-port dump codis-server生成rdb 文件。
# ./redis-port dump -f 192.168.34.6:6379 -o dump88.rdb
2015/05/13 12:10:58 [INFO] set ncpu = 1, parallel = 1
2015/05/13 12:10:58 [INFO] dump from '192.168.34.6:6379' to 'dump88.rdb'
2015/05/13 12:10:58 [INFO] rdb file = 363
2015/05/13 12:10:58 [INFO] total = 363 - 363 [100%]
2015/05/13 12:10:58 [INFO] dump: rdb done
redis-port使用
(1)DECODE
解析rdb文件。
redis-port decode [--ncpu=N] [--input=INPUT] [--output=OUTPUT]
(2)RESTORE
恢復rdb文件導入到codis的代理中
redis-port restore [--ncpu=N] [--input=INPUT] --target=TARGET [--auth=AUTH] [--extra] [--faketime=FAKETIME] [--filterdb=DB]
(3)DUMP
從指定的redis實例中導出爲rdb文件
redis-port dump [--ncpu=N] --from=MASTER [--password=PASSWORD] [--output=OUTPUT] [--extra]
(4)SYNC
將一個redis實例中的數據同步到另外一個redis實例中,或者同步到codis的代理中
redis-port sync [--ncpu=N] --from=MASTER [--password=PASSWORD] --target=TARGET [--auth=AUTH] [--sockfile=FILE [--filesize=SIZE]] [--filterdb=DB] [--psync]
參考:
https://github.com/wandoulabs/redis-port