最近项目组在推行自动构建,在每次build完了之后,需要自动把文件部署到linux服务器上,因此根据项目的具体的情况,写了一个shell脚本,供自动构建工具来调用执行部署。
粗略的思路就是:停止系统-->备份现有的环境-->部署新build的文件-->修改需要修改的配置文件(如果需要)-->启动系统
大致脚本如下:
#!/bin/bashDAY=`date +%Y-%m-%d-%H:%M:%S`#the path for deleting the log and tmpAPP_MAIN_PATH=/data/yousystem/jboss-5.1.0.GA/server/default#the key word for searching the process idPID_MSG=yousystem/#msg of the file which is running now,backup it.APP_FILE_NAME1=yousystem.warAPP_FILE_PATH1=/data/yousystem/jboss-5.1.0.GA/server/default/deploy#msg of the start file which is used to start the systemAPP_START_FILE=app-run.shAPP_START_PATH=/data/yousystem#msg of the update fileBUILD_FILE_NAME1=yousystem.warBUILD_FILE_PATH=/data/backup/autobuild/build_files_backup#msg of the old files for backupOLD_FILE_PATH=/data/backup/autobuild/old_files_backup#path of the shell script logSHELL_LOG_PATH=/data/backup/autobuildecho '=============================adp==================================='>>$SHELL_LOG_PATH/log_build.logmkdir -p /data/backupmkdir -p /data/backup/autobuildmkdir -p /data/backup/autobuild/auto-build-scriptsmkdir -p /data/backup/autobuild/build_files_backupmkdir -p /data/backup/autobuild/old_files_backup#关闭正在运行的系统time_now_1=`date +%Y-%m-%d" "%H:%M:%S`echo $time_now_1 'Begin to kill the progress.....'>>$SHELL_LOG_PATH/log_build.logpid=`ps -ef|grep $PID_MSG|grep -v grep|awk '{print $2}'`if [ -n "$pid" ]thenecho 'The pid: ' $pid ' will be killed....'>>$SHELL_LOG_PATH/log_build.logkill -9 $pidfirm -rf $APP_MAIN_PATH/log/ $APP_MAIN_PATH/tmp/ $APP_MAIN_PATH/work/time_now_2=`date +%Y-%m-%d" "%H:%M:%S`echo $time_now_2 'End to kill the progress.....'>>$SHELL_LOG_PATH/log_build.log#备份现有的环境,具体的备份路径和范围由个人决定echo '*******************************************************'>>$SHELL_LOG_PATH/log_build.logtime_now_3=`date +%Y-%m-%d" "%H:%M:%S`echo $time_now_3 'Begin to backup the files: '$APP_FILE_NAME1>>$SHELL_LOG_PATH/log_build.logcp -r $APP_FILE_PATH1/$APP_FILE_NAME1 $OLD_FILE_PATH/$APP_FILE_NAME1-$DAYtime_now_4=`date +%Y-%m-%d" "%H:%M:%S`echo $time_now_4 'End to backup the files.....'>>$SHELL_LOG_PATH/log_build.log#备份完之后先删除掉现有的环境,备份哪些就删除哪些,主要是怕覆盖的时候有文件错误echo '*******************************************************'>>$SHELL_LOG_PATH/log_build.logtime_now_5=`date +%Y-%m-%d" "%H:%M:%S`echo $time_now_5 'Begin to delete the files: ' $APP_FILE_NAME1>>$SHELL_LOG_PATH/log_build.logrm -rf $APP_FILE_PATH1/$APP_FILE_NAME1time_now_6=`date +%Y-%m-%d" "%H:%M:%S`echo $time_now_6 'End to delete the files....'>>$SHELL_LOG_PATH/log_build.log#把最新build的文件拷贝到需要解压的目录echo '*******************************************************'>>$SHELL_LOG_PATH/log_build.logtime_now_7=`date +%Y-%m-%d" "%H:%M:%S`echo $time_now_7 'Begin to copy the build files: ' $BUILD_FILE_NAME1>>$SHELL_LOG_PATH/log_build.logcp -r $BUILD_FILE_PATH/$BUILD_FILE_NAME1 $APP_FILE_PATH1time_now_8=`date +%Y-%m-%d" "%H:%M:%S`echo $time_now_8 'End to copy the build files.....'>>$SHELL_LOG_PATH/log_build.log#删除环境上的缓存文件echo '*******************************************************'>>$SHELL_LOG_PATH/log_build.logtime_now_9=`date +%Y-%m-%d" "%H:%M:%S`echo $time_now_9 'Begin to backup the build files: ' $BUILD_FILE_NAME1>>$SHELL_LOG_PATH/log_build.logmv -f $BUILD_FILE_PATH/$BUILD_FILE_NAME1 $BUILD_FILE_PATH/$BUILD_FILE_NAME1-$DAYtime_now_10=`date +%Y-%m-%d" "%H:%M:%S`echo $time_now_10 'End to backup the build files.....'>>$SHELL_LOG_PATH/log_build.log#解压新build的文件(视个人项目而定,一般war包不需要解压,但是这里我们需要去修改war包中的配置文件)echo '*******************************************************'>>$SHELL_LOG_PATH/log_build.logtime_now_11=`date +%Y-%m-%d" "%H:%M:%S`echo $time_now_11 'Begin to uncompress the files: ' $BUILD_FILE_NAME1>>$SHELL_LOG_PATH/log_build.logcd $APP_FILE_PATH1mkdir unwarecho 'mkdir success'>>$SHELL_LOG_PATH/log_build.logcp -r $APP_FILE_NAME1 unwar/echo 'copy file success'>>$SHELL_LOG_PATH/log_build.logcd unwar/jar -xf $APP_FILE_NAME1echo 'uncompress file success'>>$SHELL_LOG_PATH/log_build.logrm -rf $APP_FILE_NAME1echo 'delete the copy file success'>>$SHELL_LOG_PATH/log_build.logcd $APP_FILE_PATH1rm -rf $APP_FILE_NAME1echo 'delete the update file success'>>$SHELL_LOG_PATH/log_build.logmv unwar $APP_FILE_NAME1echo 'change the folder name success'>>$SHELL_LOG_PATH/log_build.logtime_now_12=`date +%Y-%m-%d" "%H:%M:%S`echo $time_now_12 'End to uncompress the files:.....'>>$SHELL_LOG_PATH/log_build.log#重启系统echo '*******************************************************'>>$SHELL_LOG_PATH/log_build.logtime_now_13=`date +%Y-%m-%d" "%H:%M:%S`echo $time_now_13 'Begin to restart the system....'>>$SHELL_LOG_PATH/log_build.logecho 'Start File is: ' $APP_START_PATH/$APP_START_FILE>>$SHELL_LOG_PATH/log_build.logsh $APP_START_PATH/$APP_START_FILEtime_now_14=`date +%Y-%m-%d" "%H:%M:%S`echo $time_now_14 'End to restart the system....'>>$SHELL_LOG_PATH/log_build.log
来源:https://www.cnblogs.com/erichhuang/archive/2012/03/12/2392035.html