1、安裝Subversion
- [root@VM_centos /]# yum install subversion
2、找一下安裝位置
- [root@VM_centos /]# rpm -ql subversion
- /etc/bash_completion.d
- /etc/bash_completion.d/subversion
- /etc/rc.d/init.d/svnserve
- /etc/subversion
- /usr/bin/svn
- /usr/bin/svnadmin
- /usr/bin/svndumpfilter
- /usr/bin/svnlook
- /usr/bin/svnserve
- /usr/bin/svnsync
- /usr/bin/svnversion
- ...
- ...
- ...
然後可以稍微看一下幫助
- [root@VM_centos /]# svn --help
- usage: svn <subcommand> [options] [args]
- Subversion command-line client, version 1.6.11.
- Type 'svn help <subcommand>' for help on a specific subcommand.
- Type 'svn --version' to see the program version and RA modules
- or 'svn --version --quiet' to see just the version number.
- Most subcommands take file and/or directory arguments, recursing
- on the directories. If no arguments are supplied to such a
- command, it recurses on the current directory (inclusive) by default.
- Available subcommands:
- add
- blame (praise, annotate, ann)
- cat
- changelist (cl)
- checkout (co)
- cleanup
- commit (ci)
- copy (cp)
- delete (del, remove, rm)
- diff (di)
- export
- help (?, h)
3、創建SVN版本庫目錄
- [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、創建版本庫
- [root@VM_centos test]# svnadmin create /home/svn/test
那麼在/home/svn/test下面出現了這幾個東西
- [root@VM_centos /]# ll
- total 24
- -rw-r--r-- 1 root root 229 Oct 27 18:21 README.txt
- drwxr-xr-x 2 root root 4096 Oct 27 18:21 conf
- drwxr-sr-x 6 root root 4096 Oct 27 18:21 db
- -r--r--r-- 1 root root 2 Oct 27 18:21 format
- drwxr-xr-x 2 root root 4096 Oct 27 18:21 hooks
- drwxr-xr-x 2 root root 4096 Oct 27 18:21 locks
5、接下來進行一些配置
首先讓我們看一下conf目錄下都是些什麼貨
- [root@VM_centos test]# ll conf/
- total 12
- -rw-r--r-- 1 root root 1080 Oct 27 18:21 authz // 權限控制
- -rw-r--r-- 1 root root 309 Oct 27 18:21 passwd // 賬號密碼
- -rw-r--r-- 1 root root 2279 Oct 27 18:21 svnserve.conf // SVN服務配置
然後設置下賬號密碼
vim
- [root@VM_centos conf]# vim passwd
- ### This file is an example password file for svnserve.
- ### Its format is similar to that of svnserve.conf. As shown in the
- ### example below it contains one section labelled [users].
- ### The name and password for each user follow, one account per line.
- <p>[users]</p><p># harry = harryssecret</p><p># sally = sallyssecret</p>
在“[users]”中添加用戶名密碼,格式:用戶名=密碼,如下:
Vim
- ### This file is an example password file for svnserve.
- ### Its format is similar to that of svnserve.conf. As shown in the
- ### example below it contains one section labelled [users].
- ### The name and password for each user follow, one account per line.
- [users]
- # harry = harryssecret
- # sally = sallyssecret
- test = 709fyfHWPb5A
設置權限
Vim
- [root@VM_centos conf]# vim authz
- ### This file is an example authorization file for svnserve.
- ### Its format is identical to that of mod_authz_svn authorization
- ### files.
- ### As shown below each section defines authorizations for the path and
- ### (optional) repository specified by the section name.
- ### The authorizations follow. An authorization line can refer to:
- ### - a single user,
- ### - a group of users defined in a special [groups] section,
- ### - an alias defined in a special [aliases] section,
- ### - all authenticated users, using the '$authenticated' token,
- ### - only anonymous users, using the '$anonymous' token,
- ### - anyone, using the '*' wildcard.
- ###
- ### A match can be inverted by prefixing the rule with '~'. Rules can
- ### grant read ('r') access, read-write ('rw') access, or no access
- ### ('').
- [aliases]
- # joe = /C=XZ/ST=Dessert/L=Snake City/O=Snake Oil, Ltd./OU=Research Institute/CN=Joe Average
- [groups]
- # harry_and_sally = harry,sally
- # harry_sally_and_joe = harry,sally,&joe
- # [/foo/bar]
- # harry = rw
- # &joe = r
- # * =
- # [repository:/baz/fuz]
- # @harry_and_sally = rw
- # * = r
在文件末尾增加下面的東西:
vim
- [/]
- test=rw
- otheruser=r
- # ...
- # 意思就是
- # [/] 在版本庫的根目錄下
修改svnserve.conf文件
Vim
- [root@VM_centos conf]# vim svnserve.conf
- ### This file controls the configuration of the svnserve daemon, if you
- ### use it to allow access to this repository. (If you only allow
- ### access through http: and/or file: URLs, then this file is
- ### irrelevant.)
- ### Visit http://subversion.tigris.org/ for more information.
- [general]
- ### These options control access to the repository for unauthenticated
- ### and authenticated users. Valid values are "write", "read",
- ### and "none". The sample settings below are the defaults.
- # anon-access = read
- # auth-access = write
- ### The password-db option controls the location of the password
- ### database file. Unless you specify a path starting with a /,
- ### the file's location is relative to the directory containing
- ### this configuration file.
- ### If SASL is enabled (see below), this file will NOT be used.
- ### Uncomment the line below to use the default password file.
- # password-db = passwd
- ### The authz-db option controls the location of the authorization
- ### rules for path-based access control. Unless you specify a path
- ### starting with a /, the file's location is relative to the the
- ### directory containing this file. If you don't specify an
- ### authz-db, no path-based access control is done.
- ### Uncomment the line below to use the default authorization file.
- # authz-db = authz
- ### This option specifies the authentication realm of the repository.
- ### If two repositories have the same authentication realm, they should
- ### have the same password database, and vice versa. The default realm
- ### is repository's uuid.
- # realm = My First Repository
- [sasl]
- ### This option specifies whether you want to use the Cyrus SASL
- ### library for authentication. Default is false.
- ### This section will be ignored if svnserve is not built with Cyrus
- ### SASL support; to check, run 'svnserve --version' and look for a line
- ### reading 'Cyrus SASL authentication is available.'
- # use-sasl = true
- ### These options specify the desired strength of the security layer
- ### that you want SASL to provide. 0 means no encryption, 1 means
- ### integrity-checking only, values larger than 1 are correlated
- ### to the effective key length for encryption (e.g. 128 means 128-bit
- ### encryption). The values below are the defaults.
- # min-encryption = 0
- # max-encryption = 256
需要打開其中的幾行註釋並做好配置,如下
Vim
anon-access = read # 匿名用戶可讀
auth-access = write # 授權用戶可寫
password-db = passwd # 指定賬號文件
authz-db = authz # 指定授權文件
realm = /home/svn/test # 指定版本庫所在目錄
6、啓動SVN版本庫
- [root@VM_centos test]# svnserve -d -r /home/svn/test
關閉所有svn killall svnserve
7、測試
比如,我就在我本地測試
把SVN中的文件checkout到本地
然後在本地的SVN文件夾裏新建個文件然後commit
那麼,這就算完成了。
END。
還沒完!
在按照上面做完之後,在服務器上checkout的時候發現了一個小問題
比如在服務器的某個目錄下:
- [root@VM_centos wwwsvn]# svn checkout svn://111.111.111.111 --username test --password xxx
之後就會在“wwwsvn”目錄下建立名爲“111.111.111.111”的目錄,目錄中是按照上面設置的版本庫的文件。而且只能有這一個版本庫,沒有其他的。
這顯然不是我想要的結果,於是又稍微琢磨了一下,目的是爲多個項目分別建立版本庫。(這裏參考了:這個、這個、這個,呃…還有,這個。其實都大同小異,或許有兩個文章的內容都差不多,不過不要在意這些細節。)
那麼首先要重複執行建立版本庫:
- [root@VM_centos /]# svnadmin create /home/svn/project_1
- [root@VM_centos /]# svnadmin create /home/svn/project_2
- [root@VM_centos /]# svnadmin create /home/svn/project_3
這之後在/home/svn/下就會有project_1、project_2、project_3這三個目錄
然後隨便進一個、比如進project_1,修改conf裏的三個配置文件
Vim
- ###svnserve.com###
- anon-access = read
- auth-access = write
- password-db = passwd
- authz-db = authz
- Vim
- ###authz###
- [groups]
- bigmaster = test1,test2
- [/]
- @bigmaster = rw
- test3 = r
- * =
- ###說明###
- [groups]
- # 定義羣組 bigmaster 包含兩個用戶 test1、test2
- bigmaster = test1,test2
- [/]
- # 定義 bigmaster 羣組的所有用戶對版本庫根目錄及根目錄下所有目錄有讀寫權限
- @bigmaster = rw
- # 定義 test3 用戶對版本庫根目錄及根目錄下所有目錄只有讀權限
- test3 = r
- # 定義"以上沒有定義"的用戶沒有任何權限
Vim
- ###passwd###
- [users]
- test1 = 5BYuu11CbNy7
- test2 = 88AhI8q2QaGe
- test3 = 9h1s3hQpLB4I
然後再去修改另外兩個項目的配置文件(如不修改,則可以checkout,但是無法commit,應該是隻讀不可寫。具體哪個配置造成的這個局面,暫沒有詳細琢磨。)
然後啓動版本庫
- [root@VM_centos test]# svnserve -d -r /home/svn
這樣之後,/home/svn/目錄下的三個版本庫就都可以用了。
END。
還!沒!完!
在第一次“END。”之前就在琢磨,比如我是一個web系統,用戶在本地commit之後,能否直接更新到web目錄中從而不用什麼操作直接訪問服務器就能看到最新的改動。
那麼就涉及到了一些觸發同步的東西。
如上面所述,在每一個svn版本庫中都有這麼幾個東西:
- [root@VM_centos project_1]# ll
- total 24
- -rw-r--r-- 1 root root 229 Oct 30 14:35 README.txt
- drwxr-xr-x 2 root root 4096 Oct 30 14:42 conf
- drwxr-sr-x 6 root root 4096 Oct 30 15:16 db
- -r--r--r-- 1 root root 2 Oct 30 14:35 format
- drwxr-xr-x 2 root root 4096 Oct 30 14:35 hooks
- drwxr-xr-x 2 root root 4096 Oct 30 14:35 locks
其中在hooks/目錄下,有一些奇怪的東西:
- [root@VM_centos hooks]# ll
- total 36
- -rw-r--r-- 1 root root 1977 Oct 30 14:35 post-commit.tmpl
- -rw-r--r-- 1 root root 1638 Oct 30 14:35 post-lock.tmpl
- -rw-r--r-- 1 root root 2289 Oct 30 14:35 post-revprop-change.tmpl
- -rw-r--r-- 1 root root 1567 Oct 30 14:35 post-unlock.tmpl
- -rw-r--r-- 1 root root 3426 Oct 30 14:35 pre-commit.tmpl
- -rw-r--r-- 1 root root 2410 Oct 30 14:35 pre-lock.tmpl
- -rw-r--r-- 1 root root 2786 Oct 30 14:35 pre-revprop-change.tmpl
- -rw-r--r-- 1 root root 2100 Oct 30 14:35 pre-unlock.tmpl
- -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.tmpl爲post-commit,然後把原內容註釋掉,先修改下權限:
- [root@VM_centos hooks]# chown www:www post-commit
- [root@VM_centos hooks]# chmod a+x post-commit
然後修改內容:
Vim
- #!/bin/sh
- export LANG=zh_CN.UTF-8
- SVN_PATH=/usr/bin/svn #不用修改
- WEB_PATH=/home/www/abc #指向要同步到的目錄
- $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中了。
如果需要對其他版本庫進行類似的自動更新,則需要修改對應版本庫中hooks的post-commit。
但是還有個問題,我這裏的web server是用www用戶執行的,而自動同步到/home/www/abc中的文件,用戶組都是root的,即使手動改爲www,然後在本地commit、同步之後,文件還是變爲root。此問題待琢磨。(2014-10-30)
凡事都有湊合的方法的,鑑於目前沒找到高bigger的解決方案,故在post-commit裏直接加了句改用戶組的話,如下-,-(2014-10-31)
Vim
- #!/bin/sh
- export LANG=zh_CN.UTF-8
- SVN_PATH=/usr/bin/svn
- WEB_PATH=/home/www/abc
- $SVN_PATH update svn://111.111.111.111/project_1 $WEB_PATH --username test1 --password 5BYuu11CbNy7 --no-auth-cache
- 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
轉載自:http://tech.mclarian.com/a/973