This repository has been archived on 2025-04-28. You can view files and clone it, but cannot push or open issues or pull requests.
CMS/5.MISC/CMS程序替换教程/upgrade.sh

112 lines
2.0 KiB
Bash
Raw Normal View History

2024-11-19 09:19:21 +00:00
#!/bin/bash
# 目标路径
BASE_PATH=/usr/local/gateway
function backup(){
if [-d $BASE_PATH]; then
if [ ! -d $BASE_PATH/backup ]; then
mkdir -p $BASE_PATH/backup
fi
cp -rf $BASE_PATH/* $BASE_PATH/backup/
fi
}
function rollback(){
if [ -d $BASE_PATH]; then
if [ -d $BASE_PATH/backup ]; then
rm -rf $BASE_PATH/* | grep -v config.toml
cp -rf $BASE_PATH/backup/* $BASE_PATH/ | grep -v config.toml
fi
fi
}
function upgrade(){
if [ ! -d $BASE_PATH]; then
mkdir -p $BASE_PATH
fi
mkdir -p $BASE_PATH/tmp
unzip -d $BASE_PATH/tmp $1
if [ 0 -ne $? ];then
echo "解压失败">&2
return false
fi
mv $BASE_PATH/tmp/* $BASE_PATH/ | grep -v config.toml
return true
}
function clean(){
rm -rf $BASE_PATH/backup
rm -rf $BASE_PATH/tmp
rm -rf $1
}
function service_ok(){
res=`systemctl status gateway`
if [[ $res =~ "(dead)" ]]
then
return false
else
return true
fi
}
function stop(){
if [ $(service_ok)=true ]; then
systemctl stop gateway
fi
}
function start(){
stop
cnt=0
flag=false
while [ 0 -eq 0 ]
do
systemctl start gateway
sleep 1
flag=$(service_ok)
if [ $flag=true ]; then
break;
else
cnt=$[${cnt}+1]
if [ ${cnt} -eq 3 ]; then
break
fi
fi
done
return $flag
}
if [ -n "$1"]
then
stop
backup
flag=$(upgrade)
if [ $flag=true ];then
rollback
start
clean
exit 1
fi
flag=$(start)
if [ $flag=true ];then
echo "success">&1
clean
exit 0
else
rollback
start
echo "服务无法启动,升级失败,回滚到上一版本">&2
clean
exit 1
fi
else
echo "没有升级包路径">&2
fi