#!/bin/sh # desc 项目上线脚本 # time 2015-12-18 # author Devil # version 2.0 echo "---------- 准备中... ----------" user=`whoami` date=$(date +%Y%m%d%H%M%S) time=$(date +%Y-%m-%d" "%H:%M:%S) name="fangao" tar_name="${date}_${name}.tar.gz" test_dir="test/test_app" bak_dir="bak" bak_log_dir="bak_log/"$(date +%Y/%m) date_name=$(date +%d)".txt" time_start=$(date +%s) # 日志写入方法 function LogInsert() { echo "user:${user}, date:${time}, msg:${1}, code:${2}" >> "${bak_log_dir}/${date_name}" if [ $2 == "success" ] then echo -e "\e[1;32m ${1} \e[0m" else echo -e "\e[1;31m ${1} \e[0m" exit fi } # 恢复脚本是否正在运行 is_restore=$(ps -ef | grep "restore" | grep -v grep | wc -l) if [ $is_restore != 0 ] then LogInsert "恢复脚本正在运行,请先停止再上线项目" "error" fi # 当前脚本是否在运行多个 is_online=$(ps -ef | grep "online" | grep -v grep | wc -l) if [ $is_online -gt 2 ] then LogInsert "当前脚本正在多处运行,请确认一人操作" "error" fi # 备份路径不存在则创建 if [ ! -x "$bak_dir" ] then mkdir -p $bak_dir fi # 日志目录不存在则创建 if [ ! -x "$bak_log_dir" ] then mkdir -p $bak_log_dir fi # 需要上线的目录不存在则退出 if [ ! -x "$test_dir" ] then LogInsert "$test_dir 目录不存在" "error" fi echo "---------- 准备结束 ----------" echo -e echo "---------- 确定需要上线么?确定:Y 取消:N ----------" read start_state if [[ $start_state != "Y" ]] then LogInsert "你取消了操作" "error" fi echo -e echo "---------- 开始压缩,请稍候... ----------" tar -cf $tar_name --exclude=img --exclude=audio --exclude=tpl_c --exclude=db_log $name if [ $? == 0 ] then LogInsert "压缩成功" "success" echo "压缩包文件名 ${tar_name}" >> "${bak_log_dir}/${date_name}" else LogInsert "压缩失败" "error" fi echo "---------- 压缩结束 ----------" echo -e echo "---------- 开始移动压缩包,请稍候... ----------" mv $tar_name $bak_dir if [ $? == 0 ] then LogInsert "压缩包移动成功" "success" else LogInsert "压缩包移动失败" "error" fi echo "---------- 压缩包移动结束 ----------" echo -e echo "---------- 开始更新项目代码,请稍候... ----------" cd $test_dir git checkout master git pull origin master if [ $? == 0 ] then echo -e "\e[1;32m git更新master成功 \e[0m" else echo -e "\e[1;31m git更新失败 \e[0m" exit fi echo "---------- 项目代码更新结束 ----------" echo -e echo "---------- 确定迁移项目么?确定:Y 取消:N ----------" read success if [[ $success != "Y" ]] then echo -e "\e[1;31m 你终止了迁移操作 \e[0m" exit fi echo "---------- 开始迁移项目,请稍候... ----------" cp -r `ls | grep -v config | xargs` ../../$name cp_state=$? cd ../../ if [ $cp_state == 0 ] then time_total=$[$(date +%s)-$time_start] LogInsert "项目迁移成功 [耗时:${time_total}秒]" "success" else LogInsert "项目迁移失败" "error" fi echo "---------- 项目迁移结束 ----------" echo -e echo -e "\e[1;36m config目录下的所有文件都未迁移,如有文件需要迁移,请手动操作 \e[0m" echo -e
发表评论: