SVN备份

dingyun00 2016-02-25

最近研究Windowssvn的备份机制,结合网上的文章,分享下完成的svndump的备份脚本,记录一下svnsync的使用方法。

1.svnadmindump本地备份脚本

不多说了,直接贴脚本。

@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的用法,在讲一下备份搭建过程,本文中省略部分建库和建用户的具体步骤。

svnsyncinit--trust-server-cert--source-usernameARG--source-passwordARG--sync-usernameARG--sync-passwordARGDEST_URLSOURCE_URL

svnsyncsync--trust-server-cert--non-interactive--source-usernameARG--source-passwordARG--sync-usernameARG--sync-passwordARGDEST_UR

注:如果svnsync版本不支持source-username、sync-username分别设置两个库的用户和口令,可以用--username、--password设置源库和备份库的用户和口令,但是两个库的用户和口令必须相同。

--trust-server-cert信任服务器证书(如https自签名证书)

--source-username源库用户名

--sync-username镜像库用户名

ARGDEST_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)新建一个新库

mkdird:\svn\lsgl

cdd:\svn\lsgl

svnadmincreate

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)执行命令

svnsyncinit--usernamesyncuser--passwordpasswdhttps://192.168.1.3:443/svn/lsglhttps://192.168.1.2:443/svn/lsgl

以下在源库服务器执行:

7)新建e:\svn\lsgl\hooks\post-commit.bat,实时备份数据,内容如下。

svnsync--trust-server-cert--non-interactive--usernamesyncuser--passwordpasswdsynchttps://192.168.1.3:443/svn/lsgl

至此完成。

还有些话要说,在搭建备份库的时候,新建库后直接开启同步,从r0同步至head;在网上看到新建库后,load数据再进行同步,将方法记录在下面,笔者并没有验证。

先准备源库的dump文件。

svnadmindumpe:\svn\lsgl>source-lsgl.dump

将上面的第6)步提换如下:

svnadminloadd:\svn\lsgl<source-lsgl.dump

然后手动设置镜像库的属性

svnpropset--revprop-r0svn:sync-from-uuid源库的UUID

svnpropset--revprop-r0svn:sync-last-merged-revdump文件的最新版本https://192.168.1.3:443/svn/lsgl

svnpropset--revprop-r0svn:sync-from-urlhttps://192.168.1.3:443/svn/lsgl。

还有话要说,如何切换备库:

这个简单,讲备库上的bat文件删除,设置好用户和权限后就可以访问了。

最后还有话要说,操作的时候由于命令行用户名输入错误,在提示输入口令时ctrl+c强退了,导致镜像库锁定,出现“Failedtogetlockondestinationreposcurrentlyheldby。。。”的错误,使用下面的命令处理错误。

svnpropdelsvn:sync-lock--revprop-r0https://10.204.3.11/svn/lsgl/

相关推荐

leehbhs / 0评论 2020-03-04