26

Redis migrate 数据迁移工具

 3 years ago
source link: https://mp.weixin.qq.com/s/wC9ppYaVjFfX2wQWYs3HsA
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Redis migrate 数据迁移工具

原创 嘟噜聪 薛定谔的猿 1周前

在工作中可能会遇到单点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

支持的数据类型

  • string 字符串
  • hash 散列列表
  • list 列表
  • 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
640?wx_fmt=jpeg

可以通过命令 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
640?wx_fmt=jpeg

迁移所有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迁移的情况下使用。

希望对大家有用,谢谢!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK