最近研究Windows svn的备份机制,结合网上的文章,分享下完成的svn dump的备份脚本,记录一下svnsync的使用方法。
1.svnadmin dump本地备份脚本
不多说了,直接贴脚本。
@echo off
rem SVN库父目录
set SVN_BASE=
rem SVN库名
set SVN_REPOSITORY=
rem 备份方式 full:全量;inc:增量
set DUMP_MOD=full
rem 备份文件
set DUMP_TARGET=
rem 是否压缩备份文件
set COMPRESS_DUMP_FILE=1
rem 存放本程序生成的中间信息的文件夹
set DUMP_HISTORY_INFO_DIR=info
rem 存放最后一次备份时的版本信息
set DUMP_LAST_LOG=last.info
rem 存放所有备份的日志
set DUMP_LOG=dump.log
set cmd_svnadmin=svnadmin
set VALID_ARG=1
goto :main
:usage
echo Usage:
echo backup -base SVN_PATH -rep REPOSITORY [-full^|-inc] -target PATH
echo Warning:
echo Any path with blank will make an unkown result.
goto :eof
:main
rem 解析参数
call :parseArg
if "%VALID_ARG%" equ "0" (
call :usage
set ERRORLEVEL=1
goto :eof
)
rem 以下是程序使用的变量,勿随便更改
rem 开始和结束版本
set REV_START=
set REV_END=
set LAST_DUMP_DATE=
set LAST_DUMP_COMMENT=
rem 备份文件(自动生成)、临时信息文件夹、日志文件等
set DUMP_FILE=
set DUMP_INFO_DIR=%DUMP_TARGET%\%SVN_REPOSITORY%\%DUMP_HISTORY_INFO_DIR%
set DUMP_LAST_LOG_FILE=%DUMP_INFO_DIR%\%DUMP_LAST_LOG%
set DUMP_LOG_FILE=%DUMP_LAST_LOG_FILE%\%DUMP_LOG%
set SVN_PATH=%SVN_BASE%\%SVN_REPOSITORY%
rem 确定备份文件名
call :FORMAT_DUMP_FILE
echo Using dump file "%DUMP_FILE%".
rem 确定备份版本
call :parseRev
echo Dump from %REV_START% to %REV_END%
rem 开始备份
call :dobackup
goto :eof
:FORMAT_DUMP_FILE
call :getDate "" "" date time
set DUMP_FILE=%SVN_REPOSITORY%_%date%_%time%.dump
goto :eof
:getDate
set DATE_SEPERATOR="%~1"
set TIME_SEPERATOR="%~2"
>"%temp%/now.vbs" echo currenttime=now
>>"%temp%/now.vbs" echo curdate=right(year(currenttime),4)^&%DATE_SEPERATOR%^&right("0"^&month(currenttime),2)^&%DATE_SEPERATOR%^&right("0"^&day(currenttime),2)
>>"%temp%/now.vbs" echo curtime=right("0"^&hour(currenttime),2)^&%TIME_SEPERATOR%^&right("0"^&minute(currenttime),2)^&%TIME_SEPERATOR%^&right("0"^&second(currenttime),2)
>>"%temp%/now.vbs" echo wscript.echo curdate^&" "^&curtime
for /f "tokens=1,2 delims= " %%a in ('cscript /nologo "%temp%/now.vbs"') do (
set "today=%%a"
set "curtime=%%b"
)
if not "%3" == "" set "%~3=%today%"
if not "%4" == "" set "%~4=%curtime%"
goto :eof
:parseRev
for /f %%a in ('svnlook youngest %SVN_PATH%') do set REV_END=%%a
if "%DUMP_MOD%" equ "full" (
REV_START=0
goto :eof
)
for /f %%a in ("%DUMP_LAST_LOG_FILE%") do call :parseRev1 "REV_START" "LAST_DUMP_DATE" "LAST_DUMP_COMMENT"
if "REV_START" equ "" (
set REV_START=0
set LAST_DUMP_DATE=
set LAST_DUMP_COMMENT=
)
goto :eof
rem parseRev1 line last date comment
:parseRev1
set line=%~1
if /i "%line:~0,4%" equ "last" set "%~2=%line:~5%"
if /i "%line:~0,4%" equ "date" set "%~3=%line:~5%"
if /i "%line:~0,6%" equ "comment" set "%~4=%line:~7%"
goto :eof
:parseArg
:parseArgStart
if "%1" equ "" goto :parseArgEnd
if /i "-base" equ "%~1" (
set SVN_BASE=%~2
shift
shift
)
if /i "-rep" equ "%~1" (
set SVN_REPOSITORY=%~2
shift
shift
)
if /i "-full" equ "%~1" (
set DUMP_MOD=full
shift
)
if /i "-inc" equ "%~1" (
set DUMP_MOD=inc
shift
)
if /i "-target" equ "%~1" (
set DUMP_TARGET=%~2
shift
shift
)
goto :parseArgStart
:parseArgEnd
call :blankAndHelp "SVN_BASE"
call :blankAndHelp "SVN_REPOSITORY"
call :blankAndHelp "DUMP_TARGET"
goto :eof
:blankAndHelp
set _WORD_=%~1
call set _VALUE_=%%%_WORD_%%%
if "%_VALUE_%" equ "" (
set VALID_ARG=0
)
goto :eof
:dobackup
>>%DUMP_LOG_FILE% echo [%today% %curtime%] Trying backup respository "%SVN_PATH%" to "%DUMP_FILE%".
if "REV_START" equ "REV_END" (
>>%DUMP_LOG_FILE% echo [%today% %curtime%] Respository "%SVN_PATH%" has no changes. The youngest revision is %REV_END%.
>%DUMP_LAST_LOG_FILE% echo comment:[%today% %curtime%] Respository "%SVN_PATH%" has no changes. The youngest revision is %REV_END%.
>>%DUMP_LAST_LOG_FILE% echo last:%REV_END%.
>>%DUMP_LAST_LOG_FILE% echo date:%today% %curtime%].
) else (
1>>%DUMP_LOG_FILE% 2>&1 %cmd_svnadmin% dump --revision %REV_START%:%REV_END% %SVN_PATH%>%DUMP_FILE%
if ERRORLEVEL 1 (
>>%DUMP_LOG_FILE% echo [%today% %curtime%] Dump repository "%SVN_PATH%" failed with returen value %ERRORLEVEL%. The youngest dumped revision is %REV_START%.
>%DUMP_LAST_LOG_FILE% echo comment:[%today% %curtime%] Dump repository "%SVN_PATH%" failed with returen value %ERRORLEVEL%. The youngest dumped revision is %REV_START%.
>>%DUMP_LAST_LOG_FILE% echo last:%REV_START%.
>>%DUMP_LAST_LOG_FILE% echo date:%today% %curtime%].
del /Q "%DUMP_FILE%"
) else (
if "COMPRESS_DUMP_FILE" equ "1" makecab %DUMP_FILE% %DUMP_FILE%.zip
)
)
>>%DUMP_LOG_FILE% echo [%today% %curtime%] Dump respository "%SVN_PATH%" has finished. The youngest revision is %REV_END%.
goto :eof
2.svnsync异地备份记录
这里先说一下svnsync的用法,在讲一下备份搭建过程,本文中省略部分建库和建用户的具体步骤。
svnsync init --trust-server-cert --source-username ARG --source-password ARG --sync-username ARG --sync-password ARG DEST_URL SOURCE_URL
svnsync sync --trust-server-cert --non-interactive --source-username ARG --source-password ARG --sync-username ARG --sync-password ARG DEST_UR
注:如果svnsync版本不支持source-username、sync-username分别设置两个库的用户和口令,可以用--username、--password设置源库和备份库的用户和口令,但是两个库的用户和口令必须相同。
--trust-server-cert 信任服务器证书(如https自签名证书)
--source-username 源库用户名
--sync-username 镜像库用户名
ARG DEST_URL、SOURCE_URL 镜像库地址,源库地址
--non-interactive 非交互模式
下面是搭建过程
源 库:https://192.168.1.2:443/svn/lsgl
服务器路径 e:\svn\lsgl
同步用户 syncuser:passwd
镜像库:https://192.168.1.3:443/svn/lsgl
服务器路径 d:\svn\lsgl
同步用户 syncuser:passwd
1)如果源库不存在,请新建源库,这里不做介绍了,然后新建用户syncuser,并授予读取权限。
以下在镜像服务器:
2)新建一个新库
mkdir d:\svn\lsgl
cd d:\svn\lsgl
svnadmin create
3)在镜像库新建用户syncuser,并授予读写权限。
4)新建文件d:\svn\lsgl\hooks\pre-revprop-change.bat,只允许同步用户修改镜像库属性,内容如下:
IF "%3" == "syncuser" exit 0
echo"Only syncuser may change revision properties">&2
exit 1
5)新建d:\svn\lsgl\hooks\start-commit.bat,只允许同步用户提交新版本,内容如下:
IF "%3" == "syncuser" exit 0
echo"Only syncuser may commit new revisions">&2
exit 1
6)执行命令
svnsync init --username syncuser --password passwd https://192.168.1.3:443/svn/lsgl https://192.168.1.2:443/svn/lsgl
以下在源库服务器执行:
7)新建e:\svn\lsgl\hooks\post-commit.bat,实时备份数据,内容如下。
svnsync --trust-server-cert --non-interactive --username syncuser --password passwd sync https://192.168.1.3:443/svn/lsgl
至此完成。
还有些话要说,在搭建备份库的时候,新建库后直接开启同步,从r0同步至head;在网上看到新建库后,load数据再进行同步,将方法记录在下面,笔者并没有验证。
先准备源库的dump文件。
svnadmin dump e:\svn\lsgl > source-lsgl.dump
将上面的第6)步提换如下:
svnadmin load d:\svn\lsgl < source-lsgl.dump
然后手动设置镜像库的属性
svn propset --revprop -r0 svn:sync-from-uuid 源库的UUID
svn propset --revprop -r0 svn:sync-last-merged-rev dump文件的最新版本 https://192.168.1.3:443/svn/lsgl
svn propset --revprop -r0 svn:sync-from-url https://192.168.1.3:443/svn/lsgl。
还有话要说,如何切换备库:
这个简单,讲备库上的bat文件删除,设置好用户和权限后就可以访问了。
最后还有话要说,操作的时候由于命令行用户名输入错误,在提示输入口令时ctrl+c强退了,导致镜像库锁定,出现“Failed to get lock on destination repos currently held by 。。。”的错误,使用下面的命令处理错误。
svn propdel svn:sync-lock --revprop -r0 https://10.204.3.11/svn/lsgl/