自定义公共bash脚本文件/usr/local/scripts/pub/functions
InBlock.gif##Log the memseges to facility
InBlock.gifLoging (){
InBlock.gif
if [ -z $LOGFILE ];then
InBlock.gif[ ! -d /var/log/scripts ] && mkdir -p /var/log/scripts
InBlock.gifLOGFILE=/var/log/scripts/`basename $0`.log
InBlock.giffi
InBlock.gif
if [
"$1"=
"-s" ];then
InBlock.gifecho
"`date +"%G-%m-%d %H:%M:%S
"` $2" | tee -a $LOGFILE &&
return 0
InBlock.giffi
InBlock.gifecho
"`date +"%G-%m-%d %H:%M:%S
"` $1" >> $LOGFILE
InBlock.gif}
InBlock.gif
##Upload file to ftp servers
InBlock.gif#You must Define variables needed,eg:
InBlock.gif#                FTPSERVER=192.168.80.220
InBlock.gif#                FTPLOGIN=backup
InBlock.gif#                FTPASSWORD=hc360bak
InBlock.gif#                FTP_SUBDIR=$1
InBlock.gif#                CMD_LIST=( ls
"lcd $1"    )
InBlock.gif
ftp_f (){
InBlock.gif#SUBDIR=$FTP_SUBDIR
InBlock.gif
if [ -z $CMD_LIST    ];then
InBlock.gifLoging -s    
"WARNING:The CMD_LIST is null,and nothing can be excuted."
InBlock.giffi
InBlock.gif
[ -z $FTPSERVER ] && Loging -s
"WARNING:The FTPSERVER is null,and the ftp server mybe not login."
InBlock.gif[ -z $FTPLOGIN ] && Loging    -s    
"WARNING:The FTPLOGIN is null,and the ftp server mybe not login."
InBlock.gif[ -z $FTPPASSWORD ] && Loging -s
"WARNING:The FTPPASSWORD is null,and the ftp server mybe not login."
InBlock.gifecho
"machine $FTPSERVER login $FTPLOGIN    password \"$FTPPASSWORD\"" >~/.netrc
InBlock.gifchmod 600 ~/.netrc
InBlock.gif
#Create command list file
InBlock.gifi=0
InBlock.gif
while [ $i -lt ${#CMD_LIST[*]} ]
InBlock.gif
do
InBlock.gifecho
"${CMD_LIST[${i}]}" >>~/.cmd_list
InBlock.giflet i=$i+1
InBlock.gifdone
InBlock.gif
[ $? -eq 0 ] && Loging
"INFO:Create comand file \"cmd_list\" is successful."
InBlock.gif
#ensure ftp can be closed
InBlock.gifecho
"bye" >>~/.cmd_list
InBlock.gif
ftp $FTPSERVER    <~/.cmd_list
InBlock.gif
if [ $? -eq 0 ];then
InBlock.gifLoging
"INFO:Exec command as \"cmd_list\"    is complete."
InBlock.gif
else
InBlock.gifLoging
"ERROR:Upload files to FTP is failure!" &&
return 1
InBlock.giffi
InBlock.gif
#clean temp file
InBlock.gif#rm -rf ~/.netrc
InBlock.gifrm -rf ~/.cmd_list
InBlock.gif
return 0
为了减少重复代码,编写之上两个通用函数,以备调用。下面是deploy.sh脚本。主要是针对tomcat的war包,进行自动备份、下载、更新、回滚等操作。具体看见脚本中help()的输出信息。
/usr/local/scripts/pub/deploy.sh
InBlock.gif#!/bin/sh
InBlock.gif
if [ ! -f /usr/local/scripts/pub/functions ];then
InBlock.gifecho
"ERROR:/usr/local/scripts/pub/functions is not exist,and exit!" && exit 1
InBlock.giffi
InBlock.gif. /usr/local/scripts/pub/functions
InBlock.gif
## initcial scipts variables
InBlock.gifinit_v(){
InBlock.gif
local PROJECT=
InBlock.gif
if [ -z $PROFILE ];then
InBlock.gifPROFILE=/etc/profile.d/deploy.sh
InBlock.giffi
InBlock.gif
if [ -z $1 ];then
InBlock.gifLoging
"ERROR:Init error for project is not speciafied." &&
return 1
InBlock.giffi
InBlock.gif#The $PROFILE
is not exist and create it!
InBlock.gif
if [ ! -f $PROFILE ];then
InBlock.gifecho
"export D_${1}=`date +"%G%m%d
"`:0" >>$PROFILE
InBlock.giffi
InBlock.gif#Import the project var
InBlock.gif. $PROFILE
InBlock.gif
#
if project
is not exist and create var
InBlock.gifeval PROJECT=\$D_$1
InBlock.gif
if [ -z $PROJECT ];then
InBlock.gifecho
"export D_${1}=`date +"%G%m%d
"`:0" >>$PROFILE
InBlock.gifPROJECT=
"`date +"%G%m%d
"`:0"
InBlock.giffi
InBlock.gif
#export the var
InBlock.gifDATE=`echo $PROJECT|awk -F: '{print $1}'`
InBlock.gifCOUNT=`echo $PROJECT|awk -F: '{print $2}'`
InBlock.gif
if [
"$DATE" != `date +
"%G%m%d"` ];then
InBlock.gifLoging -s
"INFO:The date is expire,and initate date and count."
InBlock.gifDATE=`date +
"%G%m%d"`
InBlock.gifCOUNT=0
InBlock.giffi
InBlock.gif
export DATE COUNT
InBlock.gif
}
InBlock.gif## Write variable to profile config file
InBlock.gifwrite_v(){
InBlock.gif
if [ -z $PROFILE ];then
InBlock.gifPROFILE=/etc/profile.d/deploy.sh
InBlock.giffi
InBlock.gifsed -i /D_${1}/s/=.*/=${DATE}:$COUNT/g    $PROFILE
InBlock.gif}
InBlock.gif
##Define help function
InBlock.gifhelp () {
InBlock.gifecho -e
"Usage:deploy.sh [command] [project name]\n"
InBlock.gifecho -e
"Usage:deploy.sh roll number [project name]"
InBlock.gifecho -e
"Command:"
InBlock.gifecho -e
"\tdeploy                     Deploy the project from ftp downloading *.war,and deploying. "
InBlock.gifecho -e    
"\tdeployb                     Backup the old project files or dirs,first,And than deploy the war from remote."
InBlock.gifecho -e
"\tbackup                     Backup the old project files or dirs."
InBlock.gifecho -e
"\troll:number                        Roll back the backup files    from backup ftp,the number default is last count of backup file"
InBlock.gifecho -e
"eg.:"
InBlock.gifecho -e
"\tdeploy.sh    deploy info"
InBlock.gif}
InBlock.gif
##Download file from remote server.
InBlock.gifdownload_f (){
InBlock.gif
InBlock.gif
if [ -z $1 ];then
InBlock.gifLoging    
"ERROR:Please speciafied argument for download_f func." &&
return 1
InBlock.giffi
InBlock.gif
local R_PATH=`dirname $1`
InBlock.giflocal R_FILENAME=`basename $1`
InBlock.giflocal L_PATH=/tmp
InBlock.gif
Loging -s
"INFO:Begin download project file from remote server."
InBlock.gifCMD_LIST=(
"bin"
"cd $R_PATH"
"lcd $L_PATH"
"get $R_FILENAME"
"bye" )
InBlock.gifftp_f
InBlock.gif
if [ $? -ne 0 ];then
InBlock.gifLoging -s
"ERROR:Download $R_FILENAME is failure!" &&
return 1
InBlock.giffi
InBlock.gif
return 0
InBlock.gif}
InBlock.gif
InBlock.gif##Shutdown server
InBlock.gifsrv_shutd (){
InBlock.gif#kill mutipl process of server
InBlock.gif
for i
in `ps -ef|grep $1|grep -v grep|awk '{print \$2}'`
InBlock.gif
do
InBlock.gifkill -9 $i
InBlock.gifdone
InBlock.gif[ $? -eq 0 ] && Loging
"INFO:Shutdown server is successfull!"
InBlock.gif
return 0
InBlock.gif}
InBlock.gif
InBlock.gifsrv_start(){
InBlock.gif
if [ -z $TOMCAT_HOME ];then
InBlock.gifTOMCAT_HOME=/usr/local/tomcat6
InBlock.giffi
InBlock.gif#export java envirment variables
InBlock.gif. /etc/profile.d/java.sh
InBlock.gif
##clear server cache files
InBlock.gifrm -rf $TOMCAT_HOME/work/*
InBlock.gif
$TOMCAT_HOME/bin/startup.sh
InBlock.gif}
InBlock.gif
InBlock.gif##Backup the old directory
InBlock.gifbackup_f (){
InBlock.gif
#define the variables of function
InBlock.giflocal BACKUP_BASE_PATH=`dirname $1`
InBlock.giflocal BACKUP_DIR=`basename $1`
InBlock.giflocal BACKUP_FILE=${BACKUP_DIR}.bak${COUNT}.tar.gz
InBlock.giflocal EXCLUDE=
InBlock.giflocal BACKUP_COMMAND=
"tar -C $BACKUP_BASE_PATH -zcvf $BACKUP_FILE $BACKUP_DIR    $EXCLUDE"
InBlock.gif
if [ ! -d $1 -a ! -f $1 ];then
InBlock.gifLoging    
"ERROR:The $1 directory or file is not exsit! " &&
continue
InBlock.giffi
InBlock.gif
if [ -z
"$BACKUP_COMMAND" ];then
InBlock.gifLoging    
"ERROR:Backup command is not defined!" &&
return 1
InBlock.giffi
InBlock.gif
#execute command
InBlock.gifecho $BACKUP_COMMAND
InBlock.gif$BACKUP_COMMAND    2>&1 >/dev/
null
InBlock.gif
if [ $? -eq 0 ];then
InBlock.gifLoging    -s
"INFO: $BACKUP_FILE     is made successful!"
InBlock.gif
else
InBlock.gifLoging    -s
"ERROR: $BACKUP_FILE is made    failure!" &&
return 1
InBlock.giffi
InBlock.gif
#upload file to remote ftp server
InBlock.gifCMD_LIST=(
"mkdir $DATE"
"cd $DATE"
"mkdir backup"
"cd backup"
"put $BACKUP_FILE"
"bye" )
InBlock.gifftp_f
InBlock.gif
if [ $? -eq 0 ];then
InBlock.gifLoging    -s
"INFO:Backup $BACKUP_FILE to remote is successful!"
InBlock.gifrm -rf $BACKUP_FILE
InBlock.gif
else
InBlock.gifLoging -s
"ERROR:Backup $BACKUP_FILE is failure!" &&
return 1
InBlock.giffi
InBlock.gif
return 0
InBlock.gif
}
InBlock.gif
InBlock.gif#Roll backup file    from ftp server,usage: roll_f <project> <backup count>
InBlock.gifroll_f (){
InBlock.giflocal PORJECT=$1
InBlock.giflocal ROLL_COUNT=$2
InBlock.gif
if [ -z $ROLL_COUNT ];then
InBlock.giflet ROLL_COUNT=$COUNT-1
InBlock.giffi
InBlock.gif
InBlock.gif#Download backup files from remote ftp.
InBlock.gifdownload_f $DATE/backup/${PORJECT}.bak$ROLL_COUNT.tar.gz
InBlock.gif
if [ ! -f /tmp/${PORJECT}.bak$ROLL_COUNT.tar.gz ];then
InBlock.gifLoging
"ERROR:Roll back failuer,mybe the /tmp/${PORJECT}.bak$ROLL_COUNT.tar.gz is not exist!"
InBlock.gif
return 1
InBlock.giffi
InBlock.gif
rm -rf /tmp/$PORJECT
InBlock.gif
tar -C /tmp -zxvf /tmp/${PORJECT}.bak$ROLL_COUNT.tar.gz    >>/dev/
null
InBlock.gif
if [ ! -d /tmp/$PORJECT ];then
InBlock.gifLoging    
"ERROR:The /tmp/$PORJECT is not exsit.Exit!"
InBlock.gif
return 1
InBlock.giffi
InBlock.gif
#Shutdown    server of tomcat
InBlock.gifsrv_shutd tomcat
InBlock.gif
if [ $? -ne 0 ];then
InBlock.gifLoging
"ERROR:Shutdown server failure."    
InBlock.gif
return 1
InBlock.giffi
InBlock.gif
#Clear apps files
InBlock.gif
rm -rf $APPS_BASE/$PORJECT
InBlock.gifrm -rf $APPS_BASE/${PORJECT}.war
InBlock.gifmv /tmp/$PORJECT $APPS_BASE/
InBlock.gif
srv_start
InBlock.gif
if [ $? -eq 0 ];then
InBlock.gifLoging
"INFO:Move roll back files is successfull!"
InBlock.gif
else
InBlock.gifLoging -s
"ERROR:roll back failure!" &&
return 1
InBlock.giffi
InBlock.gif
}
InBlock.gif
InBlock.gif##Deploy
new files to server
InBlock.gifdeploy_f (){
InBlock.giflocal PROJECT=$1
InBlock.gif#Download
new files package from remote ftp.
InBlock.gif
#remove old app file and then download
new file
InBlock.gif#rm -rf /tmp/$PROJECT.war
InBlock.gifdownload_f $DATE/$PROJECT.war
InBlock.gif
if [ ! -f /tmp/${PROJECT}.war ];then
InBlock.gifLoging
"ERROR:deploy failuer,mybe the /tmp/$PROJECT.war is not exist!"
InBlock.gif
return 1
InBlock.giffi
InBlock.gif
srv_shutd tomcat
InBlock.gif
if [ $? -ne 0 ];then
InBlock.gifLoging    -s
"ERROR:Shutdown server failure."
InBlock.gif
return 1
InBlock.giffi
InBlock.gif
#Clear the project
in server
InBlock.gifrm -rf $APPS_BASE/$PROJECT
InBlock.gifrm -rf $APPS_BASE/$PROJECT.war
InBlock.gif
mv /tmp/$PROJECT.war $APPS_BASE/
InBlock.gif
#Start server
InBlock.gifsrv_start ||
return 1
InBlock.gifLoging -s
"INFO:Start server is ok!"
InBlock.gif}
InBlock.gif
InBlock.gifset_var () {
InBlock.gif##Start scripts
InBlock.gif#FTPSERVER=101.251.113.247
InBlock.gif#FTPLOGIN=hdong
InBlock.gif#FTPPASSWORD=
"ECHwey81"
InBlock.gif
##MMT ftp authentication
InBlock.gif
FTPSERVER=101.251.113.247
InBlock.gifFTPLOGIN=zhounan
InBlock.gifFTPPASSWORD=
"123ZXC,./"
InBlock.gif
CMD_LIST=( ls
"lcd $1"    )
InBlock.gifPROFILE=/etc/profile.d/deploy.sh
InBlock.gif
if [ -Z $TOMCAT_HOME    ];then
InBlock.gifread -t20 -p
"The variable $TOMCAT_HOME is null,Please specify the TOMCAT_HOME path:" TOMCAT_HOME
InBlock.giffi
InBlock.gif
TOMCAT_HOME=${TOMCAT_HOME:-
"/usr/local/tomcat6"}
InBlock.gif
read -t10 -p
"The apps default base path is \$TOMCAT_HOME/webapps,are you sure change it (Y/N):" FG
InBlock.gif
case $FG
in
InBlock.gif
y|Y|yes|YES|Yes) read -t 30 -p
"Please input:" APPS_BASE ;;
InBlock.gif
*) APPS_BASE=$TOMCAT_HOME/webapps
InBlock.gifesac
InBlock.gif
}
InBlock.gif
###############################################Debug###############
InBlock.gif
#######################end#######################################
InBlock.gif
InBlock.gifcd ~
InBlock.gif
#Initate variables
InBlock.gif
init_v $2
InBlock.gif
Loging -s
"INFO:Initated variables complete.DATE=$DATE,COUNT=$COUNT."
InBlock.gif
InBlock.gif
case $1
in
InBlock.gif    
"deploy")shift
InBlock.gif                        
if [ -z    $1    ];then
InBlock.gif                             help
InBlock.gif                        fi
InBlock.gif                        #Define variables
InBlock.gif                        set_var
InBlock.gif                        #Start deploy war-file
InBlock.gif                        deploy_f $1
InBlock.gif                        
if [ $? -eq 0 ];then
InBlock.gif                        Loging -s
"INFO:Complete depoy successfull!"
InBlock.gif                        fi
InBlock.gif                        ;;
InBlock.gif    
"backup") shift
InBlock.gif                        
if [ -z    $1    ];then
InBlock.gif                             help
InBlock.gif                         fi
InBlock.gif                        #Define variables
InBlock.gif                        set_var
InBlock.gif                        #Start backup old project
InBlock.gif                         backup_f $TOMCAT_HOME/webapps/$1
InBlock.gif                        
if [ $? -eq 0 ];then
InBlock.gif                            Loging -s
"INFO:Complete backup old project successfull!"
InBlock.gif                        #Write variables
InBlock.gif                         let COUNT=$COUNT+1
InBlock.gif                         write_v
InBlock.gif                         fi
InBlock.gif                         ;;
InBlock.gif    
"deployb")shift
InBlock.gif                        
if [ -z    $1    ];then
InBlock.gif                             help
InBlock.gif                         fi
InBlock.gif                        
InBlock.gif                         #Define variables
InBlock.gif                         set_var
InBlock.gif                         #Start backup old project
InBlock.gif                            backup_f $TOMCAT_HOME/webapps/$1
InBlock.gif                        
InBlock.gif                        
if [ $? -eq 0 ];then
InBlock.gif                            Loging -s
"INFO:Complete backup old project successfull!"
InBlock.gif                         #Write variables
InBlock.gif                         let COUNT=$COUNT+1
InBlock.gif                         write_v
InBlock.gif                         fi
InBlock.gif
                         #Start deploy war-file
InBlock.gif                        deploy_f $1
InBlock.gif                        
if [ $? -eq 0 ];then
InBlock.gif                        Loging -s
"INFO:Complete depoy successfull!"
InBlock.gif                        fi
InBlock.gif                         ;;
InBlock.gif        
"roll") shift
InBlock.gif                        
InBlock.gif                        
if [ -z    $1    ];then
InBlock.gif                             help
InBlock.gif                         fi
InBlock.gif                        
InBlock.gif                        #Define variables
InBlock.gif                        set_var    
InBlock.gif                        #Start Roll back from ftp server.
InBlock.gif                         roll_f $1 $2
InBlock.gif
                        
if [ $? -eq 0 ];then
InBlock.gif                            Loging -s
"INFO:Complete roll back    successfull!"
InBlock.gif                         fi
InBlock.gif                        ;;
InBlock.gif                *) help&& exit 1
InBlock.gifesac