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相關下載下傳,詳見:
<a href="http://www.ttlsa.com/html/723.html" target="_blank">http://www.ttlsa.com/html/723.html</a>
rsync下載下傳安裝
rpm -qa | grep rsync
(如果沒有輸出的話,執行下面指令安裝,如果有,跳過)
yum install -y rsync
[/codesyntax]
inotify下載下傳
[codesyntax lang="php"]
4、安裝配置SVN
注:由于在A伺服器和B伺服器上都要安裝Svn client
是以,為了簡單起見,建議兩台伺服器上都完整安裝SVN
5、安裝配置rsync+inotify服務端
這裡根據自身情況,略加修改,具體内容如下
5.1、B伺服器(rsync daemon)
由于是将版本庫代碼同步至B伺服器,是以安裝rsync server端的任務就落在了B伺服器的身上
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 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
# 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
ok,這樣rsync server端就搞定了,接下來就是client端的腳本了
5.2、A伺服器版本庫
由于A伺服器端是源伺服器,是以inotify需要安裝在源伺服器上
# 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
6、通過svn自帶hooks實作自動update(本節内容是為svnsync準備,檔案内容可留白即可,如果不使用svnsync功能,可略過此節)
6.1、A伺服器,修改post-commit檔案
# 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=<code>/usr/local/bin/svnlook author -r $REV $REPOS</code>
DATE=<code>date '+%F'</code>
TIME=<code>date '+%F %T'</code>
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;
簡單描述下上述腳本的功能
首先post-commit腳本,最主要的作用就是等版本庫有人commit送出了以後進行的動作
這個腳本最先生成了一個以日期為檔案名的日志在$DIR/logs目錄下
然後分别記錄了時間,作者,以及svn updata的情況和svn log,svn diff的資訊
注:記錄日志是為了友善查詢排錯使用
6.2、B伺服器,修改start-commit和pre-revprop-change
# cd /data/svn/test/hooks/
# vi start-commit
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
将檔案内容修改為上述内容
# vi pre-revprop-change
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
7、通過rsync和inotify實作A伺服器與B伺服器同步
各元件安裝完畢,接下來要自己寫一個rsync背景監控腳本,内容如下
# vi /usr/local/src/scripts/rsync.sh
#!/bin/bash
HOST=107.6.15.235
/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 ];
echo "remote rsync failed" >> ${RSYNC_LOGFILE};
echo "-----------------------------------END-------------------------------------" >> ${RSYNC_LOGFILE} 2>&1;
done
簡單說明一下腳本的功能,主要是兩個
a、由inotifywait監控${src}目錄的變化
b、将變化的檔案存入${file},傳遞給rsync進行同步(即增量同步),并将更新的檔案寫入日志
8、通過crontab定時備份SVN
# crontab -e
* * * * * /usr/local/src/scripts/svnsync.sh
# vi /usr/local/src/scripts/svnsync.sh
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
echo "svnsync successed" >> $DIR/logs/$DATE-log.txt;
echo "svnsync failed" >> $DIR/logs/$DATE-log.txt;
上述腳本主要功能簡介:
首先crontab每分鐘執行一次svnsync.sh腳本
腳本中是遠端同步本地版本庫(注:非working copy,即svn的db檔案)
同步完成後将腳本執行結果記錄進日志
本文轉自淺景塵51CTO部落格,原文連結:http://blog.51cto.com/857803451/1957874 ,如需轉載請自行聯系原作者