# vi /etc/rsyncd.confuid = nobodygid = nobodyuse chroot = nomax connections = 100timeout = 600pid file = /var/run/rsyncd.pidlock file = /var/run/rsyncd.locklog file = /var/log/rsyncd.log[test]path = /data/www/test/ignore errorsread only = nolist = nohosts allow = 192.168.1.0/255.255.255.0auth users = testsecrets 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文件) 同步完成后将脚本执行结果记录进日志