关于redis:Redis-migrate-数据迁移工具

22次阅读

共计 2455 个字符,预计需要花费 7 分钟才能阅读完成。

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

有序汇合

能够通过命令 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 迁徙的状况下应用。

心愿对大家有用,谢谢!

作者:宜信技术学院 王聪

正文完
 0