Benjamin Zhang

目前就职于中科院计算机网络信息中心,主要兴趣集中在数据库,操作系统,中间件。

【SHELL】一键部署补丁

26 Jul 2019 » SHELL

一键部署补丁

需求:有一部分时间是部署应用程序。而正式环境有百余台。如何批量部署。保证补丁部署的可连续性和稳定性,且不会因为个人误操作,导致某些问题。这个脚本应运而生。

应用补丁部署

简单介绍:给一个传参值【VERSION_ID】,版本号。通过版本号,wget下载程序包,并且赋权后部署。由于内置了run.shdeploy.sh,预部署和正式部署两步。

通过定义函数DEF_INPUT。保证第一步预部署脚本run.sh正常运行之后,通过交互动作,来确定是否继续第二步?保证补丁的正常部署。

关于IP地址的方面,做了模糊改变。

这个脚本,后面可以通过python语言,批量执行。保证百余台同时部署这个补丁。后续推出。

#!/bin/bash

if [ $# -lt 1 ] ; then          #not given enough parameters to script
        cat <<USAGEINFO
USAGE:  deployProgram.sh <version>
        must given one argument
        VERSION - respect new arp version id 
EXAMPLE:
        deployProgram.sh 3.1.2
USAGEINFO
        exit 1
fi

# function ,if input value = null,so select original value
function DEF_INPUT {
        DEFAULT_VALUE=$1
        read value
        if [ -n "$value" ];then
                _GET_VALUE=$value
                else
                _GET_VALUE=$DEFAULT_VALUE
        fi
}


VERSION_ID=$1

LOG_PATH=/root
LOG_FILE=${LOG_PATH}/patch_`date +%F`.log
myfile="program"$VERSION_ID".tar.gz"

echo -e "==== jobs started at `date +%F' '%T' '%w` ====" >> ${LOG_FILE}
echo >> ${LOG_FILE}

echo $myfile  >> ${LOG_FILE}

if [ -f "/opt/update/$myfile" ]; then
        echo "file is existed"     >> ${LOG_FILE}
        rm -f /opt/update/$myfile  >> ${LOG_FILE}
fi

echo "program is patched"          >> ${LOG_FILE}
cd /opt/update/                  
echo "check current directory"     >> ${LOG_FILE}

pwd                                >> ${LOG_FILE}

wget  http://172.xxx.0.xxx/patchs/program$VERSION_ID.tar.gz
tar  zxvf  program$VERSION_ID.tar.gz  >> ${LOG_FILE}
chmod -R 777 /opt/update/program$VERSION_ID/*

/opt/update/patch/bin/run.sh  /opt/update/program$VERSION_ID/ >> ${LOG_FILE}

zflag=0

if [ -d "/opt/update/program$VERSION_ID/backend" ]; then
        echo "dir is existed"        >> ${LOG_FILE}
        zflag=1
fi

if [ $zflag == 1 ];then
        echo "ready to deploy"
        echo -n continue[Y/y] or exit[N/n]:[y]:
        DEF_INPUT y
        GOON=$_GET_VALUE
        GOON=`echo $GOON | tr '[a-z]' '[A-Z]' | cut -c1`
         case $GOON in
         "N") exit;;
         "Y") ;;
         *) DEF_INPUT y;;
         esac
fi

/opt/update/patch/bin/deploy.sh /opt/update/program$VERSION_ID/ >> ${LOG_FILE}
tail -f /opt/tomcat/logs/catalina.out

数据库SQL补丁部署

与应用程序补丁类似,通过封装dbUpdate.sh脚本,一键部署SQL补丁。避免敲多次命令或者误操作等。

#!/bin/bash
if [ $# -lt 1 ] ; then          #not given enough parameters to script
        cat <<USAGEINFO
USAGE:  deployDB.sh <version>
        must given one argument
        VERSION - respect new arp version id 
EXAMPLE:
        deployDB.sh 3.1.3
USAGEINFO
        exit 1
fi

VERSION_ID=$1

LOG_PATH=/root
LOG_FILE=${LOG_PATH}/patch_`date +%F`.log
myfile="db"$VERSION_ID".tar.gz"

echo -e "==== jobs started at `date +%F' '%T' '%w` ====" >> ${LOG_FILE}
echo "--------------------------------------------------">> ${LOG_FILE}

echo $myfile  >> ${LOG_FILE}

#if [ -f "/tmp/$myfile" ]; then
#        echo "file is existed"     >> ${LOG_FILE}
#        rm -f /tmp/$myfile  >> ${LOG_FILE}
#fi

echo "db is ready to patch"        >> ${LOG_FILE}
cd /tmp/                  
echo "check current directory"     >> ${LOG_FILE}

pwd                                >> ${LOG_FILE}

#wget  http://172.31.xx.xx/update/patches/$VERSION_ID/db$VERSION_ID.tar.gz 

tar zxvf db$VERSION_ID.tar.gz  >> ${LOG_FILE}
chmod -R 777 /tmp/db$VERSION_ID

#su - oracle -c "cd /tmp/db$VERSION_ID"
#su - oracle -s /bin/bash /tmp/db$VERSION_ID/dbUpdate.sh 
su - oracle