Skip to content

快速开始:数据迁移(使用 scan 模式)

suxb201 edited this page Nov 21, 2022 · 4 revisions

对于大多数 Redis 的数据迁移推荐《快速开始:数据迁移》中的方案,当云厂商出于种种考虑禁用了 Redis 的 PSync 命令时,前述方案不可用。 对于这种情况可以使用 redis-shake 的 scan 模式来进行数据迁移,原理是调用 scan 命令来获取 Redis 中的 key,然后使用 dump 命令获取 key 的内容,最终使用 restore 命令恢复 key 至目的端。


scan 模式可能会导致数据丢失:

  1. 如果某个 key 在迁移过程中一直存在,scan 模式能保证他一定被迁移
  2. 如果某个 key 在迁移过程中不是一直存在,scan 模式不保证其一定被迁移
  3. 如果某个 key 在迁移过程中被修改,scan 模式不保证修改能同步到对端

可见 scan 模式会有许多缺点,所以推荐 sync 模式,其次 restore 模式。


这里我们举几个例子,演示使用 redis-shake 的 scan 模式来迁移数据。

实例信息

单机实例 A

  • 地址:r-aaaaa.redis.zhangbei.rds.aliyuncs.com
  • 端口:6379
  • 密码:r-aaaaa:xxxxx

单机实例 B

  • 地址:r-bbbbb.redis.zhangbei.rds.aliyuncs.com
  • 端口:6379
  • 无密码

集群实例 C

  • 地址:
    • 192.168.0.1:6379
    • 192.168.0.2:6379
    • 192.168.0.3:6379
    • 192.168.0.4:6379
  • 密码:r-ccccc:xxxxx

集群实例 D

  • 地址:
    • 192.168.1.1:6379
    • 192.168.1.2:6379
    • 192.168.1.3:6379
    • 192.168.1.4:6379
    • 192.168.1.5:6379
    • 192.168.1.6:6379
  • 密码:r-ddddd:xxxxx

单机到单机:A->B

修改 scan.toml,改为如下配置:

type = "scan"

[source]
address = "r-aaaaa.redis.zhangbei.rds.aliyuncs.com:6379"
password = ""

[target]
type = "standalone"
address = "r-bbbbb.redis.zhangbei.rds.aliyuncs.com:6379"
password = "r-bbbbb:xxxxx"

启动 redis-shake:

./redis-shake scan.toml

单机到集群:A->C

修改 scan.toml,改为如下配置:

type = "scan"

[source]
address = "r-aaaaa.redis.zhangbei.rds.aliyuncs.com:6379"
password = "r-aaaaa:xxxxx"

[target]
type = "cluster"
address = "192.168.0.1:6379" # 这里写集群中的任意一个节点的地址即可
password = "r-ccccc:xxxxx"

启动 redis-shake:

./redis-shake scan.toml

集群到集群:C->D

方法1:手动起多个 redis-shake

集群 C 有四个节点:

  • 192.168.0.1:6379
  • 192.168.0.2:6379
  • 192.168.0.3:6379
  • 192.168.0.4:6379

把 4 个节点当成 4 个单机实例,参照前文部署 4 个 redis-shake 进行数据同步。

方法2:借助 cluster_helper.py 启动

脚本 cluster_helper.py 可以方便启动多个 redis-shake 从集群迁移数据,效果等同于方法1。

⚠️注意:

  • 源端有多少个分片,cluster_helper.py 就会起多少个 redis-shake 进程,所以如果源端分片数较多的时候,需要评估当前机器是否可以承担这么多进程。
  • cluster_helper.py 异常退出的时候,可能没有正常退出 redis-shake 进程,需要 ps aux | grep redis-shake 检查。
  • 每个 redis-shake 进程的执行日志记录在 RedisShake/cluster_helper/data/xxxxx 中,反馈问题请提供相关日志。

依赖

Python 需要 python3.6 及以上版本,安装 Python 依赖:

cd RedisShake/cluster_helper
pip3 install -r requirements.txt

配置

修改 scan.toml:

type = "scan"

[source]
address = "192.168.0.1:6379" # 集群 C 中任意一个节点地址
password = "r-ccccc:xxxxx"

[target]
type = "cluster"
address = "192.168.1.1:6380" # 集群 D 中任意一个节点地址
password = "r-ddddd:xxxxx"

运行

cd RedisShake/cluster_helper
python3 cluster_helper.py ../redis-shake ../scan.toml
  • 参数 1 是 redis-shake 可执行程序的路径
  • 参数 2 是配置文件路径

运行效果类似: image

Redis 哨兵模式

忽略 sentinel 节点,将哨兵拓扑当成普通的主从节点即可,参照单机到单机:A->B配置文件:

  1. source 的 address 写源端的 master 或 slave 节点地址,推荐 slave 节点。
  2. target 的 address 些目的端的 master 节点地址。