当前位置:网站首页>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主从实现方式,请分享。
边栏推荐
- vulnhub靶场-Hacker_Kid-v1.0.1
- 开发那些事儿:EasyCVR集群设备管理页面功能展示优化
- Software construction scheme of smart factory collaborative management and control application system
- Detailed explanation of string's trim() and substring()
- 荣威 RX5 的「多一点」产品策略
- [wrung Ba wrung Ba is 20] [essay] why should I learn this in college?
- (1) CNN network structure
- China biodegradable plastics market forecast and investment strategy report (2022 Edition)
- ACL 2022 | decomposed meta learning small sample named entity recognition
- MySQL + JSON = King fried
猜你喜欢
(28) Shape matching based on contour features
New 95 community system whole station source code
Data warehouse (3) star model and dimension modeling of data warehouse modeling
LeetCode中等题之TinyURL 的加密与解密
Enter wechat applet
Common design parameters of solid rocket motor
Penetration practice vulnhub range Tornado
(17) DAC conversion experiment
In aks, use secret in CSI driver mount key vault
Intel's open source deep learning tool library openvino will increase cooperation with local software and hardware parties and continue to open
随机推荐
(17) DAC conversion experiment
ISO 27001 Information Security Management System Certification
两数之和c语言实现[通俗易懂]
Setting up a time server requires the client to automatically synchronize the time of the server at 9 a.m. every day
麦趣尔:媒体报道所涉两批次产品已下架封存,受理消费者诉求
Can hero sports go public against the wind?
Reflective XSS vulnerability
MES production equipment manufacturing execution system software
C language implementation of sum of two numbers [easy to understand]
Technical secrets of ByteDance data platform: implementation and optimization of complex query based on Clickhouse
People help ant help task platform repair source code
String的trim()和substring()详解
Radhat builds intranet Yum source server
Yyds dry inventory MySQL RC transaction isolation level implementation
L'ouverture d'un compte d'actions en ligne est - elle sécurisée? Fiable?
Htt [ripro network disk link detection plug-in] currently supports four common network disks
(28) Shape matching based on contour features
Depth first traversal and breadth first traversal [easy to understand]
Work and leisure suggestions of old programmers
DRF --- response rewrite