部署脚本

  1 #!/bin/bash
  2 # date_time: 2022-03-02
  3 # author: yxy
  4 
  5 servies_name='snail-ad.war'
  6 bak_time=`date "+%Y%m%d"`
  7 
  8 stop(){
  9     # 停止服务
 10     /snail/tomcats/snail-ad-server-0{1..3}/bin/shutdown.sh
 11     # 检测服务是否停止,如果未停止,则强制停止
 12     for server in snail-ad-server-0{1..3}
 13     do 
 14         sleep 5
 15         pid=`ps -ef |grep ${server} |grep -v "grep"|awk '{print $2}' `
 16         ps -ef |grep ${server} |grep -v "grep"  > /dev/null
 17         if [ $? == 0 ]
 18         then
 19             sleep 5
 20             kill -9 $pid
 21         else        
 22             echo "${server} 服务已停止"
 23         fi
 24     done
 25 }
 26 
 27 
 28 start(){
 29     # 启动服务
 30     /snail/tomcats/snail-ad-server-0{1..3}/bin/startup.sh
 31     for server in snail-ad-server-0{1..3}
 32     do 
 33         sleep 5
 34         pid=`ps -ef |grep ${server} |grep -v "grep"|awk '{print $2}' `
 35         ps -ef |grep ${server} |grep -v "grep"  > /dev/null
 36         if [[ $? == 0 ]]
 37         then
 38             echo "${server} 服务已启动"            
 39         else    
 40             sleep 5        
 41             echo "${server} 请确认服务是否启动"    
 42         fi
 43     done
 44 }
 45 
 46 
 47 rollback(){
 48     if [ ! -f /snail/appData/${servies_name}${bak_time} ]
 49     then
 50         # 没有文件备份
 51         exit 0
 52     fi
 53     
 54     # 停止服务
 55     stop
 56     
 57     if [  -f "/snail/appData/${servies_name}" ]
 58     then
 59         rm -fr /snail/appData/${servies_name}
 60         # 回退并启动服务
 61         tar -xf /snail/appData/${servies_name}${bak_time}  /snail/appData/
 62         start
 63         exit 0
 64     fi    
 65 }
 66 
 67 
 68 
 69 # 判断新部署项目包不存在,退出部署脚本
 70 if [ ! -f "/home/ops/"${servies_name} ]
 71 then
 72     echo "/home/ops/${servies_name}文件不存在"
 73     exit 0
 74 fi
 75 
 76 # 判断旧版本包是否存在
 77 if [ ! -f "/snail/appData/"${servies_name} ]
 78 then
 79     echo "/snail/appData/${servies_name} 文件不存在"
 80     exit 0
 81 fi
 82 
 83 
 84 # 备份原snail-ad.war
 85 tar -zcvf /snail/appData/${servies_name}${bak_time} -C /snail/appData/ ${servies_name}
 86 # 判断是否备份成功
 87 if [ ! -f /snail/appData/${servies_name}${bak_time} ]
 88 then
 89     # 文件备份不成功,退出
 90     echo "文件备份不成功退出脚本"
 91     exit 0
 92 fi
 93 
 94 
 95 # 调用停止服务函数
 96 stop
 97 # 删除旧版本war包
 98 rm -fr /snail/appData/${servies_name}
 99 # 将部署包/home/ops/snail-ad.war包copy到数据目录下,执行启动函数
100 cp /home/ops/${servies_name} /snail/appData/
101 if [ ! -f "/snail/appData/${servies_name}" ]
102 then
103     # 判断如果没有copy成功,回退并启动服务
104     tar -xf /snail/appData/${servies_name}${bak_time}  /snail/appData/
105     start
106     exit 0
107 fi    
108 
109 # 调用启动函数 
110 start
111 
112 # 防止下次部署包文件冲突
113 rm -fr /home/ops/${servies_name}

 

posted @ 2022-10-19 10:40  yxy_linux  阅读(23)  评论(0编辑  收藏  举报