脚本实现目的:
应用上线时,通过脚本同步代码到生产环境。
脚本思路:
在测试环境启用rsync服务,在生产环境使用脚本同步代码,并排除不需要同步的文件。
可以通过参数指定需要同步的应用,或通过注释配置文件排除不同步的应用。
可以通过配置文件定义同步之前是否备份当前文件。
同步完成后检测同步是否成功,若失败,则进入循环同步模式,并发邮件给管理员。
通过配置文件的方式传递给脚本,以方便批量部署。
脚本内容:
#!/bin/bash #use for rsync #created in 2013-05-21 source ~/.bash_profile &>/dev/null dir=/tol/script dt2=`date +"%Y-%m-%d"` conf=$dir/rsync_web.conf host=`/sbin/ifconfig |grep "inet addr"|cut -d ':' -f2 |awk '{print $1}'|head -1` backupdir="/tol/backup/rsync_web/" ls $backupdir &> /dev/null || mkdir -p $backupdir ls $dir/logs &> /dev/null || mkdir -p $dir/logs function shijian () { dt=`date +"%Y-%m-%d-%H:%M:%S"` } if test ! -f $conf ; then echo "The $conf is not exist" exit 0 fi case $1 in "") ;; "$1") op=`cat $conf |grep "\<$1\>"|grep -v \#` if test ! -z "$op" ; then echo "$op" > $dir/$1-rsync.conf conf=$dir/$1-rsync.conf else echo "Usage $0 RSYNC_NAME" exit fi ;; esac while read line do kg=`echo "$line" |grep \#` if [ "$line" = "" ]; then continue elif test "$kg" ; then continue fi shijian name=`echo $line |awk -F\; '{print $1}'` name2=`echo $line |awk -F\; '{print $2}'` testip=`echo $line |awk -F\; '{print $3}'` desdir=`echo $line |awk -F\; '{print $4}'` exclude=`echo $line |awk -F\; '{print $5}'` option=`echo $line |awk -F\; '{print $6}'` num=`echo $line |awk -F\; '{print $7}'` log=$dir/logs/rsync-$name-$dt2.log backfile=$backupdir"$name"-$dt.tar.bz2 if test ! "$num" ; then find $backupdir -name "*tar.bz2*" -mtime +7 -exec rm -rf {} \; tar -jpcf $backfile --exclude="*tar.bz2*" --exclude="*.tbz2*" $desdir &> /dev/null fi while [ 1 -eq 1 ] do shijian echo "$dt begin rsync $name" >> $log /usr/bin/rsync $option $exclude $name2@$testip::$name2 $desdir >> $log if test `echo $?` -ne 0 ; then shijian echo "The $dt $host rsync $desdir from $testip is fail" | mail -s "check $host rsync" jiank echo "$dt $host rsync $desdir from $testip is fail" >> $log sleep 30 else chown -R tomcat.tomcat $desdir &> /dev/null echo "$dt $host rsync $desdir from $testip is ok" >> $log break fi done done < $conf
脚本配置文件:
tomcat;tomcat;172.18.32.213;/tol/htdocs/tomcat/ROOT;--exclude=/upload --exclude=/WEB-INF/classes/services.xml --exclude=/WEB-INF/classes/SystemGlobals.properties --exclude=/WEB-INF/web.xml;-vazu --delete --password-file=/etc/rsync.test.secrets;nobak;
rsync服务的搭建请参考:http://rmeos.blog.51cto.com/761575/1423455
rsync命令的相关用法请参考:http://rmeos.blog.51cto.com/761575/1423458
转载于:https://blog.51cto.com/rmeos/1423465