linux 安装svn,并设置钩子来同步更新

来源:互联网 发布:邻家女孩服饰淘宝网 编辑:程序博客网 时间:2024/05/29 19:49

一,安装要的软件

wget http://subversion.tigris.org/downloads/subversion-1.6.1.tar.gz
wget http://subversion.tigris.org/downloads/subversion-deps-1.6.1.tar.gz

svn的官方网址是http://subversion.tigris.org

 

上面二个压缩文件解压后会放到同一个文件下,不要另建文件夹

二,安装独立svn

1,解压安装

  1. tar zxvf subversion-1.6.1.tar.gz  
  2. tar zxvf subversion-deps-1.6.1.tar.gz  
  3. cd subversion-1.6.1/  
  4. ./configure --prefix=/usr/local/svn  
  5. make && make install  

到这儿就安装好了。安装独立svn,它也考虑到将来有可能会和apache结合,所以安装的时候,它把和apache结合要用到的模块都放到/usr/lib/httpd/modules,部分提示如下
Libraries have been installed in:
/usr/lib/httpd/modules

If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and

2,查看svn信息

[root@BlackGhost bin]# /usr/local/svn/bin/svnserve –version
svnserve, version 1.6.1 (r37116)
compiled Jul  7 2010, 23:06:21

Copyright (C) 2000-2009 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.

Cyrus SASL authentication is available.

3,将svn的bin目录加到环境变量中去

[root@BlackGhost /]# PATH=$PATH:/usr/local/svn/bin
[root@BlackGhost /]# export PATH
[root@BlackGhost /]# svn
svn            svnadmin       svnlook        svnsync
svn2abs        svndumpfilter  svnserve       svnversion

三,建立仓库,配置svn

1,建个svn的根目录,因为项目不只一个

[zhangy@BlackGhost ~]$ mkdir -p /home/zhangy/www    #-p的意思是说如果没有父目录建之

2,建个仓库

[zhangy@BlackGhost ~]$ mkdir -p /home/zhangy/www/repos

[zhangy@BlackGhost www]$ svnadmin create /home/zhangy/www/repos/

3,导入数据

[zhangy@BlackGhost ~]$ svn import ./svntest file:///home/zhangy/www/repos -m “Initial repository test”
Adding         svntest/test.php

Committed revision 1.

4,修改svnserve.conf

[root@BlackGhost conf]# vi svnserve.conf

[general]
anon-access = none
auth-access = write
password-db = passwd
authz-db = authz

5,目录控制文件authz

[root@BlackGhost conf]# vi authz
[groups]

admin= zhangy

[repos:/]
@admin = rw
* = r

6,修改用户密码文件passwd

[root@BlackGhost conf]# vi passwd

[users]
zhangy = *****

四,启动和简单测试

1,启动svn

[root@BlackGhost www]# svnserve -d -r /home/zhangy/www

在这里特别的要注意,/home/zhangy/www是仓库的根目录,不要和[repos:/]目录重叠了。如果重叠是会提示你以下错误

[zhangy@BlackGhost checkout]$ svn co svn://127.0.0.1/
svn: Authorization failed

2,测试svn

a),checkout

[zhangy@BlackGhost checkout]$ svn co svn://127.0.0.1/repos
Authentication realm: <svn://127.0.0.1:3690> 3d0c32b1-3841-4518-b6b1-dcdb6c7ed716
Password for ‘zhangy’:
———————————————————————–
ATTENTION!  Your password for authentication realm:

<svn://127.0.0.1:3690> 3d0c32b1-3841-4518-b6b1-dcdb6c7ed716

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
‘/home/zhangy/.subversion/servers’.
———————————————————————–
Store password unencrypted (yes/no)? yes
A    repos/test.php
Checked out revision 1.

checkout的时候会出现Store password unencrypted,解决办法

vi /home/zhangy/.subversion/servers

找到以下内容,注释去掉并改成yes就行了

# store-plaintext-passwords = no

b),add 和submit

[zhangy@BlackGhost repos]$ svn add aaa.php
A         aaa.php
[zhangy@BlackGhost repos]$ svn commit aaa.php -m “ok”
Adding         aaa.php
Transmitting file data .
Committed revision 2.


二,设置钩子 

我们知道要把svn的内容更新到web目录需要手动的svn up,但是今天笔者教你自动更新web目录的方法,我们要在svn版本库中配置钩子来实现,就是创建一个post-commit的配置文件,对其进行简单的配置,简简单单的四步就可以实现Linux下SVN自动更新web目录配置。 

第一步:建立你的web程序目录 
mkdir /var/www/html/test 

   进入/var/www/html/test目录。 
   svn checkout svn://121.14.177.178:843/svntest 不重命名文件夹,直接在当前目录下检出 
    svn checkout svn://121.14.177.178:843/svntest test 检出文件并且重命名文件夹 
   
第二步:在项目库的 hooks/ 目录下新建 post-commit 文件 【钩子脚本】 
添加脚本内容如下: 
#!/bin/sh 
SVN=/usr/bin/svn           #这里配置的是svn安装bin目录下的svn文件 
WEB=/var/www/html/test     #要更新的目录 
$SVN update $WEB --username xxx --password xxx (此版本是linux下,windows下是.bat,写法少有不同) 
其中SVN=右边改成 svn 命令位置 
WEB=右边改成你实际的web目录 
第三步:让post-commit有执行的权限 chmod 777 post-commit 
第四步:这里就已经完成了,第四步就是测试了。 
说明: 
#!/bin/sh 说明是执行shell命令 
export LANG=zh_CN.GBK 是为了解决svn post commit 中文乱码,设置本地化编码,因为我的系统为GBK编码,SVN默认是UTF-8编码,如果不设置将会出现错误,而执行不成功,错误标识为svn: Can't convert string from native encoding to 'UTF-8' 
/usr/bin/svn update --username lxy --password 123456 /var/www/myproject 执行更新操作 
如果提示:post-commit hook failed (exit code 255) with no output赋予post-commit文件可执行权限 
如果您的默认编码就是UTF-8的,要上传中文文件,先将文件另存为UTF-8格式在提交 


6.实现SVN与WEB同步,可以CO一个出来,也可以直接配在仓库中

1)设置WEB服务器根目录为/var/www/webroot

2)checkout一份SVN

svn co svn://localhost/njlrxx /var/www/webroot/njlrxx

修改权限为WEB用户

chown -R apache:apache /var/www/webroot/njlrxx

3)建立同步脚本

cd /var/www/svndata/njlrxx/hooks/

cp post-commit.tmpl post-commit

编辑post-commit,在文件最后添加以下内容

复制代码
REPOS="$1"REV="$2"BASEPATH=/var/www/webroot/njlrxxWEBPATH="$BASEPATH/"export LANG=zh_CN.UTF-8svn update $WEBPATH --username jiqing --password 123456 --no-auth-cache
复制代码

 

增加脚本执行权限

chmod +x post-commit

最后操作是关闭服务然再打开服务:

svn服务的关闭:

killall svnserve

svn开启:

svnserve -d -r /var/www/svndata


0 0
原创粉丝点击