检查名为s1plat的Redis数据库。检查Redis中key数量的变化来判断备份数据是否异常。
现行k8s中的Redis状况检查;
status-redis-s1plat.sh
#!/bin/bash
rundir=$(cd $(dirname $0); pwd)
theday=`date +%F`
if [ ! -d "${rundir}/log" ]; then
mkdir ${rundir}/log
fi
if [ "$*" ];then
arg=$*
else
arg="redis-0"
fi
echo -e "当前 s1plat-Redis 数据库的 key 的数量:" > ${rundir}/log/redis-s1plat-status-${theday}.txt
#kubectl exec -it s1plat-redis-0 -n s1plat redis-cli -- -a xxx --raw dbsize
kubectl exec -it s1plat-redis-0 -n s1plat redis-cli -- -a 访问密码 --raw dbsize \
>> ${rundir}/log/redis-s1plat-status-${theday}.txt 2>&1
#sed -n '$p' ${rundir}/log/redis-status-${theday}.txt
sed -i '/Defaulting/d' ${rundir}/log/redis-s1plat-status-${theday}.txt
sed -i '/Use/d' ${rundir}/log/redis-s1plat-status-${theday}.txt
cat ${rundir}/log/redis-s1plat-status-${theday}.txt
运行
验证Redis数据;
redis-check-s1plat.sh
#!/bin/bash
rundir=$(cd $(dirname $0); pwd)
theday=`date +%F`
echo -e "=======${theday} s1plat-Redis备份验证======="
#清除上次验证数据
dbtmpdir="${rundir}/dbtmp/redis"
rm -rf ${dbtmpdir} && mkdir ${dbtmpdir} -p
#如果没有参数则搜索当前运行目录下的
if [ "$1" ];then
baktar=$1
else
baktar=$(find ${rundir} -name "*s1plat*redis.tar" | sort -rn | head -1)
fi
if [ ! -e "${baktar}" ];then
echo "没有找到备份*redis.tar!!!"
exit 0
fi
echo -e "1.Redis备份文件是:\n ${baktar}"
echo -e "2.还原数据库的临时目录:\n ${dbtmpdir}"
echo -e "3.还原数据"
cp -rf ${baktar} ${dbtmpdir}/redis.tar
cd ${dbtmpdir}
tar -xvzf ${dbtmpdir}/redis.tar >> /dev/null 2>&1
echo -e " 还原的数据库:${dbtmpdir}/data"
#echo -e "\n======================================" >> ${rundir}/log/redis-check.txt
echo -e "=======${theday} s1plat-redis备份验证=======" >> ${rundir}/log/redis-check.txt
#echo -e "======================================" >> ${rundir}/log/redis-check.txt
echo -e "备份文件为:${baktar}" >> ${rundir}/log/redis-check.txt
echo -e "查询备份数据库的 key 的数量:" >> ${rundir}/log/redis-check.txt
echo -e "4.检验数据"
docker run --name redis-check -d -v ${dbtmpdir}/data:/data redis:3.2 redis-server \
>> /dev/null 2>&1
echo -e "等待redis服务启动完成,10秒..."
sleep 10;
docker exec -it redis-check redis-cli -c 'dbsize' \
>> ${rundir}/log/redis-check.txt
docker stop redis-check >> /dev/null 2>&1
docker rm redis-check >> /dev/null 2>&1
#sed -i 's///g' ${rundir}/log/etcd-check.txt
#sed -i '/^$/d' ${rundir}/log/etcd-check.txt
#cat ${rundir}/log/etcd-check.txt | tr -s "\n" > ${rundir}/log/etcd-check.txt
tail -n 4 ${rundir}/log/redis-check.txt
echo -e "验证结果输出:${rundir}/log/redis-check.txt"
运行