subversion(以下简称svn)是近年来崛起的版本管理工具,是cvs的接班人。
svn服务器有2种运行方式:独立服务器和借助apache。2种方式各有利弊。
svn存储版本数据也有2种方式:BDB和FSFS。因为BDB方式在服务器中断时,有可能锁住数据,所以还是FSFS方式更安全一点。
1. svn服务器安装
操作系统: Redhat Linux AS4
获取svn安装包
最新版本subversion可以在http://subversion.tigris.org/getting.html 下载。
编译svn
以root用户登录。
将subversion-1.4.0.tar.gz和subversion-deps-1.4.0.tar.gz传到服务器。
tar xfvz subversion-1.4.0.tar.gz
tar xfvz subversion-deps-1.4.0.tar.gz
cd subversion-1.4.0
./configure –prefix=/opt/svn –without-berkeley-db –with-zlib
(注:以svnserve方式运行,不加apache编译参数。以fsfs格式存储版本库,不编译berkeley-db)
make clean
make
make install
vi /etc/profile,在/etc/profile最后加入:
PATH=$PATH:/opt/svn/bin
export PATH
svn测试
svnserve –version
如果显示如下,svn安装成功:
svnserve, version 1.4.0 (r21228)
compiled Oct 12 2006, 10:18:56Copyright (C) 2000-2006 CollabNet.
Subversion is open source software, see http://subversion.tigris.org/
This product includes software developed by CollabNet (http://www.Collab.Net/).
The following repository back-end (FS) modules are available:
* fs_fs : Module for working with a plain file (FSFS) repository.
2. svn配置
建立svn版本库目录
可建多个:
mkdir -p /opt/svndata/repos1
mkdir -p /opt/svndata/repos2
建立svn版本库
svnadmin create /opt/svndata/repos1
svnadmin create /opt/svndata/repos2
修改svn版本库配置文件
版本库1:
vi /opt/svndata/repos1/conf/svnserve.conf
内容修改为:
[general]
anon-access = none
auth-access = write
password-db = /opt/svn/conf/pwd.conf
authz-db = /opt/svn/conf/authz.conf
realm = repos1
版本库2:
vi /opt/svndata/repos2/conf/svnserve.conf
内容修改为:
[general]
anon-access = none
auth-access = write
password-db = /opt/svn/conf/pwd.conf
authz-db = /opt/svn/conf/authz.conf
realm = repos2
即除realm = repos2外,其他与版本库1配置文件完全相同。如果有更多的版本库,依此类推。
删除svn文件库:
尝试如下:
1、输入:rm -rf repos1
如果提示访问拒绝。进到 repos/db/revs里面发现里面的文件权限都是
-r--r--r--
这样的,所以谁也不能用上面的命令进行删除了。
2、输入:rm -R repos_backup1
遇到每个文件都提示:是否重写权限,选择是,才会删除成功:
override rwxrwx--- 4294967294/users for 1117/repos1/db/revprops/11249?
一个库往往有数十个万个文件,这种删除方式显然不现实。
3、输入:chmod -R 777 repos_backup1;rm -R repos_backup1
等待半天,提示堆栈溢出。输入ulimit -a查看,堆栈限制为7715;修改这个参数必须重启系统,服务器级别比较高,不敢重启,只好作罢。
4、用脚本来进行删除。
删除备份方案设计如下:
目标:定期删除备份数据,并保证备份数据一直介于7到16份之间
步骤:1、每月1号删除上个月21号之前所有数据
2、每月15号删除上个月所有数据和本月8号前的所有数据
3、每月25号删除本月19号之前的所有数据。
用perl脚本实现,代码如下:
#! /usr/bin/perl
# Author:xuejiang
# Site: http://www.scmbbs.com
#本单元:定期删除备份数据,并保证备份数据一直介于7到16份之间,
#适用:备份目录以mmdd的日期格式命名。
use Cwd;
use File::Find;
&main();
sub main()
{
#取得当前时间,根据时间设置定期进行删除
my $targetDir="/home/backup/svn/hotcopy2/"; #定义备份目录。
my( $sec, $min, $hour, $day, $month, $year,$wday,$yday ) = localtime( time() );
$month++;
my $today = sprintf( "%02d", $day);
if ($today !~ /(01$|15$|25$|)/)
{
exit(0);
}
my $monthday= sprintf( "%02d%02d", $month, $day);
my $thismonth= sprintf("%02d",$month);
my ($lastmonth,$passdate,$tempstr);
#定义删除的目录数组,对每个目录调用函数deleteDir进行删除。
chdir($targetDir);
my @dir=<*>;
print @dir;
if($thismonth=~/01/)
{
$lastmonth='12';
}else
{
$lastmonth=$thismonth - 1;
}
foreach my $dir(@dir)
{
if ($dir =~ /[^0-9]/)
{
next;
}
#每月1号删除上个月21号之前所有数据
if ($today=~/01$/)
{
$passdate=$lastmonth.'21';
if ($lastmonth == '12')
{
$tempstr= substr($dir,0,2);
if (($tempstr ne "01")&&($dir < $passdate))
{
&deleteDir($targetDir.$dir);
}
}else
{
if ($dir < $passdate)
{
&deleteDir($targetDir.$dir);
}
}
}
#每月15号删除上个月所有数据和本月8号前的所有数据
if ($today=~ /15$/)
{
$passdate=$thismonth.'08';
if ($dir < $passdate)
{
&deleteDir($targetDir.$dir);
}
if ($lastmonth == '12')
{
$tempstr= substr($dir,0,2);
if ($tempstr == "12")
{
&deleteDir($targetDir.$dir);
}
}
}
#每月25号删除本月19号之前的所有数据。
if ($today=~ /25$/)
{
$passdate=$thismonth.'19';
if ($dir < $passdate)
{
&deleteDir($targetDir.$dir);
}
if ($lastmonth == '12')
{
$tempstr= substr($dir,0,2);
if ($tempstr == "12")
{
&deleteDir($targetDir.$dir);
}
}
}
}
}
#递归遍历各目录,遍历各个文件,每个文件先进行chmod 777 filename操作,再进行 rm -rf filename操作。
sub deleteDir($)
{
my ($dir)=@_;
print "log in the dir:$dir /n";
my $handle;
opendir($handle,$dir);
while(defined(my $ndir=readdir($handle)))
{
if (($ndir eq ".")||($ndir eq ".."))
{
next;
}
$ndir=$dir."/".$ndir;
if (-f $ndir)
{
ls `chmod 777 $ndir`; ##如果需要此步。
print "file:$ndir/n" if unlink($ndir);
}else
{
print "Dir:$ndir/n";
deleteDir($ndir);
}
}
closedir($handle);
rmdir($dir);
print "now locate:$dir/n";
}
配置允许访问的svn用户
vi /opt/svn/conf/pwd.conf
为了简化配置,2个版本库共用1个用户配置文件。如有必要,也可以分开。
注意:对用户配置文件的修改立即生效,不必重启svn。
文件格式如下:
[users]
<用户1> = <密码1>
<用户2> = <密码2>
其中,[users]是必须的。下面列出要访问svn的用户,每个用户一行。示例:
[users]
alan = password
king = hello
配置svn用户访问权限
vi /opt/svn/conf/authz.conf
为了简化配置,3个版本库共用1个权限配置文件/opt/svn/conf/pwd.conf。如有必要,也可以分开。文件中定义用户组和版本库目录权限。
注意:
* 权限配置文件中出现的用户名必须已在用户配置文件中定义。
* 对权限配置文件的修改立即生效,不必重启svn。
用户组格式:
[groups]
<用户组名> = <用户1>,<用户2>
其中,1个用户组可以包含1个或多个用户,用户间以逗号分隔。
版本库目录格式:
[<版本库>:/项目/目录]
@<用户组名> = <权限>
<用户名> = <权限>
其中,方框号内部分可以有多种写法:
/,表示根目录及以下。根目录是svnserve启动时指定的,我们指定为/opt/svndata。这样,/就是表示对全部版本库设置权限。
repos1:/,表示对版本库1设置权限
repos2:/abc, ,表示对版本库2中的abc项目设置权限
repos2:/abc/aaa, ,表示对版本库2中的abc项目的aaa目录设置权限
权限主体可以是用户组、用户或*,用户组在前面加@,*表示全部用户。权限可以是w、r、wr和空,空表示没有任何权限。
示例:
[groups]
admin = alan
[/]
@admin = rw
[repos1:/abc/aaa]
king = rw
[repos2:/pass]
king =
svn配置完毕,删除无用文件
rm /opt/svndata/repos1/conf/authz
rm /opt/svndata/repos1/conf/passwd
rm /opt/svndata/repos2/conf/authz
rm /opt/svndata/repos2/conf/passwd
3. 启动svn
建立启动svn的用户
useradd svn
passwd svn
根据提示为用户svn设置密码
允许用户svn访问版本库
chown -R svn:svn /opt/svndata
chown -R svn:svn /opt/data
启动svn
su - svn -c “svnserve -d –listen-port 9999 -r /opt/svndata”
其中:
su - svn表示以用户svn的身份启动svn
-d表示以daemon方式(后台运行)运行
–listen-port 9999表示使用9999端口,可以换成你需要的端口。但注意,使用1024以下的端口需要root权限
-r /opt/svndata指定根目录是/opt/svndata
检查:
ps -ef|grep svnserve
如果显示如下,即为启动成功:
svn 6941 1 0 15:07 ? 00:00:00 svnserve -d –listen-port 9999 -r /opt/svndata
二:通过web方式访问svn
安装配置方法如下:
安装apache
wget http://www.wmwweb.com/apache/httpd/httpd-2.2.3.tar.gz
tar xfvz httpd-2.2.3.tar.gz
cd httpd-2.2.3
./configure –prefix=/usr/local/apache2 –enable-module=so –enable-module=setenvif –enable-module=rewrite –enable-rewrite=shared –enable-proxy=shared –with-mpm=prefork –enable-so –enable-auth-anon –enable-file-cache=shared –enable-cache=shared –enable-disk-cache=shared –enable-mem-cache=shared
make clean
make
make install
配置apache
增加用户apache
useradd apache
passwd apache
按照提示输入密码
修改httpd.conf
vi /usr/local/apache2/conf/httpd.conf
User daemon
Group daemon
改成
User apache
Group apache
Options Indexes FollowSymLinks
改成
Options -ExecCGI -Indexes FollowSymLinks
Order allow,deny
Deny from all
Satisfy All
之后增加
Order allow,deny
Deny from all
Satisfy All
Order allow,deny
Deny from all
Satisfy All
AddType application/x-gzip .gz .tgz
之后增加
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
DirectoryIndex index.html
改成
DirectoryIndex index.html index.php
安装php
wget http://cn2.php.net/get/php-5.1.6.tar.gz/from/this/mirror
tar xfvz php-5.1.6.tar.gz
cd php-5.1.6
./configure –prefix=/usr/local/php –with-apxs2=/usr/local/apache2/bin/apxs –enable-mbstring –enable-track-vars –enable-calendar –enable-trans-sid –with-gettext –enable-sysvmsg –enable-sockets –enable-bcmath –enable-wddx –disable-debug –with-dom=/usr/local
make clean
make
make install
cp php.ini-recommended /usr/local/apache2/conf/php.ini
安装websvn
cd /usr/local/apache2/htdocs
mkdir websvn
cd websvn
wget -np -nH –cut-dirs=4 -r -v http://guest:@websvn.tigris.org/svn/websvn/tags/2.0rc4/
配置websvn
配置config.inc
cd include
cp distconfig.inc config.inc
vi config.inc
编辑文件,使其只包含以下几项内容:
版本库,格式$config->addRepository(<显示名>, svn串, group, <用户名>, <密码>);
如 $config->addRepository(”docs”, “svn://192.168.0.1:1234/repos1/test”, NULL, “test”, “test1″);
svn串可以指定到任意目录,svn协议是通过svnserve访问版本库,所以受到svnserve用户和权限的限制,这样比较安全
指定的用户名和密码必须对目录有读权限,建议为websvn单独设置用户。
指定模版:
$config->setTemplatePath(”$locwebsvnreal/templates/BlueGrey/”);
指定字符集:
$config->setInputEncoding(”UTF-8″);
$config->setContentEncoding(”GB18030″);
指定语言:
include ‘languages/schinese.inc’;
允许下载:
$config->allowDownload();
其他:
$config->setMinDownloadLevel(2);
$config->setCachingOn();
$config->expandTabsBy(4);
修改websvn模版
由于中文和英文宽度的差异,需要做一些修改。
通过ftp,将/usr/local/apache2/htdocs/websvn/languages/schinese.inc下载到windows机器上。将文件中的”查看记录”改成”查看”,再ftp回Linux机器。
vi /usr/local/apache2/htdocs/websvn/templates/BlueGrey/directory.tmp
修改或添加下图红色的部分(HTML贴上来总是报错,只能以图说明了):
运行websvn
/usr/local/apache2/bin/apachectl start
用浏览器访问http://192.168.0.1/websvn就可以浏览svn版本库了。
特殊说明
目前的配置下,websvn可以正确显示文件中GB编码的汉字,但不能显示UTF-8格式的汉字。可以满足大多数情况下的要求。