背景
公司正常的業(yè)務(wù)流程是生產(chǎn)服務(wù)器上部署的一個(gè)程序去讀取數(shù)據(jù)庫(kù)盛泡,并獲取所有ip信息,啟動(dòng)socket連接夷蚊,發(fā)送相關(guān)業(yè)務(wù)指令构挤。
目前有一個(gè)需求,需要單獨(dú)測(cè)試一個(gè)ip惕鼓,這個(gè)單獨(dú)的ip需要使用另外的程序測(cè)試筋现,其他的ip使用原始生產(chǎn)程序測(cè)試。并且測(cè)試完畢,需要恢復(fù)這個(gè)ip由生產(chǎn)程序接管矾飞。
沒有自動(dòng)化運(yùn)維腳本的時(shí)候一膨,需要把這個(gè)ip拆分出來時(shí),我們需要
- 去數(shù)據(jù)庫(kù)修改數(shù)據(jù)(比如:刪除待測(cè)數(shù)據(jù)ip或者置可用狀態(tài)為0)
- 停止原生產(chǎn)程序洒沦,并重啟該程序(重啟后會(huì)重新讀取數(shù)據(jù)庫(kù)豹绪,這樣就可以排除需要單獨(dú)測(cè)試的ip)
- 啟動(dòng)新程序
在恢復(fù)這個(gè)ip由生產(chǎn)程序接管時(shí),我們需要
- 停止新程序
- 修改生產(chǎn)數(shù)據(jù)庫(kù)申眼,把這個(gè)ip恢復(fù)
- 重新啟動(dòng)生產(chǎn)程序
在反復(fù)操作過程中發(fā)現(xiàn)瞒津,可以使用自動(dòng)運(yùn)維腳本來實(shí)現(xiàn)這種繁瑣又重復(fù)的勞動(dòng)。
運(yùn)維腳本
運(yùn)維腳本分為兩個(gè)括尸,一個(gè)是啟動(dòng)腳本巷蚪,另外一個(gè)是恢復(fù)腳本。
啟動(dòng)腳本如下:
#! /bin/bash
# author: ging
# date: 2020-07-22
ip=$1;
port=$2;
jarname=$3
user=XXX
passwd=XXX
sqlport=3306
host=XXX
autodir=/data/sg/testled
curdate="`date +%Y-%m-%d,%H:%M:%S`";
table_name="t_led_reader_lnk"
# save ori sql info
echo "${curdate}:start back up ori sql info" >> ${autodir}/autostart.txt
if [ -f "${autodir}/${table_name}.sql" ];then
echo "${curdate}:文件存在濒翻,先刪除屁柏!" >> ${autodir}/autostart.txt
rm -rf ${autodir}/${table_name}.sql
else
echo "${curdate}:文件不存在,可以直接導(dǎo)入" >> ${autodir}/autostart.txt
fi
mysqldump -u$user -p$passwd -h$host -P$sqlport sg ${table_name} > ${table_name}.sql
if [ -f "${autodir}/${table_name}.sql" ];then
echo "${curdate}:導(dǎo)入成功有送!" >> ${autodir}/autostart.txt
else
echo "${curdate}:導(dǎo)入失斍傲!" >> ${autodir}/autostart.txt
return;
fi
# delete that line for ip and port
delete_sql="delete from ${table_name} where led_ip='${ip}'"
mysql -u$user -p$passwd -h$host -P$sqlport sg -e "${delete_sql}" -vvv > ${autodir}/autostart-delete.txt
#jurge if delete-success
grep "Query OK, 1 row affected" ${autodir}/autostart-delete.txt >/dev/null
if [ $? -eq 0 ]; then
echo "${curdate}:刪除成功娶眷!" >> ${autodir}/autostart.txt
else
echo "${curdate}:刪除失敗啸臀!" >> ${autodir}/autostart.txt
return;
fi
# stop led-send
ps -ef | grep "java" | grep "/data/sg/ledsend" | awk '{print $2}' | xargs kill -9
if [ $? -eq 0 ]; then
echo "${curdate}:關(guān)閉程序成功届宠!" >> ${autodir}/autostart.txt
else
echo "${curdate}:關(guān)閉程序失敗乘粒!" >> ${autodir}/autostart.txt
return;
fi
# restart led-send
nohup java -XX:-UseAdaptiveSizePolicy -XX:SurvivorRatio=8 -jar /data/sg/ledsend-0.0.1-SNAPSHOT.jar --spring.profiles.active=verify > /data/sg/lslog.txt 2>&1 &
if [ $? -eq 0 ]; then
echo "${curdate}:重啟程序成功豌注!" >> ${autodir}/autostart.txt
else
echo "${curdate}:重啟程序失敗灯萍!" >> ${autodir}/autostart.txt
return;
fi
# start testledjar]
nohup java -jar /data/sg/testled/${jarname} > /data/sg/testled/testled_${jarname}.txt 2>&1 &
if [ $? -eq 0 ]; then
echo "${curdate}:啟動(dòng)測(cè)試程序成功轧铁!" >> ${autodir}/autostart.txt
else
echo "${curdate}:重啟測(cè)試程序失敗旦棉!" >> ${autodir}/autostart.txt
return;
fi
恢復(fù)腳本如下:
#! /bin/bash
# author : ging
# date : 2020-07-22
# stop the test jar
jarname=$1
user=root
passwd=123456
sqlport=3306
host=127.0.0.1
autodir=/data/sg/testled
curdate="`date +%Y-%m-%d,%H:%M:%S`";
ledjarname=$2
ps -ef | grep "java" | grep "${jarname}" | awk '{print $2}' | xargs kill -9
if [ $? -eq 0 ]; then
echo "${curdate}:關(guān)閉測(cè)試程序成功齿风!" >> ${autodir}/autorecover.txt
else
echo "${curdate}:關(guān)閉測(cè)試程序失敗绑洛!" >> ${autodir}/autorecover.txt
return;
fi
#recover the sql
mysql -u$user -p$passwd -h$host -P$sqlport -Dsg < ${autodir}/t_led_reader_lnk.sql > ${autodir}/autostop-recover.txt
# stop led-send
ps -ef | grep "java" | grep "/data/sg/ledsend" | awk '{print $2}' | xargs kill -9
if [ $? -eq 0 ]; then
echo "${curdate}:關(guān)閉程序成功救斑!" >> ${autodir}/autorecover.txt
else
echo "${curdate}:關(guān)閉程序失敗真屯!" >> ${autodir}/autorecover.txt
return;
fi
# restart led-send
nohup java -XX:-UseAdaptiveSizePolicy -XX:SurvivorRatio=8 -jar /data/sg/${ledjarname} --spring.profiles.active=verify > /data/sg/lslog.txt 2>&1 &
if [ $? -eq 0 ]; then
echo "${curdate}:重啟程序成功脸候!" >> ${autodir}/autorecover.txt
else
echo "${curdate}:重啟程序失敗!" >> ${autodir}/autorecover.txt
return;
fi