redis恢复(aof)
Posted 东南风
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了redis恢复(aof)相关的知识,希望对你有一定的参考价值。
----------------redis备份恢复方法-----------------------------
1.采用aof恢复方法
若appendonly设置为no的情况下,在每个节点上执行bgrewriteaof命令生成一个aof文件
若appendonly已经为yes的话,在data目录会自动生成有aof文件
我这里是appendonly设置为no的,所以在每个节点上执行如下命令
./redis-cli -c -h 192.168.1.118 -p 1001
./redis-cli -c -h 192.168.1.118 -p 1002
./redis-cli -c -h 192.168.1.118 -p 1003
./redis-cli -c -h 192.168.1.85 -p 2001
./redis-cli -c -h 192.168.1.85 -p 2002
./redis-cli -c -h 192.168.1.85 -p 2003
如下:
192.168.1.118:1001> bgrewriteaof
Background append only file rewriting started
要是数据量很大的话,生成aof文件需要些时间
生成的aof文件是一些指令命令
[[email protected] c3]# more c3.aof
*2
$6
SELECT
$1
0
*3
$3
SET
$6
name15
$15
huangxueliang15
*3
$3
SET
$6
name25
$15
huangxueliang25
*3
$3
SET
$6
name18
$15
huangxueliang18
执行以上脚本后,在每个节点的data目录下都会生成一个aof文件
2.停掉现有的集群
192.168.1.118
./redis-cli -c -h 192.168.1.118 -p 1001 shutdown
./redis-cli -c -h 192.168.1.118 -p 1002 shutdown
./redis-cli -c -h 192.168.1.118 -p 1003 shutdown
192.168.1.85
./redis-cli -c -h 192.168.1.85 -p 2001 shutdown
./redis-cli -c -h 192.168.1.85 -p 2002 shutdown
./redis-cli -c -h 192.168.1.85 -p 2003 shutdown
3.初始化集群(删除data目录下的相应文件)
A.cluster-config-file指定的文件
B.data目录下的aof文件,文件我们不能删除,可以重命名的方式
C.data目录下的rdb文件
我这里是采用mv的方式
[[email protected] c1]# pwd
/opt/redis_cluster/data/c1
[[email protected] c1]# mv c1.aof bak_c1.aof
[[email protected] c1]# rm c1.conf
[[email protected] c1]# mv dump-c1.rdb bak_dump-c1.rdb
4.初始化集群(修改集群各节点端口号)
192.168.1.118(c1.conf,c2.conf,c3.conf)
1001->7001
1002->7002
1003->7003
192.168.1.85(c1.conf,c2.conf,c3.conf)
2001->8001
2002->8002
2003->8003
5.初始化集群(启动各节点)
192.168.1.118
/opt/redis_cluster/bin/redis-server /opt/redis_cluster/conf/c1.conf
/opt/redis_cluster/bin/redis-server /opt/redis_cluster/conf/c2.conf
/opt/redis_cluster/bin/redis-server /opt/redis_cluster/conf/c3.conf
192.168.1.85
/opt/redis_cluster/bin/redis-server /opt/redis_cluster/conf/c1.conf
/opt/redis_cluster/bin/redis-server /opt/redis_cluster/conf/c2.conf
/opt/redis_cluster/bin/redis-server /opt/redis_cluster/conf/c3.conf
5.初始化集群(创建集群)
/opt/redis_cluster/redis-trib.rb create --replicas 1 192.168.1.118:7001 192.168.1.118:7002 192.168.1.118:7003 192.168.1.85:8001 192.168.1.85:8002 192.168.1.85:8003
[[email protected] redis_cluster]# ./redis-trib.rb check 192.168.1.118:7001
>>> Performing Cluster Check (using node 192.168.1.118:7001)
M: 9af2a4297106fd62ca577946781f476b44bbd170 192.168.1.118:7001
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: e2b286b2ce65b2afa4d9afabc9fbadb4106824d3 192.168.1.118:7002
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: fffaad68a73c0fe62e190c1bb39d92cdcb424eab 192.168.1.85:8003
slots: (0 slots) slave
replicates e2b286b2ce65b2afa4d9afabc9fbadb4106824d3
M: 68783efacb82dc3a52570f593aaa2fe40fb6623e 192.168.1.85:8001
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 57a7b5eb8e1612bac7f16bcf7e6d0df9a4c5e3a0 192.168.1.85:8002
slots: (0 slots) slave
replicates 9af2a4297106fd62ca577946781f476b44bbd170
S: 078dc90bbe8d55b6aa6b321704cbe6baa8394505 192.168.1.118:7003
slots: (0 slots) slave
replicates 68783efacb82dc3a52570f593aaa2fe40fb6623e
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered
可以看到当前的集群是没有数据的
[[email protected] bin]# ./redis-cli -c -h 192.168.1.118 -p 7001
192.168.1.118:7001> keys *
(empty list or set)
6.数据恢复
步骤3的时候每个节点我们都备份了一个aof文件,那么我们采用该文件进行恢复
192.168.1.118:7001
./redis-cli -h 192.168.1.118 -p 7001 --pipe < /opt/redis_cluster/data/c1/bak_c1.aof
All data transferred. Waiting for the last reply...
Last reply received from server.
errors: 0, replies: 11
这里导入了11条记录,也就是原来节点的数据
192.168.1.118:7002
./redis-cli -h 192.168.1.118 -p 7002 --pipe < /opt/redis_cluster/data/c2/bak_c2.aof
192.168.1.118:7003
[[email protected] bin]# ./redis-cli -h 192.168.1.118 -p 7003 --pipe < /opt/redis_cluster/data/c3/bak_c3.aof
All data transferred. Waiting for the last reply...
MOVED 10325 192.168.1.85:8001
MOVED 6586 192.168.1.85:8001
MOVED 7702 192.168.1.85:8001
MOVED 10767 192.168.1.85:8001
MOVED 6888 192.168.1.85:8001
MOVED 7012 192.168.1.85:8001
MOVED 6462 192.168.1.85:8001
MOVED 10589 192.168.1.85:8001
MOVED 7368 192.168.1.85:8001
MOVED 6764 192.168.1.85:8001
MOVED 8090 192.168.1.85:8001
MOVED 5878 192.168.1.85:8001
MOVED 7244 192.168.1.85:8001
MOVED 10713 192.168.1.85:8001
MOVED 10891 192.168.1.85:8001
MOVED 7966 192.168.1.85:8001
Last reply received from server.
errors: 16, replies: 17
[[email protected] bin]#
发现该节点是从节点,不需要导入数据,从上面的配置信息发现也是报错
以下操作发现是从节点的也会报上面的错误
192.168.1.85:8001
./redis-cli -h 192.168.1.85 -p 8001 --pipe < /opt/redis_cluster/data/c1/bak_c1.aof
192.168.1.85:8002
./redis-cli -h 192.168.1.85 -p 8002 --pipe < /opt/redis_cluster/data/c2/bak_c2.aof
192.168.1.85:8003
./redis-cli -h 192.168.1.85 -p 8003 --pipe < /opt/redis_cluster/data/c3/bak_c3.aof
其实恢复过程只需要主节点上的aof文件即可
7.检查数据恢复情况(检查主节点上的记录即可)
[[email protected] bin]# ./redis-cli -h 192.168.1.118 -p 7001
192.168.1.118:7001> keys *
1) "name40"
2) "name22"
3) "name31"
4) "name26"
5) "name35"
6) "name08"
7) "name04"
8) "name17"
9) "name13"
10) "name39"
[[email protected] bin]# ./redis-cli -h 192.168.1.118 -p 7002
192.168.1.118:7002> keys *
1) "name10"
2) "name03"
3) "name19"
4) "name25"
5) "name06"
6) "name14"
7) "name36"
8) "name02"
9) "name07"
10) "name29"
11) "name21"
12) "name32"
13) "name15"
14) "name18"
15) "name11"
192.168.1.118:7002>
[[email protected] bin]# ./redis-cli -h 192.168.1.85 -p 8001
192.168.1.85:8001> keys *
1) "name33"
2) "name30"
3) "name34"
4) "name23"
5) "name01"
6) "name05"
7) "name09"
8) "name20"
9) "name12"
10) "name24"
11) "name37"
12) "name27"
13) "name16"
14) "name28"
15) "name38"
可以看到数据以及恢复,原来集群里的数据是name1-name40
以上是关于redis恢复(aof)的主要内容,如果未能解决你的问题,请参考以下文章