1 Description
this Shell script is the first generation of my written version of the Delta Code on-line script, now the code-on-line script has been updated several versions, focus on the technical focus is to visualize the code version of any rollback of the online system (Python language), These scripts ^_^ are gradually shared later . Although it seems that the script is very miserable, but at that time changed the company's online approach, from the handover of the manual on-line, change to script Automation online, is also a starting point and a turning point.
2 Module Analysis
This online script function is relatively simple, first see the online overall process:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/73/B4/wKiom1YErsPhTsYBAAIa7ka8mRY125.jpg "title=" 001. PNG "alt=" wkiom1yersphtsybaaia7ka8mry125.jpg "/>
The following is a detailed on-line flowchart:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/73/B4/wKiom1YErtjCZhi8AAM4js3B1pI907.jpg "title=" 002. PNG "alt=" wkiom1yertjczhi8aam4js3b1pi907.jpg "/>
3 Code Section
Please open this document using "notepad++" or another editor, "Alt+0" to make it easy to review after folding the function.
#!/bin/sh#set -x#auther: gushao#time: 2014-5-30# Setting the initial variable #$1 parameter description 1 front 2 backend 3 app# used to differentiate the test environment on-line and production environment online env_test=1case $env _test &NBSP;IN1) remote_ip=192.168.1.110parts=22# for server restart with Remote_ip10=192.168.1.110remote_ip11=192.168.1.111remote_ ip12=192.168.1.112parts_resin10=22parts_resin11=22parts_resin12=22;; 2) remote_ip=211.162.125.207parts=1573# for server restart with Remote_ip10=211.***.125.2**remote_ip11=211.***.125.***remote_ ip12=211.**.125.***parts_resin10=1573parts_resin11=10011parts_resin12=10012;; *) echo "input parameter is not available, select test or formal environment" exit 1;; esac# Intermediate Path variable edir= ' date +%y-%m-%d '/' date +%h%m%s ' rdate= ' date +%y-%m-%d_%h%m%s ' NDATE= ' date +% y-%m-%d\ %h:%m:%s ' det_ryd=/home/resin/project_name #本地同步代码DET_TOMCAT =/home/resin/tomcat/webappsdet_ app=/opt/tomcat_app/webapps# the directory that needs to be synced to when SSH synchronization is used det_ryd_home=/home/resin/det_tomcat_home=/home/resin/tomcatdet_ App_home=/opt/tomcat_appsource_ryd= "/home/demo/demo_Ryd/project_name " #存放需要升级的代码路径SOURCE_TOMCAT ="/home/demo/demo_tomcat/webapps "source_app="/Home /demo/demo_app/webapps "#日志保存位置LOG_RYD_BACK =/home/resin/log_back/project_namelog_tomcat_back=/home/resin/log_ back/tomcatlog_app_back=/home/resin/log_back/applog_path=/home/resin/codeupdate.log #日志文件位置 # will change the code to save from the server to the established path Reval_ryd=/home/resin/reval_back/ryd/rydreval_tomcat=/home /resin/reval_back/tomcat/tomcatreval_app=/home/resin/reval_back/app/app# Save the uploaded code code_ryd_back=/home/resin/ code_bak/ryd/rydcode_tomcat_back=/home/resin/code_bak/tomcat/tomcatcode_app_back=/hom/resin/code_bak/app/ appdebug=1# Debug Module, judge the result of code execution, output error message after instruction execution error function altert () {#usage:alert <$?> < object>if [ "$" -ne 0 ]then echo "[warn]:$2 Instruction execution Error" >&2 echo " Instruction execution error, please check the cause of the error, go back to the car " read xfi} #此函数的做用是遍历 the directory source, find out if there is a consistent file Det in the $ $ directory, and output different information # The variable for this function in this script is a file that needs to be uploaded, and the server synchronizes to the local file Function ergodic () {#echo "$4" Init_path=$3#usage: ergodic $1 $2 $3=$1 for file in ' ls $1 ' do #此段代码的目的, is the full file or directory path through text echo ${1}/${file} > /tmp /tmp_wenijian.txt #然后将路径替换 Delete, replace with ${det} path eval sed -i "s#${ init_path}## " /tmp/tmp_wenijian.txt unx= ' cat /tmp/tmp_wenijian.txt | head -n 1 ' det1=${2} $UNX path_a=${file} if [ -d ${1}/${file} ];then ergodic ${1} /${file} ${2} $3 $4 else #此代码段目的, for whatever level of directory you are looping to,Intercept echo ${1}/${file} > /tmp/tmp_ by completing the path wenijian.txt eval sed -i "s#${INIT_PATH}##" /tmp/tmp_wenijian.txt #unx是去除文件投的相对路径 unx= ' Cat /tmp/tmp_ Wenijian.txt | head -n 1 ' det1=${2} $UNX if [ -e ${DET1} ];then echo "[info][$NDATE] detected a change of file: $UNX" | tee -a $LOG _path # The purpose of this code is to copy the path of the file when speaking together &NBSP;&NBSP;&NBSP;&NBSP;CP&NBSP;&NBSP;--PARENT&NBSP;${2} $UNX $4_${rdate} else echo "[warn][$NDATE] detected a new file: $UNX" | tee -a $LOG _ path fi fi Done} #此函数的做用是遍历 the directory and compares the files in the $ $, whether they are consistent, output different information Function diff_ryd () {Init_path=$3 for file in ' ls $1 ' do echo ${1}/${file} > /tmp/tmp_wenijian.txt eval sed -i "s#${init_path}##" /tmp/tmp_wenijian.txt unx= ' cat /tmp/tmp_wenijian.txt | head -n 1 ' &nbsP;&NBSP;&NBSP;&NBSP;DET1=${2} $UNX path_a=${file} if [ -d ${1}/${file} ];then &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;DIFF_RYD&NBSP;${1}/${FILE}&NBSP;${2} $3 else echo ${1}/${file} > /tmp/tmp_wenijian.txt eval sed -i "s#${init_path}##" /tmp/tmp_ Wenijian.txt unx= ' Cat /tmp/tmp_ Wenijian.txt | head -n 1 ' &NBSP;DET1=${2} $UNX if [ -e ${det1} ];then #比较上传到生产的文件和上传文件的差异 diff -q ${DET1} ${1}/${file} | tee -a $LOG _path else echo "[error][$NDATE] The upload file is not synchronized with the local file, please detect" | tee -a $LOG _path echo "[warn][$NDATE] detected a new file: ${1}/${file}" | tee -a $LOG_path fi fi done} #project_name上传代码, which is a code source that needs to be uploaded, $ $ is the local synchronization directory det,$3 is the upper level directory of the synchronization code det_home#$4 is ${code_back},$5 is ${log_back},$6 is ${reval},$7 remote_ip ,$8 port# $INIT _path is the code that needs to be uploaded, $DET is the locally synchronized code, $DET _home is the upper level directory of the Synchronization Code function model_upload () {#同步上产脚本主体函数 # Pre-Create various custom catalogs test -e $2 | | mkdir -p $2test -e $4_${rdate} | | mkdir -p $4_${rdate}test -e $5 | | mkdir -p $5test -e $6_${rdate} | | mkdir -p $6_${rdate}test -e $1 | | echo "[error][$NDATE] ${2} deployment File not found" | tee -a $LOG _pathtest -e $1 | | exitecho '%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ' > $LOG _pathecho [info][$NDATE] Start automatic deployment ${2} version uploads | tee -a $LOG _pathchown -r resin:resin $1# the function I defined ergodic $1 $2 $1 $6chown -r resin:resin $2echo "--------------------------------" | tee -a $LOG _ pathecho "Determine ${2} server changes during two synchronizations" | tee -a $LOG _pathrsync - vzrtopg --delete --progress "-e ssh -p $PARTS" [email Protected] $REMOTE _ip:$2 $3 | tee -a $LOG _pathsleep 2echo "Second judgment" rsync -vzrtopg --delete --progress "-e ssh -p $PARTS" [email protected] $REMOTE _ip:$2 $3 | tee -a $ log_pathecho "End of judgment" &NBSP;&NBsp;| tee -a $LOG _pathecho "-------------------" | tee -a $LOG _pathecho "-----------------------------------------" echo "Synchronize local files to remote server" | tee -a $LOG _pathrsync -vzrtopg --progress "-e ssh -p $PARTS " $1 [email protected] $REMOTE _ip:$3 | tee -a $LOG _pathsleep 2echo "second time" rsync -vzrtopg --progress "-E ssh -p $PARTS " $1 [email protected] $REMOTE _ip:$3 | tee -a $LOG _pathecho "------------------------------------------" | tee -a $LOG _pathecho "Sync files to local server" | tee -a $LOG _pathrsync -vzrtopg --progress "-e ssh -p $PARTS" $1 $3 | tee -a $LOG _pathecho "--------------------------------------------------" # This action is used to compare whether the uploaded file and server are synchronized to the local file echo "Synchronize the shipping server with the local server" | tee -a $LOG _pathrsync -vzrtopg --delete --progress "-e ssh -p $PARTS" [email protected] $REMOTE _ip:$2 $3 | tee -a $LOG _pathsleep 2echo "Second time" rsync -vzrtopg --delete --progress "-e ssh -p $ PARTS " [email protected] $REMOTE _ip:$2 $3 | tee -a $LOG _pathecho "------------------------------" | tee -a $LOG _ pathecho "File detection started:" | tee -a $LOG _pathdiff_ryd $1 $2 $1 echo "End of File detection" | tee -a $LOG _pathecho "---- ---------------------------------------------------------------------------------" | tee -a $LOG _path# Save this long-passing file mv $1 $4_${rdate}echo "File upload End" #cat $LOG _pathcat $LOG _path > $5/${rdate}.log}case $1 in1) model_upload $SOURCE _ryd $DET _ryd $DET _ ryd_home $CODE _ryd_back $LOG _ryd_back $REVAL _ryd; 2) model_upload $SOURCE _tomcat $DET _tomcat $DET _tomcat_home $CODE _tomcat_back $LOG _tomcat_back $REVAL _tomcat ;; 3) model_upload $SOURCE _app $DET _app $DET _app_home $CODE _app_back $LOG _app _back $REVAL _app;; *) echo "Usage:$0 {1project_name|2tomcat|3app}" | tee -a $LOG _pathexit 1;; Esacexit 0
This shell script, is the initial practiced hand, it is difficult to see clearly is sure, the overall write sucks, but in small places some small tricks, really can let the small partners for reference.
Hope that through this series of articles to record their growth footsteps, no one can suddenly become Daniel, after the polishing of time, let us step-by-step change it!!!
Where the script is deficient:
1. Code readability is poor
2. Whether rsync execution is complete or not checked for correctness
3. Rsync does not perform timeout processing
This article is from the "Ops Road" blog, please be sure to keep this source http://vekergu.blog.51cto.com/9966832/1698102
Shallow incremental code on-line shell script-Rookie version