使用RedisShake迁移redis
最近要迁移一个接口服务,后端是java程序,一开始我就想,这个迁移很简单呀,直接复制整个目录,开通mysql访问权限,启动程序,修改前端代理完成,我迁移前看了服务器上有哪些进程正在允许,使用 netstat -nlpt
看到了redis进程,卧草,我祈祷程序配置里使用的缓存是云缓存,结果看配置里的缓存IP是本地IP,这就非常的坑了,听运营部门说,迁移后会加功能访问会翻倍。沃日,这不是前人留下的坑我正好踩到吗?
迁移前还得看下本地redis有哪些人连接
redis-cli -h 127.0.0.1 -p 6379 -a xxxx CLIENT LIST
还好,就本地程序连接,也看了redis 就600M的数据。
这次我迁移redis 直接把redis 迁移到云缓存上,这样就不用维护redis,又能减少维护工作量,机智的我😄。
这次迁移使用的是 RedisShake迁移,版本时v3.1.10
可能更新版本后,以下文档不正确了。
这次迁移是单节点到主从,同时但节点到集群也可以使用。
示例IP:
-
单节点IP:10.33.x.x
-
云缓存IP:10.x.x.x
首先下载 RedisShake
➜ wget https://github.com/tair-opensource/RedisShake/releases/download/v3.1.10/redis-shake-linux-amd64.tar.gz
--2023-05-29 10:42:28-- https://github.com/tair-opensource/RedisShake/releases/download/v3.1.10/redis-shake-linux-amd64.tar.gz
Resolving github.com (github.com)... 20.205.243.166
Connecting to github.com (github.com)|20.205.243.166|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://objects.githubusercontent.com/github-production-release-asset-2e65be/173063554/366a42a9-557a-4276-acb4-bb5a3e2a51bb?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20230529%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20230529T024229Z&X-Amz-Expires=300&X-Amz-Signature=23e987a510a2a4973c4d6078a5cfa9ec92484d830a8e8c5e83df770639838113&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=173063554&response-content-disposition=attachment%3B%20filename%3Dredis-shake-linux-amd64.tar.gz&response-content-type=application%2Foctet-stream [following]
--2023-05-29 10:42:29-- https://objects.githubusercontent.com/github-production-release-asset-2e65be/173063554/366a42a9-557a-4276-acb4-bb5a3e2a51bb?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20230529%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20230529T024229Z&X-Amz-Expires=300&X-Amz-Signature=23e987a510a2a4973c4d6078a5cfa9ec92484d830a8e8c5e83df770639838113&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=173063554&response-content-disposition=attachment%3B%20filename%3Dredis-shake-linux-amd64.tar.gz&response-content-type=application%2Foctet-stream
Resolving objects.githubusercontent.com (objects.githubusercontent.com)... 185.199.109.133, 185.199.110.133, 185.199.111.133, ...
Connecting to objects.githubusercontent.com (objects.githubusercontent.com)|185.199.109.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 5107541 (4.9M) [application/octet-stream]
Saving to: ‘redis-shake-linux-amd64.tar.gz’
100%[=============================================================================================================>] 5,107,541 --.-K/s in 0.1s
2023-05-29 10:42:29 (45.0 MB/s) - ‘redis-shake-linux-amd64.tar.gz’ saved [5107541/5107541]
解压
➜ tar -xf redis-shake-linux-amd64.tar.gz
➜ ll
drwxr-xr-x 2 1001 docker 4.0K Mar 2 14:58 cluster_helper
drwxr-xr-x 2 1001 docker 4.0K Mar 2 14:58 filters
-rwxr-xr-x 1 1001 docker 9.0M Mar 2 14:58 redis-shake
-rw-r--r-- 1 root root 4.9M Mar 2 14:58 redis-shake-linux-amd64.tar.gz
-rw-r--r-- 1 1001 docker 1.9K Mar 2 14:58 restore.toml
-rw-r--r-- 1 1001 docker 1.9K Mar 2 14:58 scan.toml
-rw-r--r-- 1 1001 docker 2.0K Mar 2 14:58 sync.toml
我们只需要修改sync.toml 文件里部分即可。
➜ vim sync.toml
# 这里是源 redis,username直接为空
[source]
version = 3.2 # redis version, such as 2.8, 4.0, 5.0, 6.0, 6.2, 7.0, ...
address = "10.33.x.x:6379"
username = "" # keep empty if not using ACL
password = "xxxxx" # keep empty if no authentication is required
tls = false
elasticache_psync = "" # using when source is ElastiCache. ref: https://github.com/alibaba/RedisShake/issues/373
# 这里是目标redis,
[target]
type = "standalone" # "standalone" or "cluster"
version = 4.0 # redis version, such as 2.8, 4.0, 5.0, 6.0, 6.2, 7.0, ...
# When the target is a cluster, write the address of one of the nodes.
# redis-shake will obtain other nodes through the `cluster nodes` command.
address = "10.x.x.x:6380"
username = "" # keep empty if not using ACL
password = "xxxxx" # keep empty if no authentication is required
tls = false
直接执行
➜ ./redis-shake sync.toml
2023-05-29 11:03:35 INF GOOS: linux, GOARCH: amd64
2023-05-29 11:03:35 INF Ncpu: 4, GOMAXPROCS: 4
2023-05-29 11:03:35 INF pid: 8121
2023-05-29 11:03:35 INF pprof_port: 0
2023-05-29 11:03:35 INF No lua file specified, will not filter any cmd.
2023-05-29 11:03:35 INF auth successful. address=[10.x.x.x:6380]
2023-05-29 11:03:35 INF redisWriter connected to redis successful. address=[127.0.0.1:6380]
2023-05-29 11:03:35 INF auth successful. address=[10.33.x.x:6379]
2023-05-29 11:03:35 INF psyncReader connected to redis successful. address=[127.0.0.1:6379]
2023-05-29 11:03:35 INF start save RDB. address=[10.33.x.x:6379]
2023-05-29 11:03:35 INF send [replconf listening-port 10007]
2023-05-29 11:03:35 INF send [PSYNC ? -1]
2023-05-29 11:03:35 INF receive [FULLRESYNC b894fd187235fedd2ec04274d617f2d89b5d16bc 0]
2023-05-29 11:03:35 INF source db is doing bgsave. address=[10.33.x.x:6379]
2023-05-29 11:03:35 INF source db bgsave finished. timeUsed=[0.06]s, address=[127.0.0.1:6379]
2023-05-29 11:03:35 INF received rdb length. length=[25425]
2023-05-29 11:03:35 INF create dump.rdb file. filename_path=[dump.rdb]
2023-05-29 11:03:35 INF save RDB finished. address=[10.33.x.x:6379], total_bytes=[25425]
2023-05-29 11:03:35 INF start send RDB. address=[10.33.x.x:6379]
2023-05-29 11:03:35 INF RDB version: 10
2023-05-29 11:03:35 INF RDB AUX fields. key=[redis-ver], value=[7.0.5]
2023-05-29 11:03:35 INF RDB AUX fields. key=[redis-bits], value=[64]
2023-05-29 11:03:35 INF RDB AUX fields. key=[ctime], value=[1685329415]
2023-05-29 11:03:35 INF RDB AUX fields. key=[used-mem], value=[1381224]
2023-05-29 11:03:35 INF RDB repl-stream-db: 0
2023-05-29 11:03:35 INF RDB AUX fields. key=[repl-id], value=[b894fd187235fedd2ec04274d617f2d89b5d16bc]
2023-05-29 11:03:35 INF RDB AUX fields. key=[repl-offset], value=[0]
2023-05-29 11:03:35 INF RDB AUX fields. key=[aof-base], value=[0]
2023-05-29 11:03:35 INF RDB resize db. db_size=[1], expire_size=[0]
2023-05-29 11:03:35 INF send RDB finished. address=[10.33.x.x:6379], repl-stream-db=[0]
2023-05-29 11:03:35 INF start save AOF. address=[10.33.x.x:6379]
2023-05-29 11:03:35 INF AOFWriter open file. filename=[0.aof]
2023-05-29 11:03:36 INF AOFReader open file. aof_filename=[0.aof]
2023-05-29 11:03:40 INF syncing aof. allowOps=[0.40], disallowOps=[0.00], entryId=[1], InQueueEntriesCount=[0], unansweredBytesCount=[0]bytes, diff=[0], aofReceivedOffset=[14], aofAppliedOffset=[14]
2023-05-29 11:03:45 INF syncing aof. allowOps=[0.00], disallowOps=[0.00], entryId=[1], InQueueEntriesCount=[0], unansweredBytesCount=[0]bytes, diff=[0], aofReceivedOffset=[14], aofAppliedOffset=[14]
2023-05-29 11:03:50 INF syncing aof. allowOps=[0.20], disallowOps=[0.00], entryId=[2], InQueueEntriesCount=[0], unansweredBytesCount=[0]bytes, diff=[0], aofReceivedOffset=[28], aofAppliedOffset=[28]
2023-05-29 11:03:55 INF syncing aof. allowOps=[0.00], disallowOps=[0.00], entryId=[2], InQueueEntriesCount=[0], unansweredBytesCount=[0]bytes, diff=[0], aofReceivedOffset=[28], aofAppliedOffset=[28]
出现 syncing aof 说明已经同步完成了,正在进行增量同步。要真正的全量同步,要把源程序停止2-3分钟,然后启动目标程序。