#!/bin/bash
#20150619重新定义编辑升级脚本
#update.sh
#1、更新升级
#2、回滚升级
# 1、查看回滚目录
# 2、选择回滚目录
#3、退出升级
#update|rollback
#ROLLBACK_HOME_DOC目录根据自己需要进行修改
NEW_DOC=$1
OLD_DOC=$2
if [ $# -lt 2 ];then
echo "参数错误"
echo "格式: ./update.sh [NEW_DOC] [OLD_DOC] [update/rollback]"
else
UPDATE_TIME=`date +%Y%m%d%H%M%S`
echo "格式: ./update.sh [NEW_DOC] [OLD_DOC] [update/rollback]"
read -p "输入项目名字: " PROJECT_NAME
ROLLBACK_HOME_DOC=/data/rollback/$PROJECT_NAME
function update(){
if [ -d $NEW_DOC ] && [ -d $OLD_DOC ];then
####################################################################
##创建回滚备份目录
for DOC in `find $NEW_DOC -type d`
do
ROLLBACK_DOC=`echo $DOC|sed 's#'$NEW_DOC'#'$UPDATE_TIME'/#'`
mkdir -p /$ROLLBACK_HOME_DOC/$ROLLBACK_DOC/
#echo $PROJECT_NAME
done
####################################################################
for NEW_FILE in `find $NEW_DOC -type f`
do
for OLD_FILE in `echo $NEW_FILE|sed 's#'$NEW_DOC'#'$OLD_DOC'#'`
do
if [ -f $OLD_FILE ];then
ROLLBACK_FILE=`echo $OLD_FILE|sed 's#'$OLD_DOC'#'$ROLLBACK_HOME_DOC'/'$UPDATE_TIME'/#'`
\cp $OLD_FILE $ROLLBACK_FILE && echo "代码备份"
\cp $NEW_FILE $OLD_FILE && echo "代码更新替换"
else
if [ -d $(dirname $OLD_FILE) ];then
\cp $NEW_FILE $OLD_FILE && echo "新代码文件复制完成"
R_F=`echo $NEW_FILE|sed 's#'$NEW_DOC'#'$ROLLBACK_HOME_DOC'/'$UPDATE_TIME'/#'`
\cp $NEW_FILE $R_F"."NEW && echo "新文件生成回滚记录"
else
mkdir -p $(dirname $OLD_FILE) && echo "创建文件目录成功"
R_D=`dirname $NEW_FILE|sed 's#'$NEW_DOC'#'$ROLLBACK_HOME_DOC'/'$UPDATE_TIME'/#'`
mkdir -p $R_D"."NEW
\cp $NEW_FILE $OLD_FILE && echo "新代码文件复制完成"
R_F=`echo $NEW_FILE|awk -F "/" '{print $NF}'`
\cp $NEW_FILE $R_D"."NEW/$R_F"."NEW && echo "新文件生成回滚记录"
fi
fi
done
done
else
echo "参数有误"
fi
}
function rollback_list(){
if [ -d $ROLLBACK_HOME_DOC ];then
echo "最近10个升级备份记录:"
ls -lsth $ROLLBACK_HOME_DOC/ |awk '{print $NF}'|sed '1d'|head -10|sed 's#^#'$ROLLBACK_HOME_DOC/'#'|awk '{print "["NR"]",$0"/"}'
else
echo "此项目未进行升级备份"
exit
fi
}
function rollback_num(){
R_=`ls -lsth $ROLLBACK_HOME_DOC/ |awk '{print $NF}'|sed '1d'|sed 's#^#'$ROLLBACK_HOME_DOC/'#'|awk '{print "["NR"]",$0"/"}'|awk '{print $2}'|sed -n ''$ROLLBACK_ACTION'p'`
if [ `find $R_ -type f|wc -l` -gt 0 ];then
###回滚升级
for R_FILE in `find $R_ -type f|grep -v NEW`
do
O_FILE=`echo $R_FILE|sed 's#'$R_'#'$OLD_DOC'#'`
\cp $R_FILE $O_FILE && echo "代码回滚成功"
done
###删除新增
for R_FILE in `find $R_ -type f|grep NEW`
do
O_FILE=`echo $R_FILE|sed 's#'$R_'#'$OLD_DOC'#'|sed 's#'\.NEW'##'`
if [ -f $O_FILE ];then
rm $O_FILE && echo "新增移出"
fi
done
for R_DIR in `find $R_ -type d |grep NEW`
do
O_DIR=`echo $R_DIR|sed 's#'$R_'#'$OLD_DOC'#'|sed 's#'\.NEW'##'`
if [ -d $O_DIR ];then
rm -rf $O_DIR && echo "新增目录移出"
fi
done
rm -rf $R_ && echo "移出回滚备份目录"
else
echo "此升级备份丢失,回滚失败"
fi
}
function rollback(){
read -p "请选择回滚历史: " ROLLBACK_ACTION
case $ROLLBACK_ACTION in
1)
rollback_num
;;
2)
rollback_num
esac
}
case $3 in
update)
update
;;
rollback)
rollback_list
rollback
;;
*)
echo "请选执行动作:
1、代码升级
2、代码回滚
3、退出"
read -p "请输入执行动作: " ACTION_1
case $ACTION_1 in
1)
update
;;
2)
rollback_list
rollback
;;
3)
exit
;;
esac
esac
fi