svn 結合rsync 的代碼發布系統,svnrsync
由開發提交到測試環境,經測試,在由營運統一上線。實驗需求一台測試伺服器,一台線上(生產環境)伺服器。測試伺服器上跑svn是開發用於代碼管理,而線上跑的svn是營運用來代碼上線的。結合rsync保持測試端的代碼與 svn的線上控制端(線上svn,在測試伺服器上的一個workcopy)的代碼保持一致。開發結合營運,並由營運周期性的提交代碼,如果有問題,復原,保證線上正常!!
svn伺服器上chackout 一個workcopy 在使用者端:(注意防火牆)
[root@v03-svn-client ~]# svn co svn://192.168.1.35/webtest client_webtestAuthentication realm: <svn://192.168.1.35:3690> 18ab87c6-8455-4174-a313-7b6fd3775a73Password for 'root': Authentication realm: <svn://192.168.1.35:3690> 18ab87c6-8455-4174-a313-7b6fd3775a73Username: svnadminPassword for 'svnadmin': Authentication realm: <svn://192.168.1.35:3690> 18ab87c6-8455-4174-a313-7b6fd3775a73Username: user01Password for 'user01': -----------------------------------------------------------------------ATTENTION! Your password for authentication realm: <svn://192.168.1.35:3690> 18ab87c6-8455-4174-a313-7b6fd3775a73can only be stored to disk unencrypted! You are advised to configureyour system so that Subversion can store passwords encrypted, ifpossible. See the documentation for details.You can avoid future appearances of this warning by setting the valueof the 'store-plaintext-passwords' option to either 'yes' or 'no' in'/root/.subversion/servers'.-----------------------------------------------------------------------Store password unencrypted (yes/no)? yesA client_webtest/default.htmlA client_webtest/default1.htmlA client_webtest/k.txtA client_webtest/index.htmlA client_webtest/index.phpChecked out revision 40.
[root@v03-svn-client ~]# tree client_webtest/client_webtest/├── default1.html├── default.html├── index.html├── index.php└── k.txt0 directories, 5 files
[root@v01-svn-test-server www]# svn co svn://192.168.1.35/online
上面的一條命令是在網站根目錄下check out 個workcopy (online)同時建立一個目錄localsvn,同過rsync同步online(除.svn)的所欲檔案 到localsvn
[root@v01-svn-test-server www]# lsauthz index.php online phpwind rsync_test.sh webtestindex.html localsvn passwd project svnserve.conf
[root@v01-svn-test-server www]# svn co svn://192.168.1.65/webtest localsvn#從線上的svn伺服器上chackout個文化workcopy 並重新命名為localsvn 為以後網線上提價代碼用
編寫svn(測試伺服器上) 鉤子代碼:
[root@v01-svn-test-server hooks]# lspost-commit post-unlock.tmpl pre-unlock.tmplpost-commit.tmpl pre-commit.tmpl start-commit.tmplpost-lock.tmpl pre-lock.tmplpost-revprop-change.tmpl pre-revprop-change.tmpl[root@v01-svn-test-server hooks]# pwd/svn/webtest/hooks
[root@v01-svn-test-server www]# cd /svn/webtest/hooks/[root@v01-svn-test-server hooks]# vi post-commitREPOS="$1"REV="$2"SVN=/usr/bin/svnLOCALSVN=/alidata/www/localsvnWEB=/alidata/www/onlineRSYNC=/usr/bin/rsyncLOG=/alidata/log/svn/svn.logexport LANG=en_US.UTF-8$SVN update $WEB --username user01 --password 123if [ $? == 0 ];thenecho "" >>$LOGecho `date` >>$LOGecho "############################" >>$LOG$RSYNC -vr --exclude=".svn" --delete $WEB/ $LOCALSVN >>$LOGfi#rsync 參數--exclude =".svn" 是除.svn都同步;--delete 刪除目標目錄的在來源目錄中不存在的檔案,保證目標目錄與來源目錄保持一致(這一點很關鍵!!)
[root@v03-svn-client client_webtest]# pwd/root/client_webtest[root@v03-svn-client client_webtest]# echo "客服端提交代碼到svn服務上">> test.txt [root@v03-svn-client client_webtest]# cat test.txt 客服端提交代碼到svn服務上
[root@v03-svn-client client_webtest]# svn status ? test.txt
[root@v03-svn-client client_webtest]# svn add test.txt A (bin) test.txt[root@v03-svn-client client_webtest]# svn ci -m "客服端添加檔案" test.txtAdding (bin) test.txtTransmitting file data .Committed revision 43.
[root@v01-svn-test-server online]# svn status [root@v01-svn-test-server online]# cat test.txt 客服端提交代碼到svn服務上[root@v01-svn-test-server online]# # 代碼成功同步到測試環境
[root@v01-svn-test-server localsvn]# lsdefault1.html default.html index.html index.php test.txt[root@v01-svn-test-server localsvn]# cat test.txt 客服端提交代碼到svn服務上[root@v01-svn-test-server localsvn]# svn status? test.txt#通過rsync -vr --exclude=".svn" --delete /alidata/www/online/ /alidata/www/localsvn 來實現代碼同步
然後根據開發統一上線(可以全部,也可一特定代碼上線!!)
[root@v03-svn-client client_webtest]# echo "更新代碼---》1" >> test.txt [root@v03-svn-client client_webtest]# touch test2.txt #添加新的代碼test.txt[root@v03-svn-client client_webtest]# svn status? test2.txtM test.txt[root@v03-svn-client client_webtest]# svn add test2.txt A test2.txt[root@v03-svn-client client_webtest]# svn ci -m "'更新代碼---》1'>> test.txt 添加新的代碼test.txt"Sending test.txtAdding test2.txtTransmitting file data ..Committed revision 44.
[root@v01-svn-test-server online]# pwd/alidata/www/online[root@v01-svn-test-server online]# lsdefault1.html default.html index.html index.php test2.txt test.txt[root@v01-svn-test-server online]# cat test.txt 客服端提交代碼到svn服務上更新代碼---》1[root@v01-svn-test-server online]# svn status[root@v01-svn-test-server online]# 代碼根新成功!!!
[root@v01-svn-test-server localsvn]# pwd/alidata/www/localsvn[root@v01-svn-test-server localsvn]# lsdefault1.html default.html index.html index.php test2.txt test.txt[root@v01-svn-test-server localsvn]# cat test.txt 客服端提交代碼到svn服務上更新代碼---》1[root@v01-svn-test-server localsvn]# svn status? test2.txt? test.txt#通過rsync同步成功!
驗證:目標於來源目錄檔案是否時時同步,包裹刪除!
[root@v03-svn-client client_webtest]# lsdefault1.html default.html index.html index.php test2.txt test.txt[root@v03-svn-client client_webtest]# svn status[root@v03-svn-client client_webtest]# lsdefault1.html default.html index.html index.php test2.txt test.txt[root@v03-svn-client client_webtest]# svn delete test2.txtD test2.txt[root@v03-svn-client client_webtest]# svn statusD test2.txt[root@v03-svn-client client_webtest]# lsdefault1.html default.html index.html index.php test.txt[root@v03-svn-client client_webtest]# svn ci -m "delete test2.txt" test2.txtDeleting test2.txtCommitted revision 45.
[root@v01-svn-test-server online]# pwd/alidata/www/online[root@v01-svn-test-server online]# lsdefault1.html default.html index.html index.php test.txt[root@v01-svn-test-server online]# svn status
[root@v01-svn-test-server www]# cd localsvn/[root@v01-svn-test-server localsvn]# lsdefault1.html default.html index.html index.php test.txt
[root@v01-svn-test-server localsvn]# cat test.txt 客服端提交代碼到svn服務上更新代碼---》1[root@v01-svn-test-server localsvn]# svn status? test.txt
[root@v03-svn-client client_webtest]# cat test.txt 客服端提交代碼到svn服務上更新代碼---》1[root@v03-svn-client client_webtest]# lsdefault1.html default.html index.html index.php test.txt[root@v03-svn-client client_webtest]# svn status[root@v03-svn-client client_webtest]# echo "更新代碼----》2" >> test.txt [root@v03-svn-client client_webtest]# svn statusM test.txt[root@v03-svn-client client_webtest]# svn ci -m "echo'更新代碼----》2' >> test.txt "Sending test.txtTransmitting file data .Committed revision 46.[root@v03-svn-client client_webtest]# svn status[root@v03-svn-client client_webtest]#
[root@v01-svn-test-server online]# pwd/alidata/www/online[root@v01-svn-test-server online]# lsdefault1.html default.html index.html index.php test.txt[root@v01-svn-test-server online]# svn status[root@v01-svn-test-server online]# cat test.txt 客服端提交代碼到svn服務上更新代碼---》1更新代碼----》2[root@v01-svn-test-server online]#
線上正式環境的svn的鉤子指令碼:
[root@v02-svn-online ~]# cat /svn/webtest/hooks/post-commitREPOS="$1"REV="$2"SVN=/usr/bin/svnWEB=/alidata/www/webtestLOG=/alidata/log/svn/svn.logexport LANG=en_US.UTF-8$SVN update $WEB --username user001 --password 123 >>$LOG#mailer.py commit "$REPOS" "$REV" /path/to/mailer.conf
(切忌防火牆不果沒配的話,可以先關了!)
[root@v01-svn-test-server localsvn]# pwd/alidata/www/localsvn[root@v01-svn-test-server localsvn]# lsdefault1.html default.html index.html index.php test.txt[root@v01-svn-test-server localsvn]# svn status? test.txt[root@v01-svn-test-server localsvn]# cat test.txt 客服端提交代碼到svn服務上更新代碼---》1更新代碼----》2[root@v01-svn-test-server localsvn]# svn add test.txt A test.txt[root@v01-svn-test-server localsvn]# svn ci -m ”定時網線上發布代碼“ test.txt svn: Commit failed (details follow):svn: Can't connect to host '192.168.1.65': No route to host(因為防火請連接埠沒開)[root@v01-svn-test-server localsvn]# svn ci -m ”定時網線上發布代碼“ test.txt Adding test.txtTransmitting file data .Committed revision 30.
[root@v02-svn-online webtest]# pwd/alidata/www/webtest[root@v02-svn-online webtest]# lsdefault1.html default.html index.html index.php test.txt xxxzz.tar xxzz.zip[root@v02-svn-online webtest]# cat test.txt 客服端提交代碼到svn服務上更新代碼---》1更新代碼----》2[root@v02-svn-online webtest]# 上線成功!
[root@v03-svn-client client_webtest]# echo "更新-----》3" > test.txt [root@v03-svn-client client_webtest]# svn statusM test.txt[root@v03-svn-client client_webtest]# cat test.txt 更新-----》3[root@v03-svn-client client_webtest]#
[root@v03-svn-client client_webtest]# svn ci -m "echo "更新-----》3" > test.txt " Sending test.txtTransmitting file data .Committed revision 47.[root@v03-svn-client client_webtest]#
[root@v01-svn-test-server online]# svn status[root@v01-svn-test-server online]# lsdefault1.html default.html index.html index.php test.txt[root@v01-svn-test-server online]# cat test.txt 更新-----》3[root@v01-svn-test-server online]#
[root@v01-svn-test-server localsvn]# pwd/alidata/www/localsvn[root@v01-svn-test-server localsvn]# lsdefault1.html default.html index.html index.php test.txt[root@v01-svn-test-server localsvn]# svn statusM test.txt[root@v01-svn-test-server localsvn]# cat test.txt 更新-----》3[root@v01-svn-test-server localsvn]#
[root@v01-svn-test-server localsvn]# svn ci -m "更新-----》3 test.txt" test.txt Sending test.txtTransmitting file data .Committed revision 31.[root@v01-svn-test-server localsvn]#
[root@v02-svn-online webtest]# cat test.txt 更新-----》3[root@v02-svn-online webtest]#
復原代碼:
[root@v01-svn-test-server localsvn]# svn diff -r 31:30Index: test.txt===================================================================--- test.txt(revision 31)+++ test.txt(revision 30)@@ -1 +1,3 @@-更新-----》3+客服端提交代碼到svn服務上+更新代碼---》1+更新代碼----》2
[root@v01-svn-test-server localsvn]# svn diff -r 31:30 test.txt Index: test.txt===================================================================--- test.txt(revision 31)+++ test.txt(revision 30)@@ -1 +1,3 @@-更新-----》3+客服端提交代碼到svn服務上+更新代碼---》1+更新代碼----》2[root@v01-svn-test-server localsvn]# svn -r 31:30 "" test.txt
[root@v01-svn-test-server localsvn]# svn merge -r31:30 ""svn: Cannot reverse-merge a range from a path's own future history; try updating first[root@v01-svn-test-server localsvn]# svn upAt revision 31.[root@v01-svn-test-server localsvn]# svn merge -r31:30 ""--- Reverse-merging r31 into '.':U test.txt[root@v01-svn-test-server localsvn]# cat test.txt 客服端提交代碼到svn服務上更新代碼---》1更新代碼----》2[root@v01-svn-test-server localsvn]# svn log -v test.txt ------------------------------------------------------------------------r31 | user001 | 2016-05-19 11:57:52 +0800 (Thu, 19 May 2016) | 1 lineChanged paths: M /test.txt更新-----》3 test.txt------------------------------------------------------------------------r30 | user001 | 2016-05-19 11:38:30 +0800 (Thu, 19 May 2016) | 1 lineChanged paths: A /test.txt”定時網線上發布代碼“------------------------------------------------------------------------[root@v01-svn-test-server localsvn]#
[root@v01-svn-test-server localsvn]# svn ci -m "merge -r31:30" test.txt Sending test.txtTransmitting file data .Committed revision 32.
[root@v02-svn-online webtest]# cat test.txt 客服端提交代碼到svn服務上更新代碼---》1更新代碼----》2[root@v02-svn-online webtest]# svn log test.txt ------------------------------------------------------------------------r32 | user001 | 2016-05-19 13:27:47 +0800 (Thu, 19 May 2016) | 1 linemerge -r31:30------------------------------------------------------------------------r31 | user001 | 2016-05-19 11:57:52 +0800 (Thu, 19 May 2016) | 1 line更新-----》3 test.txt------------------------------------------------------------------------r30 | user001 | 2016-05-19 11:38:30 +0800 (Thu, 19 May 2016) | 1 line”定時網線上發布代碼“------------------------------------------------------------------------[root@v02-svn-online webtest]# 復原成功!