客户环境安装了redis+哨兵 关闭的时候顺序不对会导致程序启动有问题,然后我写了个备份还原脚本,如果启动有问题的时候就执行这个脚本:
!/bin/bash
日志目录
logpath="/home/archforce/log/"
写入位置
bashrc="/home/archforce/.bashrc"
备份配置文件目录
confpath="/home/archforce/ASF/cfg/"
历史备份配置文件目录
histroy_confpath="/home/archforce/ASF/redis_conf_bak/"
历史配置文件最大数量
histroy_numbers=12
历史保留文件数量
reserver=6
每月自动备份一次,默认每月第一天备份,参数最小值1 最大28
bak_day1=1
获取当前日期
bak_day2=$(date +%d)
写入登录执行
written(){
cat ${bashrc} |grep renew_to_redis> /dev/null
if [ $? -eq 0 ] ;then
#echo "文件已存在,"
logs 登录启动文件存在
else
echo "/bin/bash /home/archforce/ASF/tool/renew_to_redis.sh" >> ${bashrc} > /dev/null
echo "写入到启动执行完成:/home/archforce/.bashrc"
logs "写入到启动执行完成:/home/archforce/.bashrc"
fi
}
打印日志方法
logs(){
tis=$(date +%F_%T)
onvalues=$1
touch ${logpath}renew_to_redis.log
echo "${tis}😒{onvalues}" >> ${logpath}renew_to_redis.log
}
备份
backup_to_redis(){
if [ $bak_day1 -eq $bak_day2 ];then
today=$(date +%F_to_%S)
if [ ! -d "/home/archforce/ASF/redis_conf_bak/" ] ;then
mkdir /home/archforce/ASF/redis_conf_bak/ -pv
fi
if [ -f ${confpath}redis.conf ];then
mv ${confpath}redis.conf ${histroy_confpath}redis.conf${today}
fi
cp /home/archforce/redis/7000/redis.conf ${confpath}redis.conf
clearbackupfile
else
logs "未到备份日期${bak_day1},暂不备份"
fi
}
备份清理
clearbackupfile(){
numbers=$(ls ${histroy_confpath}redis.conf|wc -l)
logs "历史文件最大保存数量:$histroy_numbers"
logs "保留文件数量:$reserver "
#((histroy_numbers+=1))
reserverfilenumber=$(($numbers - $reserver))
echo "保留文件${reserver}个"
logs "保留文件${reserver}个"
echo "当前备份文件${numbers}个"
echo "备份目录:$histroy_confpath"
logs "当前备份文件${numbers}个"
if [ $numbers -gt $histroy_numbers ];then
filelist=$(ls -t ${histroy_confpath}redis.conf |tail -${reserverfilenumber})
for i in $filelist;do
rm -f ${i}
logs "删除文件${i}"
echo "删除文件${i}"
done
fi
}
还原配置文件
restore(){
logs "开始还原"
echo "10秒钟内可以执行Ctrl + C 取消配置文件还原操作"
sleep 10
cp /home/archforce/redis/7000/redis.conf ${histroy_confpath}redis.bak_conf$bak_day2
logs "还原前文件放置到:${histroy_confpath}redis.conf_backup$bak_day2"
cp ${confpath}redis.conf /home/archforce/redis/7000/redis.conf
}
帮助
helps(){
logs 帮助
echo "-----------------------------------------------------------------"
echo " "
echo "帮助"
echo " "
echo "手动备份 :bash /home/archforce/ASF/tool/renew_to_redis.sh d "
echo "还原配置文件:bash /home/archforce/ASF/tool/renew_to_redis.sh r "
echo "本脚本自动每月备份一次,备份目录:${histroy_confpath}"
echo " "
echo "-----------------------------------------------------------------"
}
main(){
#写入登录启动脚本
written
#开始备份
backup_to_redis
if [ $1 = "r" ];then
restore
elif [ $1 = "d" ];then
bak_day1=$bak_day2
backup_to_redis
elif [ $1 = "h" ];then
helps
else
exit
fi
}
canshu="e"
if [ ! -z "$1" ];then
canshu=$1
fi
main $canshu