一.环境准备
1.linux版本为centos6.4 X64
2.软件安装
yum install subversion httpd mod_dav_svn mod_perl sendmail mailx wget gcc-c++ make unzip perl*
3.以下所有操作均在root用户下完成
二.SVN服务器配置
1.新建一个目录用于存储SVN所有文件
mkdir /home/svn
2.新建一个版本仓库
svnadmin create /home/svn/project
3.配置用户
vi /home/svn/project/conf/passwd
[users] admin = netqin #用户名 = 密码 user = 123456
4.配置用户访问策略
vi /home/svn/project/conf/authz
[groups] # harry_and_sally = harry,sally # harry_sally_and_joe = harry,sally,&joe project_a = admin #组名 = 用户名,多个用户使用逗号分隔 project_u = user [project:/] @project_a = rw #组名 = 读写权限r,w,rw;这里配置admin有根路径的读写权限 * = #其它组没有访问权限 [project:/server] @project_a = rw @project_u = rw * =
5.用户和策略配置升效
vi /home/svn/project/conf/svnserve.conf
[general] anon-access = none auth-access = write password-db = /home/svn/project/conf/passwd authz-db = /home/svn/project/conf/authz
6.启动服务器
svnserve -d -r /home/svn
7.开启防火墙
vi /etc/sysconfig/iptables
#svnserver -A INPUT -p tcp -m state --state NEW -m tcp --dport 3690 -j ACCEPT
service iptables restart
8.测试服务器
使用svn客户端工具,或者找一台安装了subversion的linux服务器进行测试
svn co svn://192.168.xx.xx/project
如果能够正常checkout,则说明svn的配置是正确的。
三.配置邮件提醒支持
1,安装Perl模块Module::Build
wget http://search.cpan.org/CPAN/authors/id/D/DA/DAGOLDEN/Module-Build-0.36_11.tar.gz
tar xvf Module-Build-0.36_11.tar.gz
cd Module-Build-0.36_11
perl Build.PL
./Build
./Build test
./Build install
2,安装Perl模块Authen::SASL
wget http://search.cpan.org/CPAN/authors/id/G/GB/GBARR/Authen-SASL-2.15.tar.gz
tar xvf Authen-SASL-2.15.tar.gz
cd Authen-SASL-2.15
perl Makefile.PL
make test
make install
3,安装Perl模块Net::SMTP_auth
wget http://search.cpan.org/CPAN/authors/id/A/AP/APLEINER/Net-SMTP_auth-0.08.tar.gz
tar xvf Net-SMTP_auth-0.08.tar.gz
cd Net-SMTP_auth-0.08
perl Makefile.PL
make test
make install
4,安装Perl模块SVN::Notify
wget http://search.cpan.org/CPAN/authors/id/D/DW/DWHEELER/SVN-Notify-2.80.tar.gz
ar xvf SVN-Notify-2.80.tar.gz
cd SVN-Notify-2.80
perl Build.PL
./Build
./Build test
./Build install
5,启动邮件服务器
service sendmail restart
6,配置自动发邮件脚本
cd /home/svn/project/hooks/
mv post-commit.tmpl post-commit
vi post-commit
#!/bin/sh REPOS="$1" REV="$2" /usr/local/bin/svnnotify --repos-path "$1" --revision "$2" --to [email protected],[email protected],[email protected] --from [email protected] --handler "HTML::ColorDiff" --with-diff --smtp localhost --smtp-user root --smtp-pass password -c "UTF-8" -g zh_CN -o raw --svnlook /usr/bin/svnlook --subject-prefix '[SVN Update]'
说明:--to:接收人列表,多个以逗号分隔;--from:发件人,随便起个名字;--smtp-user root --smtp-pass password 服务器的用户和密码;
chmod +x post-commit
ok,此时提交svn时就会收到邮件了。
四.提交svn时要求填写注释
cd /home/svn/project/hooks/
mv pre-commit.tmpl pre-commit
vi pre-commit
#!/bin/sh REPOS="$1" TXN="$2" SVNLOOK=/usr/bin/svnlook LOGMSG=`$SVNLOOK log -t "$TXN" "$REPOS" | grep "[a-zA-Z0-9]" | wc -c` if [ "$LOGMSG" -lt 5 ]; #要求注释长度大于5 then echo -e "\nEmpty log message not allowed. Commit aborted!" 1>&2 exit 1 fi
chmod +x pre-commit
mv pre-revprop-change.tmpl pre-revprop-change
vi pre-revprop-change
REPOS="$1" REV="$2" USER="$3" PROPNAME="$4" if ["$PROPNAME" = "svn:log"];then exit 0;fi exit 1
chmod +x pre-revprop-change
ok,测试提交svn时就会要求输入注释了。
五.支持http访问
经过以上配置,svn服务基本上就算配置完成了,不过此时只能通过svn://192.168.xx.xx/project的方式访问,如果希望通过http的方式访问,则还需进行如下配置。
上面安装工具时已经安装了Apache和Perl。
1,转换SVN服务器的密码
由于SVN服务器的密码是明文的,HTTP服务器不与支持,所以需要转换成HTTP支持的格式。可以写了一个Perl脚本完成这个工作.
cd /home/svn/project/conf/
vi PtoWP.pl
use warnings; use strict; #open the svn passwd file open (FILE, "passwd") or die ("Cannot open the passwd file!!!\n"); #clear the apache passwd file open (OUT_FILE, ">webpasswd") or die ("Cannot open the webpasswd file!!!\n"); close (OUT_FILE); #begin foreach (<FILE>) { if($_ =~ m/^[^#].*=/) { $_ =~ s/=//; `htpasswd -b webpasswd $_`; #核心语句就这个,如果以后添加或者修改用户密码,可以使用 htpasswd -nb username password ,加密后的信息会被显示在屏幕上,复制内容到webpasswd文件即可。 } }
perl PtoWP.pl #执行脚步后会在当前路径下生成webpasswd文件,里面就是已经加过密的密码信息。
2,修改httpd.conf,添加关于SVN服务器的内容
编辑/etc/httpd/conf/httpd.conf,在最后添加如下信息:
<Location /project>
DAV svn
SVNPath /home/svn/project/
AuthType Basic
AuthName "svn for project"
AuthUserFile /home/svn/project/conf/webpasswd #这里一定要使用webpasswd
AuthzSVNAccessFile /home/svn/project/conf/authz
Satisfy all
Require valid-user
</Location>
需要加载三个模块,如果是手工编译Apache时应该加上--enable-dav
LoadModule dav_module modules/mod_dav.so
LoadModule dav_svn_module modules/mod_dav_svn.so
LoadModule authz_svn_module modules/mod_authz_svn.so
3,启动HTTPD服务器
service httpd restart
4,授予txn-current-lock文件777权限
原因是svn目录是使用root用户创建,而Apache可能不是root用户启动的,所以在提交svn时Apache用户没有/home/svn/project/db/txn-current-lock文件的访问权限
chmod 777 /home/svn/project/db/txn-current-lock
5,测试
使用svn客户端工具,或者找一台安装了subversion的linux服务器进行测试
svn co http://192.168.xx.xx/project
六.备份svn
写个脚步,定期备份/home/svn下的内容即可。
举例:
mkdir -p /opt/project_backup #备份保存路径
vi /home/svn/project_backup.sh #备份脚步
#!/bin/bash cd /home/svn now=`/bin/date +%Y%m%d` /bin/tar czvf "project_backup_$now.tar.gz" project/ && rm -rf /opt/project_backup/* && /bin/mv project_backup_*.tar.gz /opt/project_backup/ if [ $? == 0 ] then result="OK!!" else result="False!!" fi #send mail to administrator /bin/mail -s "project_backup_$now" [email protected]<<MESSAGE Result: `/bin/echo $result` MESSAGE
chmod +x project_backup.sh
将脚步加入定时任务
crontab -e
0 23 * * * /home/svn/project_backup.sh #每天晚上11点备份
七.将svn启动脚步加入service
vi /home/svn/svnserver
# vi svnserver # chmod +x svnserver #!/bin/bash case "$1" in start) svnserve -d -r /home/svn svnport=`netstat -natp | grep svnserve | awk -F: '{print $2}' | awk '{print $1}'` if [ -n $svnport ] && [ $svnport -eq 3690 ]; then echo "SVN Server Already Runnning. Port:3690" else echo "SVN Server Does Not Start" fi ;; reload) svnport=`netstat -natp | grep svnserve | awk -F: '{print $2}' | awk '{print $1}'` if [ -n $svnport ] && [ $svnport -eq 3690 ]; then killall svnserve && svnserve -d -r /home/svn echo "Reload OK" else echo "SVN Server Is Not Running" fi ;; stop) killall svnserve echo "SVN Server Has Been Stopped" ;; status) svnport=`netstat -natp | grep svnserve | awk -F: '{print $2}' | awk '{print $1}'` pid=`ps aux | grep svnserve | grep -v "grep" | awk '{print $2}'` if [ -n $svnport ] && [ $svnport -eq 3690 ]; then echo "SVN Server (pid:$pid) 正在运行..." else echo "SVN Server 停止运行..." fi ;; *) echo "$0: Usage: $0 {start|status|stop|reload}" exit 1 ;; esac
mv /home/svn/svnserver /etc/init.d
chmod +x svnserver
启动svn:service svnserver start
关闭svn:service svnserver stop
重启svn:service svnserver reload
参考资料:http://www.cnblogs.com/wrmfw/archive/2011/09/08/2170465.html