redis迁移同步工具-redis-shake

官方文档:
https://github.com/alibaba/RedisShake/wiki/快速开始:数据迁移
下载:
https://github.com/alibaba/RedisShake/releases

redis-shake.toml 单机到单机配置,源 6383 目标端 6384

启动,增量同步

./redis-shake-linux-amd64 redis-shake.toml

--此启动方式将会 把源端所有数据增量同步到目标端, 目标端现有数据key与源端相同的情况下,会被覆盖, 不同的情况下会保留目标端现有数据,单机同步到集群时需用fifter脚本过滤 源库db到目标库db0

如有数据清洗需求,可使用filter 脚本
官方文档:https://github.com/alibaba/RedisShake/wiki/使用-filters-做数据清洗

脚本说明:
filter 脚本使用 lua 语言编写,在启动 redis-shake 的时候通过命令行传入:
./redis-shake sync.toml ../filters/print.lua
filter 脚本内需要实现一个名为 filter 的 lua 函数:

function filter(id, is_base, group, cmd_name, keys, slots, db_id, timestamp_ms)
-- write something
return 0, db_id
end

参数说明:
id:number 类型,redis-shake 会对所有数据进行编号,无意义
is_base:boolean 类型,代表这条数据是否是 dump.rdb 中的,可用于跳过存量数据,只同步增量数据。
group:string 类型,代表这条数据是什么类型的,大写,比如 STRING、HASH、LIST、SET 等
cmd_name:string 类型,代表 Redis 命令,大写,比如:HSET、XADD、LPOP 等
keys:table 类型,代表命令中的 key,这里使用 table 类型是因为有多个 key 的命令,比如 MSET。
slots:table 类型,对应 key 所属于的 slot。
db_id:number 类型,databse id。
timestamp_ms:时间戳,目前不可用。

返回值说明:
code:可选值 0,1,2
0:允许此条数据发送至对端
1:跳过此条数据
2:不应该出现此数据,立即终止 redis-shake
db_id:代表这条命令会发送到哪个 database,只在 swap.db 中使用。一般情况下与传入 db_id 保持一致即可。

示例脚本1:

---指定db5数据同步到db6
---指定db8数据同步到db9
---其他db数据不同步


function filter(id, is_base, group, cmd_name, keys, slots, db_id, timestamp_ms)
    if db_id == 5 then
        -- print("db_id is 5, redirect to 6")
        return 0, 6
    elseif db_id == 8 then
        -- print("db_id is 8, redirect to 9")    
        return 0, 9
    else
        return 1, db_id
    end
end

示例脚本2:过滤掉前缀为 ABC 的数据:

-- skip keys prefixed with ABC
function filter(id, is_base, group, cmd_name, keys, slots, db_id, timestamp_ms)
    if #keys ~= 1 then
        return 0, db_id -- allow
    end

    if string.sub(keys[1], 0, 3) == "ABC" then
    return 1, db_id -- disallow
    end

    return 0, db_id -- allow
end

示例脚本3: 过滤前缀AB并指定同步db 10 到 db 11

function filter(id, is_base, group, cmd_name, keys, slots, db_id, timestamp_ms)

    if #keys ~= 1 then
        return 0, db_id -- allow
    end

     ---           0开始截取的索引   2结束时的索引                
    if string.sub(keys[1], 0, 2) == "AB" and db_id == 10 then              
    return 0, 11 -- disallow   ---同步到db11

    else
    return 1, db_id -- allow     ---其他库都不同步
    end

end
posted @   怀里的懒猫  阅读(458)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
点击右上角即可分享
微信分享提示