一、目的
通过SVN再带工具svnsync来实现版本库的同步,实现异地开发中心版本库向北京中心的实时同步。
二、搭建SVN服务器
异地开发中心SVN服务器已经投入使用,需要在北京本地安装配置SVN服务器。
以Linux操作系统为例:
1、下载SVN安装所需的软件
sqlite-amalgamation-3.6.13.tar.gz
apr-1.2.7.tar.gz
apr-util-1.2.7.tar.gz
httpd-2.2.13.tar.bz2
subversion-1.6.6.tar.bz2
2、检查SVN是否安装成功
在安装服务器上执行svnadmin命令,是否有相应。
3、在北京中心创建镜像库
#mkdir –p /data/svn //创建SVN库存储目录
#svnadmin create mir //创建镜像库,名字可根据实际调整
#netstat -nltp //如果有3690端口在监听,说明运行成功
4、修改配置文件
#cd /data/svn/mir/conf
将svnserve.conf去掉下面几行的注释:
anon-access = read
auth-access = write
password-db = passwd
注意: 上述三行前面不能留有空格,否则要出错。
5、修改密码文件
修改密码文件passwd,添加账号和密码
三、配置手工同步
1、修改同步文件
#cp pre-revprop-change.tmpl pre-revprop-change
#chmod +x pre-revprop-change
#chmod 777 pre-revprop-change
2、同步初始化
#svnsync init file:///data/svn/mir/ http://IP(远程地址) /svn/test_dir
此时北京mir库就是远程test_dir库的异地镜像拷贝
3、手工同步
#svnsync sync svn://IP(远程地址) --username username --password password //IP地址、username、password根据实际填写
通过手工同步方式比较麻烦,需要配置自动同步来实现实时自动同步
四、配置实时同步
自动实时同步需要在异地源SVN服务器上进行配置。
#cd /data/svn/test_dir/hooks
该目录有两个重要的文件:pre-commit.tmpl和post-commit.tmpl
pre-commit.tmpl:该文件是SVN每次提交之前都要执行的脚本;
post-commit.tmpl:该文件是SVN每次提交之后执行的脚本。
此时我们若将同步命令svnsync init file:///data/svn/mir/ http://IP(北京地址)/svn/test_dir写入上述2个脚本中的一个,显然就可以达到实时同步的目的了。
建议:最好将svnsync init file:///data/svn/mir/ http://IP(北京地址)/svn/test_dir写入post-commit脚本里,而非pre-commit中,如果写入pre-commit中,是每次提交前同步,显然镜像库会比源库一直会落后一个版本。
#cp post-commit.tmpl post-commit
#chmod +x post-commit
#chmod 777 post-commit
文件内容如下:
# cat post-commit
#!/bin/sh
# POST-COMMIT HOOK #
# The post-commit hook is invoked after a commit. Subversion runs # this hook by invoking a program (script, executable, binary, etc.) # named 'post-commit' (for which this file is a template) with the # following ordered arguments: #
# [1] REPOS-PATH (the path to this repository)
# [2] REV (the number of the revision just committed) #
# The default working directory for the invocation is undefined, so # the program should set one explicitly if it cares. #
# Because the commit has already completed and cannot be undone, # the exit code of the hook program is ignored. The hook program # can use the 'svnlook' utility to help it examine the # newly-committed tree. #
# On a Unix system, the normal procedure is to have 'post-commit' # invoke other programs to do the real work, though it may do the # work itself too. #
# Note that 'post-commit' must be executable by the user(s) who will # invoke it (typically the user httpd runs as), and that user must # have filesystem-level permission to access the repository.
#
# On a Windows system, you should name the hook program # 'post-commit.bat' or 'post-commit.exe', # but the basic idea is the same. #
# The hook program typically does not inherit the environment of # its parent process. For example, a common problem is for the # PATH environment variable to not be set to its usual value, so # that subprograms fail to launch unless invoked via absolute path. # If you're having unexpected problems with a hook program, the # culprit may be unusual (or missing) environment variables. #
# Here is an example hook script, for a Unix /bin/sh interpreter. # For more examples and pre-written hooks, see those in # the Subversion repository at
# http://svn.collab.net/repos/svn/trunk/tools/hook-scripts/ and # http://svn.collab.net/repos/svn/trunk/contrib/hook-scripts/ REPOS=\"$1\" REV=\"$2\"
commit-email.pl \"$REPOS\" \"$REV\" commit-watchers@example.org log-commit.py --repository \"$REPOS\" --revision \"$REV\"
svnsync sync svn://ip(北京地址) --username username --password password
根据以上配置以后,在远程主机上提交相应的文件版本更新后,北京中心也会同步更新。
五、总结
实际中开发的代码或者文件都可以提交到异地各中心的SVN上,而此时北京中心服务器上会形成一个和异地中心服务器上一模一样的镜像库,关键是这种同步是实时的。
所以不管异地中心服务器的SVN因为OS还是硬件故障挂掉,我们的北京中心服务器都可以立马接替它的工作。SVN挂掉不影响团队工作,最主要的是做好了SVN的数据安全,更重要的是这种数据是实时的。
因篇幅问题不能全部显示,请点此查看更多更全内容