你的位置:首页 > 操作系统

[操作系统]svn 结合rsync 的代码发布系统

由开发提交到测试环境,经测试,在由运维统一上线。试验需求一台测试服务器,一台线上(生产环境)服务器。测试服务器上跑svn是开发用于代码管理,而线上跑的svn是运维用来代码上线的。结合rsync保持测试端的代码与 svn的线上控制端(线上svn,在测试服务器上的一个workcopy)的代码保持一致。开发结合运维,并由运维周期性的提交代码,如果有问题,回滚,保证线上正常!!

svn服务器上chackout 一个workcopy 在用户端:(注意防火墙)

[[email protected] ~]# 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.

 

[[email protected] ~]# tree client_webtest/client_webtest/├── default1.html├── default.html├── index.html├── index.php└── k.txt0 directories, 5 files

[[email protected] www]# svn co svn://192.168.1.35/online

 上面的一条命令是在网站根目录下check out 个workcopy (online)同时新建一个目录localsvn,同过rsync同步online(除.svn)的所欲文件 到localsvn

[[email protected] www]# lsauthz    index.php online phpwind rsync_test.sh webtestindex.html localsvn  passwd project svnserve.conf

 

[[email protected] www]# svn co svn://192.168.1.65/webtest localsvn#从线上的svn服务器上chackout个文化workcopy 并重命名为localsvn 为以后网线上提价代码用

 

 

编写svn(测试服务器上) 钩子代码:

[[email protected] 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[[email protected] hooks]# pwd/svn/webtest/hooks

 

[[email protected] www]# cd /svn/webtest/hooks/[[email protected] 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 删除目标目录的在源目录中不存在的文件,保证目标目录与源目录保持一致(这一点很关键!!)

 

[[email protected] client_webtest]# pwd/root/client_webtest[[email protected] client_webtest]# echo "客服端提交代码到svn服务上">> test.txt [[email protected] client_webtest]# cat test.txt 客服端提交代码到svn服务上

 

[[email protected] client_webtest]# svn status ?    test.txt

 

[[email protected] client_webtest]# svn add test.txt A (bin) test.txt[[email protected] client_webtest]# svn ci -m "客服端添加文件" test.txtAdding (bin) test.txtTransmitting file data .Committed revision 43.

 

[[email protected] online]# svn status [[email protected] online]# cat test.txt 客服端提交代码到svn服务上[[email protected] online]# # 代码成功同步到测试环境

 

[[email protected] localsvn]# lsdefault1.html default.html index.html index.php test.txt[[email protected] localsvn]# cat test.txt 客服端提交代码到svn服务上[[email protected] localsvn]# svn status?    test.txt#通过rsync -vr --exclude=".svn" --delete /alidata/www/online/ /alidata/www/localsvn 来实现代码同步

 然后根据开发统一上线(可以全部,也可一特定代码上线!!)

[[email protected] client_webtest]# echo "更新代码---》1" >> test.txt [[email protected] client_webtest]# touch test2.txt #添加新的代码test.txt[[email protected] client_webtest]# svn status?    test2.txtM    test.txt[[email protected] client_webtest]# svn add test2.txt A     test2.txt[[email protected] client_webtest]# svn ci -m "'更新代码---》1'>> test.txt 添加新的代码test.txt"Sending    test.txtAdding     test2.txtTransmitting file data ..Committed revision 44.

 

[[email protected] online]# pwd/alidata/www/online[[email protected] online]# lsdefault1.html default.html index.html index.php test2.txt test.txt[[email protected] online]# cat test.txt 客服端提交代码到svn服务上更新代码---》1[[email protected] online]# svn status[[email protected] online]# 代码根新成功!!!

 

[[email protected] localsvn]# pwd/alidata/www/localsvn[[email protected] localsvn]# lsdefault1.html default.html index.html index.php test2.txt test.txt[[email protected] localsvn]# cat test.txt 客服端提交代码到svn服务上更新代码---》1[[email protected] localsvn]# svn status?    test2.txt?    test.txt#通过rsync同步成功!

 验证:目标于源目录文件是否时时同步,包裹删除!

[[email protected] client_webtest]# lsdefault1.html default.html index.html index.php test2.txt test.txt[[email protected] client_webtest]# svn status[[email protected] client_webtest]# lsdefault1.html default.html index.html index.php test2.txt test.txt[[email protected] client_webtest]# svn delete test2.txtD     test2.txt[[email protected] client_webtest]# svn statusD    test2.txt[[email protected] client_webtest]# lsdefault1.html default.html index.html index.php test.txt[[email protected] client_webtest]# svn ci -m "delete test2.txt" test2.txtDeleting    test2.txtCommitted revision 45.

 

[[email protected] online]# pwd/alidata/www/online[[email protected] online]# lsdefault1.html default.html index.html index.php test.txt[[email protected] online]# svn status

 

[[email protected] www]# cd localsvn/[[email protected] localsvn]# lsdefault1.html default.html index.html index.php test.txt

 

[[email protected] localsvn]# cat test.txt 客服端提交代码到svn服务上更新代码---》1[[email protected] localsvn]# svn status?    test.txt

 

[[email protected] client_webtest]# cat test.txt 客服端提交代码到svn服务上更新代码---》1[[email protected] client_webtest]# lsdefault1.html default.html index.html index.php test.txt[[email protected] client_webtest]# svn status[[email protected] client_webtest]# echo "更新代码----》2" >> test.txt [[email protected] client_webtest]# svn statusM    test.txt[[email protected] client_webtest]# svn ci -m "echo'更新代码----》2' >> test.txt "Sending    test.txtTransmitting file data .Committed revision 46.[[email protected] client_webtest]# svn status[[email protected] client_webtest]# 

 

[[email protected] online]# pwd/alidata/www/online[[email protected] online]# lsdefault1.html default.html index.html index.php test.txt[[email protected] online]# svn status[[email protected] online]# cat test.txt 客服端提交代码到svn服务上更新代码---》1更新代码----》2[[email protected] online]# 

 线上正式环境的svn的钩子脚本:

[[email protected] ~]# 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

 (切忌防火墙不果没配的话,可以先关了!)

[[email protected] localsvn]# pwd/alidata/www/localsvn[[email protected] localsvn]# lsdefault1.html default.html index.html index.php test.txt[[email protected] localsvn]# svn status?    test.txt[[email protected] localsvn]# cat test.txt 客服端提交代码到svn服务上更新代码---》1更新代码----》2[[email protected] localsvn]# svn add test.txt A     test.txt[[email protected] 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(因为防火请端口没开)[[email protected] localsvn]# svn ci -m ”定时网线上发布代码“ test.txt Adding     test.txtTransmitting file data .Committed revision 30.

 

[[email protected] webtest]# pwd/alidata/www/webtest[[email protected] webtest]# lsdefault1.html default.html index.html index.php test.txt xxxzz.tar xxzz.zip[[email protected] webtest]# cat test.txt 客服端提交代码到svn服务上更新代码---》1更新代码----》2[[email protected] webtest]# 上线成功!

 

[[email protected] client_webtest]# echo "更新-----》3" > test.txt [[email protected] client_webtest]# svn statusM    test.txt[[email protected] client_webtest]# cat test.txt 更新-----》3[[email protected] client_webtest]#

 

[[email protected] client_webtest]# svn ci -m "echo "更新-----》3" > test.txt " Sending    test.txtTransmitting file data .Committed revision 47.[[email protected] client_webtest]# 

 

[[email protected] online]# svn status[[email protected] online]# lsdefault1.html default.html index.html index.php test.txt[[email protected] online]# cat test.txt 更新-----》3[[email protected] online]# 

 

[[email protected] localsvn]# pwd/alidata/www/localsvn[[email protected] localsvn]# lsdefault1.html default.html index.html index.php test.txt[[email protected] localsvn]# svn statusM    test.txt[[email protected] localsvn]# cat test.txt 更新-----》3[[email protected] localsvn]# 

 

[[email protected] localsvn]# svn ci -m "更新-----》3 test.txt" test.txt Sending    test.txtTransmitting file data .Committed revision 31.[[email protected] localsvn]# 

 

[[email protected] webtest]# cat test.txt 更新-----》3[[email protected] webtest]# 

 回滚代码:

[[email protected] localsvn]# svn diff -r 31:30Index: test.txt===================================================================--- test.txt	(revision 31)+++ test.txt	(revision 30)@@ -1 +1,3 @@-更新-----》3+客服端提交代码到svn服务上+更新代码---》1+更新代码----》2

 

[[email protected] 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[[email protected] localsvn]# svn -r 31:30 "" test.txt

 

[[email protected] localsvn]# svn merge -r31:30 ""svn: Cannot reverse-merge a range from a path's own future history; try updating first[[email protected] localsvn]# svn upAt revision 31.[[email protected] localsvn]# svn merge -r31:30 ""--- Reverse-merging r31 into '.':U  test.txt[[email protected] 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]# 回滚成功!