SVN备份及恢复(全量备份,增量备份及恢复)

     前段时间部门把版本控制工具由VSS换成了SVN,最近我抽空收集整理了一些SVN备份的资料,感觉比VSS备份麻烦了点,主要是一些DOS命令。老实说,网上可以直接用的SVN备份脚本,貌似真的不多,或者是我没找到。脚本其实不难,关键是像我这样的菜鸟之前没怎么接触过dos命令,所以当自己动手去写的时候,还是遇到了不少问题,特别是调试的时候,着实费了一番周折。为了像我这样的新手少走弯路,我把自己的代码贴出来,欢迎新手参考更欢迎老鸟批评指正。   

总共2个备份脚本,一个是全量备份脚本,一个是增量备份脚本。脚本如下:

1. FullVersionBackup.bat

ContractedBlock.gif ExpandedBlockStart.gif FullVersionBackup
 
     
rem VisualSVN 根目录
set SVN_HOME = " C:\Program Files (x86)\VisualSVN Server\bin "
set SVN_ADMIN = %SVN_HOME% \ svnadmin . exe
set SVN_REPOROOT = D: \ Repositories
rem 压缩工具7Z路径
set 7Z_CMD = " D:\Bakcup\SVN_BAK "
rem 备份位置
set BACKUP_SVN_ROOT =\\ 192.168 . 0.12 \ SVN_Backup

D:
cd %SVN_REPOROOT%

rem 不同的时区date格式不一样,可能要更改
SET VAR_DATE = % DATE :~ 0 , 4 %-% DATE :~ 5 , 2 %-% DATE :~ 8 , 2 %
dir / B / AD > DIRS
IF EXIST TEMP_DIRECTORY (
DEL / Q TEMP_DIRECTORY
)

FOR / F %%I IN ( DIRS ) do @ if exist " %%I\conf\svnserve.conf " echo %%I >> TEMP_DIRECTORY


FOR / F %%i IN ( TEMP_DIRECTORY ) DO (
SET REPOS_NAME = %%i
CALL :DUMP_REPOS
)

SET REPOS_NAME =

ECHO .

ECHO Compressing dump files ...

dir / b / a:-d >> Directory

DEL / Q DIRS



FOR / F %%i IN ( Directory ) DO (
%7 Z_CMD% \ 7za a Weekly_%VAR_DATE% . 7z %%i -x!Directory
)

%7 Z_CMD% \ 7za d Weekly_%VAR_DATE% . 7z TEMP_DIRECTORY
%7 Z_CMD% \ 7za d Weekly_%VAR_DATE% . 7z DUMP_FILES
del -q Directory

xcopy Weekly_%VAR_DATE% . 7z %BACKUP_SVN_ROOT% / s / i / y

ECHO Delete dump files ...

FOR / F %%i IN ( DUMP_FILES ) DO (
DEL / Q %%i
)
DEL / Q DUMP_FILES
DEL / Q DIRS
DEL / Q TEMP_DIRECTORY
DEL / Q DUMP_FILES
DEL / Q %VAR_DATE% . bin
DEL / Q %DUMP_FILE%
DEL / Q Weekly_%VAR_DATE% . 7z

ECHO Successed .
GOTO :EOF

:: ==== Functions ====
:DUMP_REPOS
SET DUMP_FILE = %REPOS_NAME% . bin
ECHO Dumping repository ^ " %Weekly_NAME%^ " ...

%SVN_ADMIN% dump ^
" %REPOS_NAME%^ " -q > %DUMP_FILE%

IF % errorlevel % == 0 (
ECHO %DUMP_FILE% >> DUMP_FILES
) ELSE (
DEL / Q %DUMP_FILE%
ECHO Dump ^ " %REPOS_NAME%^ " failed .
)
SET DUMP_FILE =
ECHO .

2. IncrementalBackup.bat

ContractedBlock.gif ExpandedBlockStart.gif IncrementBackup
 
     
set SVN_HOME = " C:\Program Files (x86)\VisualSVN Server\bin "
set SVN_ADMIN = %SVN_HOME% \ svnadmin . exe
set SVN_LOOK = %SVN_HOME% \ svnlook . exe
set SVN_REPOROOT = D: \ Repositories
set 7Z_CMD = " D:\"
set RAR_STORE=\\192.168.0.12\SVN_Backup\svnincrementW
set Log_PATH=D:\Bakcup\incrementW

D:
cd %SVN_REPOROOT%
SET VAR_DATE=%DATE:~0,4%-%DATE:~5,2%-%DATE:~8,2%
dir /B /AD > DIRS
IF EXIST TEMP_DIRECTORY (
DEL /Q TEMP_DIRECTORY
)

FOR /F %%I IN (DIRS) do @if exist
" %%I \ conf \ svnserve . conf " echo %%I >> TEMP_DIRECTORY

FOR /F %%i IN (TEMP_DIRECTORY) DO (
SET PROJECT=%%i
CALL :Dump_Entrence
)

D:
cd D:\Repositories
del /q DIRS
del /q TEMP_DIRECTORY

:Dump_Entrence

if not exist %RAR_STORE%\%PROJECT% mkdir %RAR_STORE%\%PROJECT%

SET LOWER=0
SET UPPER=0

%SVN_LOOK% youngest %SVN_REPOROOT%\%PROJECT%> %Log_PATH%\A.TMP

@FOR /f %%D IN (%Log_PATH%\A.TMP) DO set UPPER=%%D
if %UPPER%==0 GOTO :N_EXIT

if not exist %Log_PATH%\%PROJECT%_last_revision.txt GOTO :BAKUP

@FOR /f %%C IN (%Log_PATH%\%PROJECT%_last_revision.txt) DO @set LOWER=%%C

IF %LOWER% LSS %UPPER% GOTO :BAKUP
IF %LOWER% GEQ %UPPER% GOTO :N_EXIT

:BAKUP
SET FILENAME=%PROJECT%_%LOWER%_%UPPER%
%SVN_ADMIN% dump %SVN_REPOROOT%\%PROJECT% -r %LOWER%:head --incremental >%FILENAME%.bin
IF %LOWER% gtr 0 GOTO :WRITENOTE
GOTO :COMPLETE
)
:WRITENOTE
@ECHO %date% >> %Log_PATH%\%PROJECT%_log.txt
del %Log_PATH%\A.TMP

:COMPLETE
move %FILENAME%.bin %RAR_STORE%\%PROJECT%\
del %Log_PATH%\A.TMP
@echo %UPPER% > %Log_PATH%\%PROJECT%_last_revision.txt

:N_EXIT
del %Log_PATH%\A.TMP
@CD..
@exit /B

:no_args
del %Log_PATH%\A.TMP
@ECHO ON

备份策略:每周六全量备份一次,每天增量备份、配合windows计划任务,实现很简单。

恢复


     全量备份的时候,我用7z(下载地址:http://files.cnblogs.com/wqhtiger/7za.7z)压缩了一下,恢复的时候需要解压成*.bin文件,再恢复。

先找到最近时间的FullVersionBackup,恢复了全量备份以后再恢复Incremental备份。恢复的脚本比较简单,我网上找了一个直接能用的全量备份恢复脚本,把这个脚本放到要恢复版本库的根目录下(确保根目录下没有子目录),然后直接执行恢复脚本就可以了。代码如下:

ContractedBlock.gif ExpandedBlockStart.gif RecoverFullVertion
 
     
@ECHO OFF
SET VAR_IS_EMPTY_DIR = TRUE
dir / B / AD > DIRS
FOR / F %%i IN ( DIRS ) DO (
SET VAR_IS_EMPTY_DIR = FALSE
)
DEL / Q DUMP_FILES

IF " %VAR_IS_EMPTY_DIR% " == " FALSE " (
ECHO Error : Current directory is not empty .
GOTO :EOF
)
SET VAR_IS_EMPTY_DIR =

dir / B / A-D * . bin > DUMP_FILES
IF " %errorlevel% " == " 0 " (
ECHO Finded dump file .
) ELSE (
ECHO Error : Can not find dump file .
GOTO :EOF
)

FOR / F %%i IN ( DUMP_FILES ) DO (
SET REPOS_FILE = %%i
CALL :LOAD_REPOS
)
SET REPOS_FILE =
DEL / Q DUMP_FILES
ECHO Successed .
GOTO :EOF

:: ==== Functions ====
:LOAD_REPOS
SET REPOS_NAME = %REPOS_FILE:~ 0 , - 4 %
mkdir %REPOS_NAME%
ECHO Loading repository ^ " %REPOS_NAME%^ " ...
svnadmin create %REPOS_NAME%
svnadmin load -q %
cd % \ %REPOS_NAME% < %REPOS_FILE%
IF % errorlevel % == 0 (
ECHO %DUMP_FILE% >> DUMP_FILES
) ELSE (
RMDIR / Q %REPOS_NAME%
ECHO Load ^ " %REPOS_NAME%^ " failed .
)
SET REPOS_NAME =
ECHO .

恢复Incremental备份的时候,我每次都是自己动手写脚本,手动一个一个恢复,要写批处理的话,有点小麻烦,而且恢复起来最多也只有6,7个Incremental备份,所以就没再写批处理文件(如果有人写好了类似脚本,可以共享一下)。命令格式:   svnadmin load  -q lower < upper



转载于:https://www.cnblogs.com/wqhtiger/archive/2011/04/08/2009912.html

你可能感兴趣的:(操作系统)