svn同步web服务器端

使用svnadmin create 创建一个版本库:
svnadmin create REPO

每个版本库的目录下有一个hooks目录:

root@SVN:/home/svn/repo# ls /home/svn/repo/
conf dav db format hooks locks README.txt

在每个版本库下有hooks文件夹,里面有很多钩子程序:

root@SVN:/home/svn/repo# ls -l hooks/
total 40
-rwxr-xr-x 1 www-data www-data 332 2010-05-30 16:47 post-commit
-rw-r--r-- 1 www-data www-data 2000 2010-05-30 15:22 post-commit.tmpl
-rw-r--r-- 1 www-data www-data 1663 2010-05-29 23:28 post-lock.tmpl
-rw-r--r-- 1 www-data www-data 2322 2010-05-29 23:28 post-revprop-change.tmpl
-rw-r--r-- 1 www-data www-data 1592 2010-05-29 23:28 post-unlock.tmpl
-rw-r--r-- 1 www-data www-data 3488 2010-05-29 23:28 pre-commit.tmpl
-rw-r--r-- 1 www-data www-data 2410 2010-05-29 23:28 pre-lock.tmpl
-rw-r--r-- 1 www-data www-data 2796 2010-05-29 23:28 pre-revprop-change.tmpl
-rw-r--r-- 1 www-data www-data 2100 2010-05-29 23:28 pre-unlock.tmpl
-rw-r--r-- 1 www-data www-data 2830 2010-05-29 23:28 start-commit.tmpl

在执行commit操作之后会自动执行post-commit这个钩子程序。
因此可以设置post-commit来自动更新:


操作步骤如下:

1. 使用checkout建立一个工作复本

mkdir /home/web

root@SVN:/home# chown www-data:www-data web

ls -l web
drwxr-xr-x 2 www-data www-data 4096 2010-05-30 16:15 web

必须使用apache的所属用户和组(在ubuntu下面的是www-data)来执行:check out

root@SVN:/home/web# sudo su www-data
$ cd /home/web
$ svn checkout http://svn.love.com/svn/repo/www/

Authentication realm: <http://svn.love.com:80> Repo Auth
Password for 'www-data':
Authentication realm: <http://svn.love.com:80> Repo Auth
Username: jack
Password for 'jack':

-----------------------------------------------------------------------
ATTENTION! Your password for authentication realm:

<http://svn.love.com:80> Repo Auth

can only be stored to disk unencrypted! You are advised to configure
your system so that Subversion can store passwords encrypted, if
possible. See the documentation for details.

You can avoid future appearances of this warning by setting the value
of the 'store-plaintext-passwords' option to either 'yes' or 'no' in
'/var/www/.subversion/servers'.
-----------------------------------------------------------------------
Store password unencrypted (yes/no)? yes

连续输入2次 "yes"

root@SVN:/home/web# ls -al www/
drwxr-xr-x 6 www-data www-data 4096 2010-05-30 16:18 .svn
可以看到.svn的权限,userown、goupown都是www-data


2,使用svn update测试,看www-data用户是否有权限更新。

$ svn update /home/web/www --username svnuser --password svnpasswd
在连续输入2次"yes"或者"no"之后、可以看到已经更新成功:
Store password unencrypted (yes/no)? At revision 37.

3,在hooks目录下面的添加一个post-commit脚本文件

#!/bin/sh

REPOS="$1"
REV="$2"
export LANG=en_US.UTF-8
svn update /home/web/www --username svnuser --password svnpasswd

在客户端commit后报错
sudo su www-data
$ cd /home/svn/repo/hooks
$ ./post-commit

提示:
Store password unencrypted (yes/no)?

在svn update --help中找到了 --no-auth-cache 这个参数:

--no-auth-cache : do not cache authentication tokens

加上这个参数后终于可以了:

root@SVN:/home/svn/repo/hooks# cat post-commit

#!/bin/sh

REPOS="$1"
REV="$2"
export LANG=en_US.UTF-8
svn update /home/web/www --username svnuser --password svnpasswd --no-auth-cache


参考资料来自:

http://blog.sina.com.cn/s/blog_4c5fc6950100h18b.html


为了便于排错,可以将脚本的错误输出、以及版本信息、执行用户的名称保存到日志里面。
#!/bin/sh

REPOS="$1"
REV="$2"
export LANG=en_US.UTF-8
echo `whoami`,$REPOS,$REV >> /tmp/svn_hooks.log
svn update /home/web/www --username svnuser --password svnpasswd --no-auth-cache 2> /home/web/svn_hook.log
发布了61 篇原创文章 · 获赞 0 · 访问量 2704
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章