当前位置:网站首页>Redis主从实现10秒检查与恢复
Redis主从实现10秒检查与恢复
2022-07-01 17:48:00 【星哥玩云】
Redis主从架构
问题:redis down重启后,能恢复key值,时间会有延迟,down机之间的值将会丢失。
实验环境,主从做在一台服务器上,利用不同端口。
解决方案:redis主从至今还不算完善,我们利用redis主从主要做redis备份,master宕机后,实现10s内尽可能的恢复key值,主主要做读写,实现快速读写,不做任何备份方式,从就简单实用rdb方式实现备份。
思路原理:
redis有两种持久化方式,rdb 与aof模式,考虑到aof模式增长过快,恢复比较缓慢,就在从上做rdb模式,主down之后,脚本检查主的状态,之后在从上做bgsave,把从上的rdb文件copy到主的data目录里,重新启动主。通过判断主上key值得数量,是否跟从上的数量一样,来判断是否恢复正常,然后就可以实现key值得完全恢复。备份方式为,一个小时copy一次从上的rbd文件,做一个小时一次的备份。
脚本的思路 主要实现上述思路原理,脚本有点别扭,2做了主,1做的redis从。别扭,实验,没时间修改
#!/bin/bash
DATE=`date +%Y%m%d%M%s`
REDIS_DIR=/usr/local/redis
REDIS2_DIR=/usr/local/redis2
BIN_DIR=/usr/local/redis/bin
DATA1_DIR=/usr/local/redis/data
DATA2_DIR=/usr/local/redis2/data
PORT1=6378
PORT2=6379
function BACKUP_RDB {
ps -ef |grep $PORT2 >/dev/null
if [ $? -eq 0 ];then
${BIN_DIR}/redis-cli -p $PORT2 bgsave >/dev/null
if [ $? -eq 0 ];then
echo -e "\033[31m MASTER REDIS DOWN AND SLAVE bgsave DONE\033[0m"
${BIN_DIR}/redis-cli -p $PORT2 SLAVEOF NO ONE >/dev/null
fi
fi
}
function CP_RDB {
[ -d ${DATA1_DIR}/redisbackup ] || mkdir ${DATA1_DIR}/redisbackup
if [ -f $DATA2_DIR/dump.rdb ];then
mv ${DATA1_DIR}/dump.rdb ${DATA1_DIR}/redisbackup/dump.rdb.$DATE
cp ${DATA2_DIR}/dump.rdb ${DATA1_DIR}/
fi
}
function START_REDIS {
KEY2_NUM=`${BIN_DIR}/redis-cli -p $PORT2 INFO |grep db[0-9]*.key |awk -F[:=,] '{total+=$3}END{print total}'`
${BIN_DIR}/redis-server ${REDIS_DIR}/etc/redis.conf
netstat -tnlp |grep "$PORT1" >/dev/null
a=$?
if [ $a -ne 0 ];then
while [ $a -ne 0 ];do
sleep 2
netstat -tnlp | grep "127.0.0.1:$PORT1" 1>/dev/null 2>/dev/null
a=$?
done
echo -e "\033[32m MASTER REDIS RUNNING ........\033[0m"
sleep 7
KEY1_NUM=`${BIN_DIR}/redis-cli -p $PORT1 INFO |grep db[0-9]*.key |awk -F[:=,] '{total+=$3}END{print
total}'`
# echo ${KEY1_NUM}
if [ $KEY1_NUM -ge $KEY2_NUM ];then
${BIN_DIR}/redis-cli -p $PORT2 SLAVEOF 127.0.0.1 $PORT1 >/dev/null
echo -e "\033[32m MASTER AND SLAVE are Normal sync \033[0m"
else
echo -e "\033[31m KEY NUM are not equal, MASTER AND SLAVE are not Normal sync,place to check ! \033[0m"
break
fi
else
echo -e "\033[32m MASTER REDIS RUNNING ........\033[0m"
sleep 7
KEY1_NUM=`${BIN_DIR}/redis-cli -p $PORT1 INFO |grep db[0-9]*.key |awk -F[:=,] '{total+=$3}END{print total}'`
# echo ${KEY1_NUM}
if [ $KEY1_NUM -ge $KEY2_NUM ];then
${BIN_DIR}/redis-cli -p $PORT2 SLAVEOF 127.0.0.1 $PORT1 >/dev/null
echo -e "\033[32m MASTER AND SLAVE are Normal sync \033[0m"
else
echo -e "\033[31m KEY NUM are not equal, MASTER AND SLAVE are not Normal sync,place to check ! \033[0m"
break
fi
fi
}
while true;do
sleep 10
netstat -tnlp | grep "127.0.0.1:$PORT1" 1>/dev/null 2>/dev/null
TRAP1=$?
ps -ef |grep "$PORT1" >/dev/null
TRAP2=$?
if [ $TRAP1 -ne 0 ] || [ $TRAP2 -ne 0 ];then
#echo redis-master down
BACKUP_RDB
CP_RDB
START_REDIS
fi
done
在恢复的过程中,比较主的key和主宕机后从的key,来判断是否恢复正常,
遇到的问题:在恢复过程中,主key的值一直小于从的key的值,但是主启动完全正常,key值也完全一样,经过思考发现,是程序执行问题,脚本执行太快,当执行到比较的时候,key值得数量未完全恢复,只是时间的问题,所以执行了sleep 3 秒之后,一切正常。大家要根据自己key值得数量的大小来决定判断sleep的时间。
如果有更好的redis主从实现方式,请分享。
边栏推荐
- Length of learning and changing
- Rotation order and universal lock of unity panel
- transform. Forward and vector3 Differences in the use of forward
- Common design parameters of solid rocket motor
- Detailed explanation of string's trim() and substring()
- Countdownlatch blocking wait for multithreading concurrency
- Why should you consider using prism
- Yyds dry inventory MySQL RC transaction isolation level implementation
- Vulnhub range hacksudo Thor
- PHP实现敏感词过滤系统「建议收藏」
猜你喜欢

Gold, silver and four job hopping, interview questions are prepared, and Ali becomes the champion

Vulnhub range hacksudo Thor
![Integer array merge [JS]](/img/0d/70535e0eb1c299bda25159b58c70d7.png)
Integer array merge [JS]

Cookies and session keeping technology

Wechat applet blind box - docking wechat payment

Cassette helicopter and alternating electric field magnetic manometer DPC

Shenyu gateway development: enable and run locally

June issue | antdb database participated in the preparation of the "Database Development Research Report" and appeared on the list of information technology and entrepreneurship industries

New 95 community system whole station source code
![[Verilog quick start of Niuke network question brushing series] ~ priority encoder circuit ①](/img/24/23f6534e2c74724f9512c5b18661b6.png)
[Verilog quick start of Niuke network question brushing series] ~ priority encoder circuit ①
随机推荐
MySQL + JSON = King fried
Object. fromEntries()
Openlayers customize bubble boxes and navigate to bubble boxes
June issue | antdb database participated in the preparation of the "Database Development Research Report" and appeared on the list of information technology and entrepreneurship industries
Data warehouse (3) star model and dimension modeling of data warehouse modeling
网上股票开户安全吗?是否可靠?
为什么你要考虑使用Prisma
Integer array merge [JS]
[mathematical modeling] [matlab] implementation of two-dimensional rectangular packing code
Debiasing word embeddings | talking about word embedding and deviation removal # yyds dry goods inventory #
Alibaba cloud Li Feifei: China's cloud database has taken the lead in many mainstream technological innovations abroad
Countdownlatch blocking wait for multithreading concurrency
Depth first traversal and breadth first traversal [easy to understand]
提交review时ReviewBoard出现500错误解决方法
Equipment simulation and deduction training system software
Penetration practice vulnhub range Nemesis
PHP实现敏感词过滤系统「建议收藏」
MES production equipment manufacturing execution system software
At present, where is the most formal and safe account opening for futures speculation? How to open a futures account?
ACL 2022 | decomposed meta learning small sample named entity recognition