当前位置:网站首页>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主从实现方式,请分享。
边栏推荐
- Gold, silver and four want to change jobs, so we should seize the time to make up
- Intelligent operation and maintenance practice: banking business process and single transaction tracking
- Countdownlatch blocking wait for multithreading concurrency
- 中国茂金属聚乙烯(mPE)行业研究报告(2022版)
- China biodegradable plastics market forecast and investment strategy report (2022 Edition)
- 网上股票开户安全吗?是否可靠?
- PIP version problems: PIP problems still occur when installing akshare and using Tsinghua source and Douban source
- 线上开通ETF基金账户安全吗?有哪些步骤?
- Redis -- data type and operation
- 手机开户股票开户安全吗?那么开户需要带些什么?
猜你喜欢
Shenyu gateway development: enable and run locally
Mysql database - Advanced SQL statement (2)
[beauty detection artifact] come on, please show your unique skill (is this beauty worthy of the audience?)
(16) ADC conversion experiment
Intelligent operation and maintenance practice: banking business process and single transaction tracking
Wechat applet blind box - docking wechat payment
换掉UUID,NanoID更快更安全!
From comedians to NBA Zhan Huang, check the encrypted advertisements during this super bowl
The difference and relationship between iteratible objects, iterators and generators
Kia recalls some K3 new energy with potential safety hazards
随机推荐
Is the software of futures pioneer formal and safe? Which futures company is safer to choose?
《中国智慧环保产业发展监测与投资前景研究报告(2022版)》
Report on research and investment prospects of China's silicon nitride ceramic substrate industry (2022 Edition)
The new server is packaged with the source code of H5 mall with an operation level value of several thousand
Cookies and session keeping technology
Object. fromEntries()
Replace UUID, nanoid is faster and safer!
Common design parameters of solid rocket motor
中国一次性卫生用品生产设备行业深度调研报告(2022版)
线上开通ETF基金账户安全吗?有哪些步骤?
ISO 27001 Information Security Management System Certification
RadHat搭建内网YUM源服务器
中国生物降解塑料市场预测与投资战略报告(2022版)
Is it safe to open an ETF account online? What are the steps?
Irradiance, Joule energy, exercise habits
String的trim()和substring()详解
Mysql database - Advanced SQL statement (2)
麦趣尔:媒体报道所涉两批次产品已下架封存,受理消费者诉求
From comedians to NBA Zhan Huang, check the encrypted advertisements during this super bowl
Oom caused by improper use of multithreading