----------------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文件是一些指令命令
[root@localhost 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的方式
[root@localhost c1]# pwd
/opt/redis_cluster/data/c1
[root@localhost c1]# mv c1.aof bak_c1.aof
[root@localhost c1]# rm c1.conf
[root@localhost 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
[root@localhost 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
可以看到當前的集群是沒有數據的
[root@localhost 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
[root@localhost 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
[root@localhost 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.檢查數據恢復情況(檢查主節點上的記錄即可)
[root@localhost 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"
[root@localhost 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>
[root@localhost 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