经验总结 svn 服务器一定要重启, 配置 一定要看对 尤其是real 指定 。
阅读数:2005
1、安装Subversion
[cpp] view plain copy
- [root@VM_centos /]# yum install subversion
2、找一下安装位置
[cpp] view plain copy
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. ...
然后可以稍微看一下帮助
[cpp] view plain copy
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
5. Type 'svn --version'
6. 'svn --version --quiet'
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
22. diff (di)
23. export
24. help (?, h)
3、创建SVN版本库目录
[cpp] view plain copy
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、创建版本库
[cpp] view plain copy
1. [root@VM_centos test]# svnadmin create /home/svn/test
那么在/home/svn/test下面出现了这几个东西
[cpp] view plain copy
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目录下都是些什么货
[cpp] view plain copy
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
[cpp] view plain copy
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.
[cpp] view plain copy
- <p>[users]</p><p># harry = harryssecret</p><p># sally = sallyssecret</p>
在“[users]”中添加用户名密码,格式:用户名=密码,如下:
Vim
[cpp] view plain copy
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
[cpp] view plain copy
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
[cpp] view plain copy
- [/]
- test=rw
- otheruser=r
- # ...
- # 意思就是
- # [/] 在版本库的根目录下
修改svnserve.conf文件
Vim
[cpp] view plain copy
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版本库
[cpp] view plain copy
1. [root@VM_centos test]# svnserve -d -r /home/svn/test
关闭所有svn killall svnserve
7、测试
比如,我就在我本地测试
把SVN中的文件checkout到本地
然后在本地的SVN文件夹里新建个文件然后commit
那么,这就算完成了。
END。
还没完!
在按照上面做完之后,在服务器上checkout的时候发现了一个小问题
比如在服务器的某个目录下:
[cpp] view plain copy
1. [root@VM_centos wwwsvn]# svn checkout svn://111.111.111.111 --username test --password xxx
之后就会在“wwwsvn”目录下建立名为“111.111.111.111”的目录,目录中是按照上面设置的版本库的文件。而且只能有这一个版本库,没有其他的。
这显然不是我想要的结果,于是又稍微琢磨了一下,目的是为多个项目分别建立版本库。(这里参考了:这个、这个、这个,呃…还有,这个。其实都大同小异,或许有两个文章的内容都差不多,不过不要在意这些细节。)
那么首先要重复执行建立版本库:
[cpp] view plain copy
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_1、project_2、project_3这三个目录
然后随便进一个、比如进project_1,修改conf里的三个配置文件
Vim
[cpp] view plain copy
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
[cpp] view plain copy
1. ###passwd###
2. [users]
3. test1 = 5BYuu11CbNy7
4. test2 = 88AhI8q2QaGe
5. test3 = 9h1s3hQpLB4I
然后再去修改另外两个项目的配置文件(如不修改,则可以checkout,但是无法commit,应该是只读不可写。具体哪个配置造成的这个局面,暂没有详细琢磨。)
然后启动版本库
[cpp] view plain copy
1. [root@VM_centos test]# svnserve -d -r /home/svn
这样之后,/home/svn/目录下的三个版本库就都可以用了。
END。
还!没!完!
在第一次“END。”之前就在琢磨,比如我是一个web系统,用户在本地commit之后,能否直接更新到web目录中从而不用什么操作直接访问服务器就能看到最新的改动。
那么就涉及到了一些触发同步的东西。
如上面所述,在每一个svn版本库中都有这么几个东西:
[cpp] view plain copy
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/目录下,有一些奇怪的东西:
[cpp] view plain copy
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.tmpl为post-commit,然后把原内容注释掉,先修改下权限:
[cpp] view plain copy
1. [root@VM_centos hooks]# chown www:www post-commit
2. [root@VM_centos hooks]# chmod a+x post-commit
然后修改内容:
Vim
[cpp] view plain copy
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中了。
如果需要对其他版本库进行类似的自动更新,则需要修改对应版本库中hooks的post-commit。
但是还有个问题,我这里的web server是用www用户执行的,而自动同步到/home/www/abc中的文件,用户组都是root的,即使手动改为www,然后在本地commit、同步之后,文件还是变为root。此问题待琢磨。(2014-10-30)
凡事都有凑合的方法的,鉴于目前没找到高bigger的解决方案,故在post-commit里直接加了句改用户组的话,如下-,-(2014-10-31)
Vim
[cpp] view plain copy
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
重启防火墙让配置生效
[root@VM_centos wwwsvn]# svn checkout svn://111.111.111.111 --username test --password xxx