首页 > 代码库 > Bash Shell-通用项目升级脚本

Bash Shell-通用项目升级脚本

#!/bin/bash

################################################################################
# Filename : upgrade_project.sh
# Summary  : 项目升级脚本
# License  : GPL    
# Version  : 1.0
# Author   : Jet Bi
# Email    : 1207501666@qq.com
# Date     : 2016/08/08
# Note     :
#    1.需要项目支持svn升级方式,并装有svn
#    2.需要restart_tomcat.sh存在
#    3.使用说明
#        升级:./upgrade_project.sh -p 项目路径 -t Tomcat路径
#        回滚:./upgrade_project.sh -r 版本号 -p 项目路径 -t Tomcat路径                                     
################################################################################

project_path=""                                          # 项目路径
project_version=""                                       # 项目版本
tomcat_path=""                                           # Tomcat路径
restart_script="/local/script/restart_tomcat.sh"         # 重启脚本路径
log_file="/local/script/logs/upgrade.log"                # 日志文件路径

# 获取参数,并给变量赋值
while getopts p:r:t: OPT; do
    case ${OPT} in
      p)
        project_path=${OPTARG}
        ;;
      r)
        project_version=${OPTARG}
        ;;
      t)
        tomcat_path=${OPTARG}
        ;;
    esac
done

# 检查是否设置了项目路径和Tomcat路径
echo ${project_path:?"-p Option:Missing Project Path"} > /dev/null
echo ${tomcat_path:?"-t Option:Missing Tomcat Path"} > /dev/null

# 查看是否有Tomcat重启脚本
if [ ! -e ${restart_script} ]; then
    echo "Can‘t find /local/script/restart_tomcat.sh"
    exit 1
fi

# 查看是否有执行脚本的权限
if [ ! -x ${restart_script} ]; then
    chmod u+x ${restart_script} 
fi

# 判断项目目录是否合法
if [[ ! -d ${project_path} ]]; then
    echo "${project_path} is not a directory!"
    exit 2
fi

# 查看是否存在日志目录,不存在创建
if [[ ! -e $(dirname ${log_file}) ]]; then
    echo "No directory of upgrade.log: $(dirname ${log_file})"
    mkdir $(dirname ${log_file})
    # exit 3
fi

# 开始升级
echo ---------------------------------------------------------------------------->> ${log_file}
echo "upgrade_title:$(date +%Y-%m-%d" "%H:%M:%S) svn up ${project_path}" >> ${log_file}
echo -e "upgrade_content:\c" >> ${log_file}
if [ ${project_version} ]; then
    svn up -r ${OPTARG} ${project_path} >> ${log_file} 2>&1 && ${restart_script} ${tomcat_path}    
else
    svn up ${project_path} >> ${log_file} 2>&1 && ${restart_script} ${tomcat_path}        
fi
echo ---------------------------------------------------------------------------->> ${log_file}
exit 0

 

Bash Shell-通用项目升级脚本