赞
踩
最近有个需求,需要把自建的Redis数据迁移至阿里云的Redis RDS。阿里云有官方的数据传输服务DTS(Data Transmission Service)。全量迁移是免费的,但增量迁移需要按量收费,遂放弃。经过一番搜索,发现了一个RedisShake
的工具,而这个也是阿里开源的,秒啊~
RedisShake 是一个用于处理和迁移 Redis 数据的工具,它提供以下特性:
不要降低 Redis 版本迁移,比如从 6.0 降到 5.0,因为 RedisShake 每个大版本都会引入一些新的命令和新的编码方式,如果降低版本,可能会导致不兼容。
Github仓库 https://github.com/tair-opensource/RedisShake
中文文档地址 https://tair-opensource.github.io/RedisShake/
Releases版本下载路径 https://github.com/tair-opensource/RedisShake/releases/
支持多个平台
同时也支持下载源码自行编译。
首先下载 RedisShake,我们以v4.1.0
为例,系统为CenterOS
wget https://github.com/tair-opensource/RedisShake/releases/download/v4.1.0/redis-shake-linux-amd64.tar.gz
解压缩
tar -zxvf redis-shake-linux-amd64.tar.gz
会得到两个文件
redis-shake
shake.toml
shake.toml
是配置文件,TOML
语法,具体可查看文档。
修改配置
[root@~ redis-shake]# vim shake.toml
function = ""
# PSync模式
[sync_reader]
cluster = false # 是否为集群模式
address = "127.0.0.1:6379"
username = ""
password = "password1"
tls = false
sync_rdb = true # 同步全量数据
sync_aof = true # 同步增量数据
prefer_replica = true
# Scan模式
#[scan_reader]
#cluster = false
#address = "127.0.0.1:6379"
#username = ""
#password = ""
#tls = false
#dbs = [] # 指定需要同步的database
#scan = true
#ksn = false
#count = 1 # 每次扫描key的数量
# RDB 模式
# [rdb_reader]
# filepath = "/tmp/dump.rdb"
# [aof_reader]
# filepath = "/tmp/.aof"
# timestamp = 0 # subsecond
[redis_writer]
cluster = false
sentinel = false # 是否为哨兵模式
master = ""
address = "192.168.1.2:6379"
username = ""
password = "password2"
tls = false
off_reply = false
[advanced]
dir = "data"
ncpu = 3 # CPU 核心数, 0表示不受限制
pprof_port = 0 # pprof访问端口, 0表示禁用
status_port = 0 # 状态检测访问端口, 0表示禁用
# log
log_file = "shake.log"
log_level = "info" # debug, info, warn 可选
log_interval = 5
# panic: 停止迁移.
# rewrite: 覆盖相同的key
# ignore: 跳过相同的key.
rdb_restore_command_behavior = "rewrite"
# 数据传输管道的大小限制.
pipeline_count_limit = 1024
# 客户端查询缓冲区大小,默认 1gb.
target_redis_client_max_querybuf_len = 1024_000_000
# In the Redis protocol, bulk requests, that are, elements representing single
# strings, are normally limited to 512 mb.
target_redis_proto_max_bulk_len = 512_000_000
# 针对AWS的Elasticache或MemoryDB的配置
aws_psync = ""
# 是否在传输前先清空目标数据
empty_db_before_sync = false
[module]
# BF.LOADCHUNK
target_mbbloom_version = 20603
配置文件修改好后保存,再运行
./redis-shake shake.toml
执行后会在同目录生成一个data
的临时目录,日志文件也会在此目录保存,还有aof
文件.
[root@~ data]# ls -lrt
total 3052
-rw------- 1 root root 0 Jun 19 17:32 pid.lockfile
drwxr-xr-x 2 root root 4096 Jun 19 19:30 reader_127.0.0.1_6379
-rw-r--r-- 1 root root 3116030 Jun 20 20:23 shake.log
同步日志如下
[root@~ redis-shake]# ./redis-shake shake.toml
2024-06-19 18:29:55 INF load config from file: shake.toml
2024-06-19 18:29:55 INF log_level: [info], log_file: [/home/redis-shake/data/shake.log]l
2024-06-19 18:29:55 INF changed work dir. dir=[/home/redis-shake/data]
2024-06-19 18:29:55 INF set ncpu to 3
2024-06-19 18:29:55 INF set GOMAXPROCS to 3
2024-06-19 18:29:55 INF not set pprof port
2024-06-19 18:29:55 INF create ScanStandaloneReader: 127.0.0.1:6379
2024-06-19 18:29:55 INF create RedisStandaloneWriter: 192.168.1.2:6379
2024-06-19 18:29:55 INF not set status port
2024-06-19 18:30:00 INF start syncing...
2024-06-19 18:30:05 INF read_count=[6866], read_ops=[869.87], write_count=[6865], write_ops=[869.87], scan_dbid=[0], scan_percent=[5.61%], need_update_count=[99884]
2024-06-19 18:30:10 INF read_count=[10815], read_ops=[601.65], write_count=[10814], write_ops=[601.65], scan_dbid=[0], scan_percent=[5.82%], need_update_count=[99753]
2024-06-19 18:30:15 INF read_count=[13557], read_ops=[622.26], write_count=[13556], write_ops=[622.26], scan_dbid=[0], scan_percent=[5.96%], need_update_count=[99872]
2024-06-19 18:30:20 INF read_count=[18269], read_ops=[852.18], write_count=[18268], write_ops=[852.18], scan_dbid=[0], scan_percent=[6.21%], need_update_count=[99887]
2024-06-19 18:30:25 INF read_count=[22062], read_ops=[502.66], write_count=[22061], write_ops=[502.66], scan_dbid=[0], scan_percent=[6.40%], need_update_count=[99878]
2024-06-19 18:30:30 INF read_count=[25226], read_ops=[829.09], write_count=[25225], write_ops=[829.09], scan_dbid=[0],] scan_percent=[6.57%], need_update_count=[99801]
... ...
2024-06-19 18:31:45 INF read_count=[94883], read_ops=[935.01], write_count=[94882], write_ops=[935.01], scan_dbid=[0], scan_percent=[10.18%], need_update_count=[99897]
2024-06-19 18:31:50 INF read_count-[100268], read_ops=[1245.28], write_count=[100267], write_ops-[1245.28], scan_dbid-[0], scan_percent=[10.46%], need_update_count-[99757]
2024-06-19 18:31:55 INF read_count=[104976], read_ops=[738.58], write_count=[104975], write_ops=[738.58], scan_dbid=[0], scan_percent=[10.70%], need_update_count=[99880]
2024-06-19 18:32:00 INF read_count=[109798], read_ops=[994.45], write_count=[109797], write_ops=[994.45], scan_dbid=[0], scan_percent=[10.95%], need_update_count=[99769]
同步时不能结束进。同步完全量数据后,开始同步增量数据。
2024-06-19 20:22:46 INF read_count=[1989683], read_ops=[1.00], write_count=[1989683], write_ops=[1.00], syncing aof, diff=[0]
这时可以正常切换Redis了。切换成功后等read_ops及write_ops为0时,就可以把自建的redis关闭了。
RedisShake
确实很强大,使用时几乎不影响原有的业务稳定性。至于其他的模式我没有测试,大家可以尝试一下,好用的话帮忙点个赞,谢谢~
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。