#!/bin/bash ## Edit date 2020/05/09 ## 说明: 脚本用于更新燕赵非车项目 ## 默认配置文件目录、app存放目录、 ## discovery、config模块基本保持不变、但必须启动且正常运行 ## 使用nginx做代理转发、备份目录存在 # 环境 env=prd # discovery服务访问链接 discovery_host=10.1.139.124 discovery_port=8761 link=http://$discovery_host:$discovery_port/eureka/ # config服务 config_host=10.1.139.124 config_port=8888 # 字符编码 encoding=GBK # base目录 BASE=/home/qianhai # 配置文件存放目录 CONFIG=$BASE/config # app存放目录,包括war包及静态文件包 APP=$BASE/app # 静态文件目录 HTML=/usr/local/webserver/nginx/html # 备份目录 BACKUP=$BASE/backup # 当前时间 NOW=`date +%Y%m%d-%H%M%S` # 一周前 WEEK=`date -d "7 days ago" +%Y%m%d` # 各模块启动命令 #application-insurance-discovery-server="nohup java -jar application-insurance-discovery-server.jar > /dev/null 2>&1 &" #application-insurance-config-server="nohup java -jar application-insurance-config-server.jar --spring.cloud.config.server.native.searchLocations=file:$CONFIG --eureka.client.serviceUrl.defaultZone=$link > /dev/null 2>&1 &" #application-insurance-core-service="nohup java -jar application-insurance-core-service.jar --spring.profiles.active=$env --eureka.client.serviceUrl.defaultZone=$link >/dev/null 2>&1 &" #application-insurance-admin-api="nohup java -jar application-insurance-admin-api.jar --spring.profiles.active=$env --eureka.client.serviceUrl.defaultZone=$link >/dev/null 2>&1 &" #application-insurance-site-api="nohup java -jar application-insurance-site-api.jar --spring.profiles.active=$env --eureka.client.serviceUrl.defaultZone=$link >/dev/null 2>&1 &" #application-insurance-company-trans="nohup java -jar -Dfile.encoding=$encoding application-insurance-company-trans.jar --spring.profiles.active=$env --eureka.client.serviceUrl.defaultZone=$link >/dev/null 2>&1 &" #nocar-static=dist.tar.gz # 备份 backup() { module=$1 target=$2 if [ -f "$BACKUP/$module-*.tar.gz" ];then rm -rf $BACKUP/$module-$WEEK-*.tar.gz fi echo -e "\033[33;1mBackup now! \033[0m" cd $target && /usr/bin/tar -zPcf $BACKUP/$module-$NOW.tar.gz --exclude=logs --exclude=*.log --exclude=*.out * echo -e "\033[33;1mBackup: $BACKUP/$module-$NOW.tar.gz \033[0m" } update() { module=$1 target=$2 echo -e "\033[33;1mCopy $module to $target now! \033[0m" cp -rf $module $target } # 解压 unpack() { module=$1 target=$2 suffix=`echo $module | awk -F "." '{print $NF}'` echo -e "\033[33;1munpackage $module to $target now! \033[0m" if [ $suffix == "war" ] || [ $suffix == "zip" ] || [ $suffix == "jar" ];then /usr/bin/unzip $module -d $target elif [ $suffix == "tar" ];then /usr/bin/tar -xf $module -C $target elif [ $suffix == "gz" ] || [ $suffix == "tgz" ]; then /usr/bin/tar -xzf $module -C $target elif [ $suffix == "bz2" ]; then /usr/bin/tar -xjf $module -C $target fi echo -e "\033[33;1munpackage finish! \033[0m" } # 检查discovery checkdiscovery() { pid=`ps aux | grep application-insurance-discovery-server | grep "java -jar" | grep -v grep | awk '{print $2}'` status=`echo "quit" | telnet $discovery_host $discovery_port 2>&1 | grep "Escape character is"` if [ -z "$status" ] || [ -z "$pid" ];then echo -e "\033[32;1mDiscovery not run, start now! \033[0m" startup application-insurance-discovery-server fi } # 检查config checkconfig() { checkdiscovery pid=`ps aux | grep application-insurance-config-server | grep "java -jar" | grep -v grep | awk '{print $2}'` status=`echo "quit" | telnet $config_host $config_port 2>&1 | grep "Escape character is"` if [ -z "$status" ] || [ -z "$pid" ];then echo -e "\033[32;1mConfig not run, start now! \033[0m" startup application-insurance-config-server fi } # 清理 remove() { target=$1 echo -e "\033[32;1mRemove $target now! \033[0m" /usr/bin/rm -rf $target echo -e "\033[32;1mRemove finish! \033[0m" } # 启动 startup() { module=$1 pid=`ps aux | grep $module | grep "java -jar" | grep -v grep | awk '{print $2}'` echo -e "\033[32;1mStop $module now! \033[0m" if [ -n "$pid" ];then kill -9 $pid fi echo -e "\033[32;1mStop $module finish! \033[0m" echo -e "\033[32;1mStart $module now! \033[0m" cd $BASE/`echo $module | awk -F '.' '{print $1}'` if [ $module == "application-insurance-discovery-server.jar" ];then echo "nohup java -jar $module > /dev/null 2>&1 &" | bash echo -e "\033[32;5;1mOther module must be RESTART now! \033[0m" elif [ $module == "application-insurance-config-server.jar" ];then checkdiscovery echo "nohup java -jar $module --spring.cloud.config.server.native.searchLocations=file:$CONFIG --eureka.client.serviceUrl.defaultZone=$link > /dev/null 2>&1 &" | bash elif [ $module == "application-insurance-core-service.jar" ];then checkdiscovery checkconfig echo "nohup java -jar $module --spring.profiles.active=$env --eureka.client.serviceUrl.defaultZone=$link >/dev/null 2>&1 &" | bash elif [ $module == "application-insurance-admin-api.jar" ];then checkdiscovery checkconfig echo "nohup java -jar $module --spring.profiles.active=$env --eureka.client.serviceUrl.defaultZone=$link >/dev/null 2>&1 &" | bash elif [ $module == "application-insurance-site-api.jar" ];then checkdiscovery checkconfig echo "nohup java -jar $module --spring.profiles.active=$env --eureka.client.serviceUrl.defaultZone=$link >/dev/null 2>&1 &" | bash elif [ $module == "application-insurance-company-trans.jar" ];then checkdiscovery checkconfig echo "nohup java -jar -Dfile.encoding=$encoding $module --spring.profiles.active=$env --eureka.client.serviceUrl.defaultZone=$link >/dev/null 2>&1 &" | bash fi } # 选择需要更新的模块 echo -e "\033[31;1m\t++++++Module List++++++\033[0m" echo -e "\033[32;1m\ta: application-insurance-discovery-server \n\tb: application-insurance-config-server \n\tc: application-insurance-core-service \n\td: application-insurance-admin-api \n\te: application-insurance-site-api \n\tf: application-insurance-company-trans \n\tg: nocar-static \t\033[0m" echo -e "\033[31;1m\tNotice: Valid Choice - a/b/c/d/e/f/g\033[0m" read -p "\tChoose Module: " Choice case $Choice in a|A|application-insurance-discovery-server) project="application-insurance-discovery-server" package="application-insurance-discovery-server.jar" ;; b|B|application-insurance-config-server) project="application-insurance-config-server" package="application-insurance-config-server.jar" ;; c|C|application-insurance-core-service) project="application-insurance-core-service" package="application-insurance-core-service.jar" ;; d|D|application-insurance-admin-api) project="application-insurance-admin-api" package="application-insurance-admin-api.jar" ;; e|E|application-insurance-site-api) project="application-insurance-site-api" package="application-insurance-site-api.jar" ;; f|F|application-insurance-company-trans) project="application-insurance-company-trans" package="application-insurance-company-trans.jar" ;; g|G|nocar-static) project="nocar-static" package="dist.tar.gz" ;; *) echo -e "\033[31;5mWrong Input! Please Check! \033[0m" exit 1 ;; esac if [ $project == "nocar-static" ];then backup $project $HTML/$project remove $HTML/$project/* unpack $APP/$package $HTML/$project else backup $project $BASE/$project remove $BASE/$project/$package update $APP/$package $BASE/$project startup $package fi