shell脚本自动部署tomcat

#!/bin/sh
#部署web和interface的脚本,第一个参数指定部署哪个服务w:web,i:interface
#部署包上传路径为/data
#exitCode:
# 1 参数错误
# 2 部署前检查不通过
# 3 tomcat启动出错
# 4 部署后日志检查出错
# 0 部署成功

localIp=$(ifconfig |grep '132.'|awk '{print $2}');
service='none'
src=/data/unified-web.war
tomcatDir=/data/tomcat/apache-tomcat-8.0.52
dest=$tomcatDir/webapps/unified.war
webService=web
interfaceService=interface
tempDir=/data/script/temp
oraclePF=WEB-INF/classes/config/oracle.properties
myExit(){
  echo $2
  if [ -e $tempDir ];
  then
  rm -rf $tempDir
  fi
  exit $1
}
# 检查入参
if [[ $1 = 'w' ]];
then
        service=$webService
elif [[ $1 = 'i' ]];
then
  service=$interfaceService
  src=/data/unified-interface.war
  tomcatDir=/data/tomcat/apache-tomcat-interface
  dest=$tomcatDir/webapps/unified-interface.war

else
        echo "执行方法:
./autoDeploy.sh w -- 部署web服务
./autoDeploy.sh w -- 部署interface服务"
        myExit 1 "入参错误"
fi
# 检查部署包
echo "开始部署$service"
if [ ! -e $src ];
then
  myExit 1 "$src 不存在!"
fi
mkdir -p $tempDir
echo "新包为:$src"
echo "tomcat部署包地址为:$dest"
echo "检查部署包大小和时间..."
ls -hl $src
read -p "请确认时间大小和时间:(y/n)" flag
if [[ $flag != 'y' ]] ;
then
myExit 2 "退出"
fi

echo 'A'|unzip /data/unified-web.war $oraclePF -d $tempDir >/dev/null
echo "================数据源=============="
cat $tempDir/$oraclePF
echo "===================================="

read -p "当前主机:$localIp,请确认数据源是否正确:(y/n)" flag
if [[ $flag != 'y' ]] ;
then
myExit 2 "退出"
fi

# 停止tomcat
echo "============停止服务...=============="
ps -ef|grep 'tomcat'|grep "$tomcatDir"|grep -v 'grep' >/dev/null
flag=$?
if [[ $flag != '0' ]];
then
  echo "$service 服务已经停止!"
  read -p "是否继续?(y/n):" flag
  if [[ $flag != 'y' ]];
  then
  myExit 3 "退出"
  fi
else
  stopFile=$tomcatDir/bin/kill.sh
  if [ ! -e $stopFile ]
  then
    myExit 3 "$stopFile 找不到"
  else
    $stopFile
  fi
fi
echo "============停止服务完成=============="
#备份
echo "============备份...=============="
pushd . >/dev/null
cd $tomcatDir
tar czvf webapps$(date +%Y%m%d).tar webapps >/dev/null
if [ ! -e $tomcatDir/webapps$(date +%Y%m%d).tar ];
then
myExit 3 "备份webapps目录出错"
fi
popd >/dev/null
echo "============备份完成=============="

echo "============复制war包...=============="
cp $src $dest
flag=$?
if [[ $flag != '0' ]];
then
myExit 3 "复制文件至$dest 出错"
fi
echo "============复制war包完成=============="
# 启动tomcat
echo "============启动tomcat...=============="
$tomcatDir/bin/startup.sh
tail -fn 0 --pid=$(ps -ef|grep 'sed -e /Server startup in/q'|grep -v 'grep'|awk '{print $2}') $tomcatDir/logs/catalina.out|sed -e '/Server startup in/q'

flag=$?
if [[ $flag != '0' ]];
then
myExit 3 "tomcat启动出错"
fi
echo "============启动tomcat完成=============="

myExit 0 "成功";


 

你可能感兴趣的:(bash)