linux下svn+rsync+inotify实现代码自动同步

1、环境简介 操作系统:CentOS release 5.7 (Final) Apache版本:Apache/2.2.21 (Unix) Subversion版本:svn, version 1.7.2 Rsync版本:rsync  version 3.0.6  protocol version 30 Inotify版本:inotifywait 3.14 SVN Server端服务器:以下统称A 同步测试服务器:以下统称B   2、需求及流程简介 由于程序员协同开发,彼此之间开发习惯各有不同,经常会出现大家误删改他人代码的情况,对线上生产环境的稳定造成了极大的影响。因此特请出SVN来负责代码版本控制,以解决上述问题。 但是简单的代码控制要增加很多不必要的人工成本,如:手动提交、检出、发布、测试等,所以作为运维人员,就自然而然想到有什么办法可以让其将以上操作都自动执行完成。固有了如下需求。 1、代码commit至A服务器后,自动update到代码的working copy 2、update后需直接远程同步至B服务器进行测试 3、定期备份svn服务器代码库文件至备份服务器(此处与B公用一台) 4、待测试完毕后,通过程序控制将working copy文件,自动发布到生产环境(待开发) 以上前3步均是由系统级操作完成,第4步需要程序写出一个交互系统,来完成测试完毕发布的过程。本文档主要实现前3步。   3、准备 SVN相关下载,详见: http://www.ttlsa.com/html/723.html   rsync下载安装 [codesyntax lang="php"] rpm -qa | grep rsync (如果没有输出的话,执行下面命令安装,如果有,跳过) yum install -y rsync [/codesyntax] inotify下载 [codesyntax lang="php"] wget http://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz [/codesyntax] 4、安装配置SVN 详见: http://www.ttlsa.com/html/723.html 注:由于在A服务器和B服务器上都要安装Svn client 所以,为了简单起见,建议两台服务器上都完整安装SVN   5、安装配置rsync+inotify服务端 安装过程主要参考: http://kerry.blog.51cto.com/172631/734087 这里根据自身情况,略加修改,具体内容如下 5.1、B服务器(rsync daemon) 由于是将版本库代码同步至B服务器,所以安装rsync server端的任务就落在了B服务器的身上 [codesyntax lang="php"]
# vi /etc/rsyncd.conf

uid = nobody
gid = nobody
use chroot = no
max connections = 100
timeout = 600
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log
[test]
path = /data/www/test/
ignore errors
read only = no
list = no
hosts allow = 192.168.1.0/255.255.255.0
auth users = test
secrets file = /etc/rsyncd/test_server.pwd
[/codesyntax] [codesyntax lang="php"] # vi /etc/rsyncd/test_server.pwd test:test # chmod 600 /etc/rsyncd.conf /etc/rsyncd/test_server.pwd # echo "Welcome to use the rsync services\!" >> /var/rsyncd.motd # /usr/bin/rsync --daemon [/codesyntax] ok,这样rsync server端就搞定了,接下来就是client端的脚本了 5.2、A服务器版本库 由于A服务器端是源服务器,所以inotify需要安装在源服务器上   [codesyntax lang="php"] # cd /usr/local/src/tarbag # tar xvf inotify-tools-3.14.tar.gz -C ../software # cd ../software/inotify-tools-3.14 # ./configure && make && make install [/codesyntax]   6、通过svn自带hooks实现自动update(本节内容是为svnsync准备,文件内容可留空即可,如果不使用svnsync功能,可略过此节) 6.1、A服务器,修改post-commit文件 [codesyntax lang="php"] # cd  /data/svn/test/hooks/ # cp  post-commit.tmpl  post-commit # vi post-commit 清空所有信息,将如下信息替换 #!/bin/sh export LANG=en_US.UTF-8 REPOS="$1" REV="$2" AUTHOR=`/usr/local/bin/svnlook author -r $REV $REPOS` DATE=`date '+%F'` TIME=`date '+%F %T'` DIR=/data/svn/test/hooks PATH=/usr/local/bin LOCAL_WORKING_COPY=/data/svn_data/test/ SVN_SRC=/data/svn_data/test/ WEB_SRC=/data/www/test/ PASSWD=/etc/rsyncd/test.pwd DES=test USER=test SVN=$PATH/svn SVNSYNC=$PATH/svnsync RSYNC=/usr/bin/rsync RSYNC_LOGFILE=/var/log/rsyncd/${DATE}-rsync.log SVN_LOGFILE=/var/log/svn/${DATE}-svn.log SVN_LOGIN_INFO="--username tonyty163 --password ty1224" #SVN UPDATE echo "----------------------------------BEGIN------------------------------------" >> ${SVN_LOGFILE} echo "The following changes were made to the code:" >> ${SVN_LOGFILE} echo "[$TIME]--[$AUTHOR] svn commit file:" >> ${SVN_LOGFILE} echo "REV is $REV,REPOS is $REPOS" >> ${SVN_LOGFILE} $SVN update $SVN_LOGIN_INFO $LOCAL_WORKING_COPY >> ${SVN_LOGFILE} if [ "$?" = "0" ]; then echo "update successed" >> ${SVN_LOGFILE}; else echo "update failed" >> ${SVN_LOGFILE}; fi $SVN log $LOCAL_WORKING_COPY -v -r "$REV" $SVN_LOGIN_INFO >> ${SVN_LOGFILE} $SVN diff $LOCAL_WORKING_COPY -c "$REV" --no-diff-deleted $SVN_LOGIN_INFO >> ${SVN_LOGFILE} echo "------------------------------------END------------------------------------" >> ${SVN_LOGFILE} #Rsync TO WEB_SRC ${RSYNC} -aH --delete --progress --exclude=".svn/" ${SVN_SRC} ${WEB_SRC} &&  echo "local rsync succesed" >> ${RSYNC_LOGFILE} 2>&1 echo "------------------------------------END------------------------------------" >> ${RSYNC_LOGFILE} 2>&1; [/codesyntax ] 简单描述下上述脚本的功能 首先post-commit脚本,最主要的作用就是等版本库有人commit提交了以后进行的动作 这个脚本最先生成了一个以日期为文件名的日志在$DIR/logs目录下 然后分别记录了时间,作者,以及svn updata的情况和svn log,svn diff的信息 注:记录日志是为了方便查询排错使用     6.2、B服务器,修改start-commit和pre-revprop-change [codesyntax lang="php"] # cd /data/svn/test/hooks/ # vi start-commit   #!/bin/sh   USER="$2"   if [ "$USER" = "user" ]; then exit 0; fi   echo "Only the user user may change revision properties as this is a read-only, mirror repository." >&2 # All checks passed, so allow the commit. exit 1 [/codesyntax] 将文件内容修改为上述内容 [codesyntax lang="php"] # vi pre-revprop-change   #!/bin/sh   REPOS="$1" REV="$2" USER="$3" PROPNAME="$4" ACTION="$5"   if [ "$ACTION" = "M" -a "$PROPNAME" = "svn:log" ]; then exit 0; fi   echo "Changing revision properties other than svn:log is prohibited" >&2 exit 0   if [ "$USER" = "user" ]; then exit; fi   echo "Only the user user may change revision properties as this is a read-only, mirror repository." >&2 exit 1 [/codesyntax] 将文件内容修改为上述内容     7、通过rsync和inotify实现A服务器与B服务器同步 各组件安装完毕,接下来要自己写一个rsync后台监控脚本,内容如下 [codesyntax lang="php"] # vi /usr/local/src/scripts/rsync.sh #!/bin/bash HOST=107.6.15.235 SVN_SRC=/data/svn_data/test/ WEB_SRC=/data/www/test/ PASSWD=/etc/rsyncd/test.pwd DES=test USER=test DATE=`date '+%F'` RSYNC_LOGFILE=/var/log/rsyncd/${DATE}-rsync.log /usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format  '%T %w%f' -e modify,delete,create,attrib ${WEB_SRC} | while read  file do rsync -aH --delete --progress ${WEB_SRC} ${USER}@${HOST}::${DES} --password-file=${PASSWD} &&  echo "${file} rsync succesed" >> ${RSYNC_LOGFILE} 2>&1 if [ $? -ne 0 ]; then echo "remote rsync failed" >> ${RSYNC_LOGFILE}; fi echo "-----------------------------------END-------------------------------------" >> ${RSYNC_LOGFILE} 2>&1; done [/codesyntax] 简单说明一下脚本的功能,主要是两个 a、由inotifywait监控${src}目录的变化 b、将变化的文件存入${file},传递给rsync进行同步(即增量同步),并将更新的文件写入日志   8、通过crontab定时备份SVN [codesyntax lang="php"] # crontab -e * * * * * /usr/local/src/scripts/svnsync.sh   # vi /usr/local/src/scripts/svnsync.sh #!/bin/sh DATE=`date '+%F'` TIME=`date '+%F %T'` DIR=/data/svn/test/hooks PATH=/usr/local/bin SVN=$PATH/svn SVNSYNC=$PATH/svnsync MIRROR_SVN=http://192.168.1.204/svn/test/ SVN_LOGIN_INFO="--username user --password passwd" $SVNSYNC sync $MIRROR_SVN $SVN_LOGIN_INFO >> $DIR/logs/$DATE-log.txt if [ "$?" = "0" ]; then echo "svnsync successed" >> $DIR/logs/$DATE-log.txt; else echo "svnsync failed" >> $DIR/logs/$DATE-log.txt; fi [/codesyntax] 上述脚本主要功能简介: 首先crontab每分钟执行一次svnsync.sh脚本 脚本中是远程同步本地版本库(注:非working copy,即svn的db文件) 同步完成后将脚本执行结果记录进日志

转载于:https://my.oschina.net/766/blog/211441

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值