关闭

SVN – 搭建 SVN 服务器 && 多项目分别建立版本库 && 同步至生产环境

742人阅读 评论(0) 收藏 举报
分类:

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

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到本地

1

 

 

输入账号密码
2

3

3

然后在本地的SVN文件夹里新建个文件然后commit

5

6

那么,这就算完成了。

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_1project_2project_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.tmplpost-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中了。

如果需要对其他版本库进行类似的自动更新,则需要修改对应版本库中hookspost-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

配置SVN流程


转载自:http://tech.mclarian.com/a/973


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:49877次
    • 积分:1175
    • 等级:
    • 排名:千里之外
    • 原创:50篇
    • 转载:121篇
    • 译文:0篇
    • 评论:3条