Redis migrate 数据迁移工具 - golang
时间: 2020-08-21来源:V2EX
前景提要
Redis migrate 数据迁移工具
在工作中可能会遇到单点 Redis 向 Redis 集群迁移数据的问题,但又不能老麻烦运维来做。为了方便研发自己迁移数据,我这里写了一个简单的 Redis 迁移工具,希望对有需要的人有用。
本工具支持: 单点 Redis 到单点 Redis 迁移 单点 Redis 到 Redis 集群迁移 Redis 集群到 Redis 集群迁移 Redis 集群到单点 Redis 迁移
该工具已经编译成了多平台命令,直接从 Github 下载二进制文件执行就好了。
项目地址: https://github.com/icowan/redis-tool
把代码拉下来之后直接执行命令 make 就可以编译多个平台可执行文件,需要依赖 golang 编译器。 Windows amd64: redis-tool-windows-amd64.exe MacOS amd64: redis-tool-darwin-amd64 Linux amd64: redis-tool-linux-amd64 Linux arm64: redis-tool-linux-arm64
查看使用方法: $ chmod a+x redis-tool-linux-amd64 $ ./redis-tool-linux-amd64 -h
支持的数据类型 [x] string 字符串 [x] hash 散列列表 [x] list 列表 [x] sorted-set 有序集合
如何使用
下载好命令并授权之后执行 ./redis-tool-linux-amd64 -h 可以查看该工具所支持的所有功能: $ ./redis-tool-darwin-amd64 migrate -h 数据迁移命令 Usage: redis-tool migrate [command] Examples: 支持命令: [hash, set, sorted-set, list] Available Commands: all 迁移所有 hash 哈希列表迁移 list 列表迁移 set redis set 迁移 sorted-set 有序集合迁移 Flags: -h, --help help for migrate --source-auth string 源密码 --source-database int 源 database --source-hosts string 源 redis 地址, 多个 ip 用','隔开 (default "127.0.0.1:6379") --source-prefix string 源 redis 前缀 --source-redis-cluster 源 redis 是否是集群 --target-auth string 目标密码 --target-database int 目标 database --target-hosts string 目标 redis 地址, 多个 ip 用','隔开 (default "127.0.0.1:6379") --target-prefix string 目标 redis 前缀 --target-redis-cluster 目标 redis 是否是集群 Use "redis-tool migrate [command] --help" for more information about a command.
参数说明: --source-auth : 源 redis 密码,如果有的话就填 --source-database : 源 database,默认是 0 --source-hosts : 源 redis 地址, 集群的多个 ip 用','隔开 (default "127.0.0.1:6379") --source-prefix : 源 redis 前缀, 可不填 --source-redis-cluster : 源 redis 是否是集群, 默认 false --target-auth : 迁移目标 redis 密码,如果有的话就填 --target-database : 迁移目标 database,默认是 0 --target-hosts : 迁移目标 redis 地址, 集群的多个 ip 用','隔开 (default "127.0.0.1:6379") --target-prefix : 迁移目标 redis 前缀, 可不填 --target-redis-cluster : 迁移目标 redis 是否是集群, 默认 false
迁移单个 key 的数据
下面就举两个例子吧,其他的都差不太多。
Hash 类型
可以通过命令 redis-tool migrate hash -h 查看使用说明 $ redis-tool migrate hash helloworld \ --source-hosts 127.0.0.1:6379 \ --target-redis-cluster true \ --target-hosts 127.0.0.1:6379,127.0.0.1:7379 \ --target-auth 123456

有序集合
可以通过命令 redis-tool migrate sorted-set -h 查看使用说明
有序集合的数据量可能会比较大,所以这里按 50000 为单位进行了切割。我这里测试过迁移近 17000000 万条的数据,用时 40 多分钟。 $ redis-tool migrate hash helloworld \ --source-hosts 127.0.0.1:6379 \ --target-redis-cluster true \ --target-hosts 127.0.0.1:6379,127.0.0.1:7379 \ --target-auth 123456

迁移所有 key 的数据支持通配符过滤
可以通过命令 redis-tool migrate all -h 查看使用说明 $ redis-tool migrate all "ipdetect:*" --source-hosts 127.0.0.1:6379 --target-redis-cluster true --target-hosts 127.0.0.1:6379,127.0.0.1:7379 --target-auth 123456
这个命令会编译匹配到的所有类型的 key,再根据 key 的类型进行逐步迁移。
尾巴
使用 golang 写的一个比较简单的工具, 主要用于在 Redis 没有持久化或多套 Redis 向一套 Redis 迁移的情况下使用。
希望对大家有用,谢谢!

科技资讯:

科技学院:

科技百科:

科技书籍:

网站大全:

软件大全:

热门排行