SVN – 搭建 SVN 服務器 && 多項目分別建立版本庫 && 同步至生產環境


經驗總結    svn 服務器一定要重啓,   配置 一定要看對   尤其是real  指定 。


閱讀數:2005

1、安裝Subversion

  1. [root@VM_centos /]# yum install subversion  

2、找一下安裝位置

  1. [root@VM_centos /]# rpm -ql subversion  
  2. /etc/bash_completion.d  
  3. /etc/bash_completion.d/subversion  
  4. /etc/rc.d/init.d/svnserve  
  5. /etc/subversion  
  6. /usr/bin/svn  
  7. /usr/bin/svnadmin  
  8. /usr/bin/svndumpfilter  
  9. /usr/bin/svnlook  
  10. /usr/bin/svnserve  
  11. /usr/bin/svnsync  
  12. /usr/bin/svnversion  
  13. ...  
  14. ...  
  15. ...  

然後可以稍微看一下幫助

  1. [root@VM_centos /]# svn --help  
  2. usage: svn <subcommand> [options] [args]  
  3. Subversion command-line client, version 1.6.11.  
  4. Type 'svn help <subcommand>' for help on a specific subcommand.  
  5. Type 'svn --version' to see the program version and RA modules  
  6.   or 'svn --version --quiet' to see just the version number.  
  7.    
  8. Most subcommands take file and/or directory arguments, recursing  
  9. on the directories.  If no arguments are supplied to such a  
  10. command, it recurses on the current directory (inclusive) by default.  
  11.    
  12. Available subcommands:  
  13.    add  
  14.    blame (praise, annotate, ann)  
  15.    cat  
  16.    changelist (cl)  
  17.    checkout (co)  
  18.    cleanup  
  19.    commit (ci)  
  20.    copy (cp)  
  21.    delete (del, remove, rm)  
  22.    diff (di)  
  23.    export  
  24.    help (?, h)  

3、創建SVN版本庫目錄

  1. [root@VM_centos /]# mkdir -p /home/svn/test  

checkout時,提示:URL svn://192.168.1.99/svntest doesn't exist...

奇怪,怎麼會提示庫不存在呢?肯定是哪裏配置問題。後來嘗試了半天,也在網上搜索了很久,終於發現問題所在。

如果你的svn庫的路徑爲:/home/svn/svntest

那麼你啓動時,不能用命令:

svnserve -d -r /home/svn/svntest

而要用命令:

svnserve -d -r /home/svn/

4、創建版本庫

  1. [root@VM_centos test]# svnadmin create /home/svn/test  

那麼在/home/svn/test下面出現了這幾個東西

  1. [root@VM_centos /]# ll  
  2. total 24  
  3. -rw-r--r-- 1 root root  229 Oct 27 18:21 README.txt  
  4. drwxr-xr-x 2 root root 4096 Oct 27 18:21 conf  
  5. drwxr-sr-x 6 root root 4096 Oct 27 18:21 db  
  6. -r--r--r-- 1 root root    2 Oct 27 18:21 format  
  7. drwxr-xr-x 2 root root 4096 Oct 27 18:21 hooks  
  8. drwxr-xr-x 2 root root 4096 Oct 27 18:21 locks  

5、接下來進行一些配置

首先讓我們看一下conf目錄下都是些什麼貨

  1. [root@VM_centos test]# ll conf/  
  2. total 12  
  3. -rw-r--r-- 1 root root 1080 Oct 27 18:21 authz // 權限控制  
  4. -rw-r--r-- 1 root root  309 Oct 27 18:21 passwd // 賬號密碼  
  5. -rw-r--r-- 1 root root 2279 Oct 27 18:21 svnserve.conf // SVN服務配置  

然後設置下賬號密碼

vim

  1. [root@VM_centos conf]# vim passwd  
  2. ### This file is an example password file for svnserve.  
  3. ### Its format is similar to that of svnserve.conf. As shown in the  
  4. ### example below it contains one section labelled [users].  
  5. ### The name and password for each user follow, one account per line.  
  1. <p>[users]</p><p># harry = harryssecret</p><p># sally = sallyssecret</p>  

“[users]”中添加用戶名密碼,格式:用戶名=密碼,如下:

Vim

  1. ### This file is an example password file for svnserve.  
  2. ### Its format is similar to that of svnserve.conf. As shown in the  
  3. ### example below it contains one section labelled [users].  
  4. ### The name and password for each user follow, one account per line.  
  5.    
  6. [users]  
  7. # harry = harryssecret  
  8. # sally = sallyssecret  
  9. test    = 709fyfHWPb5A  
  10.    

設置權限

Vim

  1. [root@VM_centos conf]# vim authz  
  2. ### This file is an example authorization file for svnserve.  
  3. ### Its format is identical to that of mod_authz_svn authorization  
  4. ### files.  
  5. ### As shown below each section defines authorizations for the path and  
  6. ### (optional) repository specified by the section name.  
  7. ### The authorizations follow. An authorization line can refer to:  
  8. ###  - a single user,  
  9. ###  - a group of users defined in a special [groups] section,  
  10. ###  - an alias defined in a special [aliases] section,  
  11. ###  - all authenticated users, using the '$authenticated' token,  
  12. ###  - only anonymous users, using the '$anonymous' token,  
  13. ###  - anyone, using the '*' wildcard.  
  14. ###  
  15. ### A match can be inverted by prefixing the rule with '~'. Rules can  
  16. ### grant read ('r') access, read-write ('rw') access, or no access  
  17. ### ('').  
  18.    
  19. [aliases]  
  20. # joe = /C=XZ/ST=Dessert/L=Snake City/O=Snake Oil, Ltd./OU=Research Institute/CN=Joe Average  
  21.    
  22. [groups]  
  23. # harry_and_sally = harry,sally  
  24. # harry_sally_and_joe = harry,sally,&joe  
  25.    
  26. # [/foo/bar]  
  27. # harry = rw  
  28. # &joe = r  
  29. # * =  
  30.    
  31. # [repository:/baz/fuz]  
  32. # @harry_and_sally = rw  
  33. # * = r  

在文件末尾增加下面的東西:

vim

  1. [/]  
  2. test=rw  
  3. otheruser=r  
  4. # ...  
  5. # 意思就是  
  6. # [/] 在版本庫的根目錄下  

修改svnserve.conf文件

Vim

  1. [root@VM_centos conf]# vim svnserve.conf  
  2. ### This file controls the configuration of the svnserve daemon, if you  
  3. ### use it to allow access to this repository.  (If you only allow  
  4. ### access through http: and/or file: URLs, then this file is  
  5. ### irrelevant.)  
  6.    
  7. ### Visit http://subversion.tigris.org/ for more information.  
  8.    
  9. [general]  
  10. ### These options control access to the repository for unauthenticated  
  11. ### and authenticated users.  Valid values are "write", "read",  
  12. ### and "none".  The sample settings below are the defaults.  
  13. # anon-access = read  
  14. # auth-access = write  
  15. ### The password-db option controls the location of the password  
  16. ### database file.  Unless you specify a path starting with a /,  
  17. ### the file's location is relative to the directory containing  
  18. ### this configuration file.  
  19. ### If SASL is enabled (see below), this file will NOT be used.  
  20. ### Uncomment the line below to use the default password file.  
  21. # password-db = passwd  
  22. ### The authz-db option controls the location of the authorization  
  23. ### rules for path-based access control.  Unless you specify a path  
  24. ### starting with a /, the file's location is relative to the the  
  25. ### directory containing this file.  If you don't specify an  
  26. ### authz-db, no path-based access control is done.  
  27. ### Uncomment the line below to use the default authorization file.  
  28. # authz-db = authz  
  29. ### This option specifies the authentication realm of the repository.  
  30. ### If two repositories have the same authentication realm, they should  
  31. ### have the same password database, and vice versa.  The default realm  
  32. ### is repository's uuid.  
  33. # realm = My First Repository  
  34.    
  35. [sasl]  
  36. ### This option specifies whether you want to use the Cyrus SASL  
  37. ### library for authentication. Default is false.  
  38. ### This section will be ignored if svnserve is not built with Cyrus  
  39. ### SASL support; to check, run 'svnserve --version' and look for a line  
  40. ### reading 'Cyrus SASL authentication is available.'  
  41. # use-sasl = true  
  42. ### These options specify the desired strength of the security layer  
  43. ### that you want SASL to provide. 0 means no encryption, 1 means  
  44. ### integrity-checking only, values larger than 1 are correlated  
  45. ### to the effective key length for encryption (e.g. 128 means 128-bit  
  46. ### encryption). The values below are the defaults.  
  47. # min-encryption = 0  
  48. # max-encryption = 256  

需要打開其中的幾行註釋並做好配置,如下

Vim

anon-access = read # 匿名用戶可讀

auth-access = write # 授權用戶可寫

password-db = passwd # 指定賬號文件

authz-db = authz # 指定授權文件

realm = /home/svn/test # 指定版本庫所在目錄

6、啓動SVN版本庫

  1. [root@VM_centos test]# svnserve -d -r /home/svn/test  

關閉所有svn    killall svnserve

7、測試

比如,我就在我本地測試

SVN中的文件checkout到本地

1

 

 

輸入賬號密碼
2

3

3

然後在本地的SVN文件夾裏新建個文件然後commit

5

6

那麼,這就算完成了。

END。


 

還沒完!

在按照上面做完之後,在服務器上checkout的時候發現了一個小問題

比如在服務器的某個目錄下:

  1. [root@VM_centos wwwsvn]# svn checkout svn://111.111.111.111 --username test --password xxx  

之後就會在“wwwsvn”目錄下建立名爲“111.111.111.111”的目錄,目錄中是按照上面設置的版本庫的文件。而且只能有這一個版本庫,沒有其他的。

這顯然不是我想要的結果,於是又稍微琢磨了一下,目的是爲多個項目分別建立版本庫。(這裏參考了:這個這個這個,呃還有,這個。其實都大同小異,或許有兩個文章的內容都差不多,不過不要在意這些細節。)

那麼首先要重複執行建立版本庫:

  1. [root@VM_centos /]# svnadmin create /home/svn/project_1  
  2. [root@VM_centos /]# svnadmin create /home/svn/project_2  
  3. [root@VM_centos /]# svnadmin create /home/svn/project_3  

這之後在/home/svn/下就會有project_1project_2project_3這三個目錄

然後隨便進一個、比如進project_1,修改conf裏的三個配置文件

Vim

  1. ###svnserve.com###  
  2. anon-access = read  
  3. auth-access = write  
  4. password-db = passwd  
  5. authz-db = authz  
  6. Vim  
  7. ###authz###  
  8. [groups]  
  9. bigmaster = test1,test2  
  10.    
  11. [/]  
  12. @bigmaster = rw  
  13. test3 = r  
  14. * =   
  15.    
  16. ###說明###  
  17. [groups]  
  18. # 定義羣組 bigmaster 包含兩個用戶 test1、test2  
  19. bigmaster = test1,test2  
  20.    
  21. [/]  
  22. # 定義 bigmaster 羣組的所有用戶對版本庫根目錄及根目錄下所有目錄有讀寫權限  
  23. @bigmaster = rw  
  24. # 定義 test3 用戶對版本庫根目錄及根目錄下所有目錄只有讀權限  
  25. test3 = r  
  26. # 定義"以上沒有定義"的用戶沒有任何權限  

Vim

  1. ###passwd###  
  2. [users]  
  3. test1 = 5BYuu11CbNy7  
  4. test2 = 88AhI8q2QaGe  
  5. test3 = 9h1s3hQpLB4I  

然後再去修改另外兩個項目的配置文件(如不修改,則可以checkout,但是無法commit,應該是隻讀不可寫。具體哪個配置造成的這個局面,暫沒有詳細琢磨。)

然後啓動版本庫

  1. [root@VM_centos test]# svnserve -d -r /home/svn  

這樣之後,/home/svn/目錄下的三個版本庫就都可以用了。

END

 

 

還!沒!完!

在第一次“END之前就在琢磨,比如我是一個web系統,用戶在本地commit之後,能否直接更新到web目錄中從而不用什麼操作直接訪問服務器就能看到最新的改動。

那麼就涉及到了一些觸發同步的東西。

如上面所述,在每一個svn版本庫中都有這麼幾個東西:

  1. [root@VM_centos project_1]# ll  
  2. total 24  
  3. -rw-r--r-- 1 root root  229 Oct 30 14:35 README.txt  
  4. drwxr-xr-x 2 root root 4096 Oct 30 14:42 conf  
  5. drwxr-sr-x 6 root root 4096 Oct 30 15:16 db  
  6. -r--r--r-- 1 root root    2 Oct 30 14:35 format  
  7. drwxr-xr-x 2 root root 4096 Oct 30 14:35 hooks  
  8. drwxr-xr-x 2 root root 4096 Oct 30 14:35 locks  

其中在hooks/目錄下,有一些奇怪的東西:

  1. [root@VM_centos hooks]# ll  
  2. total 36  
  3. -rw-r--r-- 1 root root 1977 Oct 30 14:35 post-commit.tmpl  
  4. -rw-r--r-- 1 root root 1638 Oct 30 14:35 post-lock.tmpl  
  5. -rw-r--r-- 1 root root 2289 Oct 30 14:35 post-revprop-change.tmpl  
  6. -rw-r--r-- 1 root root 1567 Oct 30 14:35 post-unlock.tmpl  
  7. -rw-r--r-- 1 root root 3426 Oct 30 14:35 pre-commit.tmpl  
  8. -rw-r--r-- 1 root root 2410 Oct 30 14:35 pre-lock.tmpl  
  9. -rw-r--r-- 1 root root 2786 Oct 30 14:35 pre-revprop-change.tmpl  
  10. -rw-r--r-- 1 root root 2100 Oct 30 14:35 pre-unlock.tmpl  
  11. -rw-r--r-- 1 root root 2780 Oct 30 14:35 start-commit.tmpl  

從這幾個貨的擴展名來看應該是一些模板示例,那麼從網上翻來得知,這裏就是用來被觸發然後做一些事情的東西。

符合我的需求的,應該是“post-commit”這個,也就是當用戶commit到版本庫之後便觸發這個腳本執行腳本內容,比如把版本庫文件更新到指定的目錄下。

(那麼,照舊,本文仍是網上各種文章的總結性發言,抄襲的來源有:這裏這裏這裏,呃還有這裏。)

那麼,假定,網站的域名是:http://abc.com(後面的配置好像和域名並沒有什麼關係),網站存放在:/home/www/abc/,需要在本地commit之後、服務器的網站代碼直接也隨之更改。

自己建立post-commit,或者複製post-commit.tmplpost-commit,然後把原內容註釋掉,先修改下權限:

  1. [root@VM_centos hooks]# chown www:www post-commit  
  2. [root@VM_centos hooks]# chmod a+x post-commit  

然後修改內容:

Vim

  1. #!/bin/sh  
  2. export LANG=zh_CN.UTF-8    
  3. SVN_PATH=/usr/bin/svn    #不用修改  
  4. WEB_PATH=/home/www/abc   #指向要同步到的目錄  
  5. $SVN_PATH update svn://111.111.111.111/project_1 $WEB_PATH --username test1 --password 5BYuu11CbNy7 --no-auth-cache  

保存退出,然後很重要的一步,在同步的目標目錄(也就是/home/www/abc下,首先進行checkout,否則在post-commit的時候會提示跳過[skip]了這個目錄),然後在本地commit之後,文件就會自動更新至/home/www/abc中了。

如果需要對其他版本庫進行類似的自動更新,則需要修改對應版本庫中hookspost-commit

但是還有個問題,我這裏的web server是用www用戶執行的,而自動同步到/home/www/abc中的文件,用戶組都是root的,即使手動改爲www,然後在本地commit、同步之後,文件還是變爲root。此問題待琢磨。(2014-10-30

凡事都有湊合的方法的,鑑於目前沒找到高bigger的解決方案,故在post-commit裏直接加了句改用戶組的話,如下--2014-10-31

Vim

  1. #!/bin/sh  
  2. export LANG=zh_CN.UTF-8  
  3. SVN_PATH=/usr/bin/svn  
  4. WEB_PATH=/home/www/abc  
  5. $SVN_PATH update svn://111.111.111.111/project_1 $WEB_PATH --username test1 --password 5BYuu11CbNy7 --no-auth-cache  
  6. chown www:www $WEB_PATH/*  

END

補充:

如果一個目錄之前已經在版本庫中,但是現在要加入到另一個版本庫,那麼首先要把這個目錄下的SVN信息刪掉,其實就是刪掉目錄下所有名爲“.svn”的文件:

find . -type d -name ".svn"|xargs rm -rf

再補充:下面這圖是我自己看的,外人用不到的。


如果其他pc不能checkout,可能是防火牆沒有開啓svn的3690端口號

vim /etc/sysconfig/iptables

不能在規則最下面添加,建議22端口下面一行

iptables -I INPUT -p tcp --dport 3306 -j ACCEPT

重啓防火牆讓配置生效 service iptables restart

配置SVN流程


轉載自:http://tech.mclarian.com/a/973

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章