svn-bak-daily.bat
@echo off d: cd D:\test rem 获取系统时间的星期值 set day=%date:~11,14% if "%day%"=="周六" ( call FullVersionBackup.bat > FullVersionBackup.bat.log ) else ( call IncrementalBackup.bat > IncrementalBackup.bat.log )
FullVersionBackup.bat
@echo off echo FullVersionBackup rem VisualSVN 根目录 set SVN_HOME="C:\Program Files (x86)\Subversion\bin" set SVN_ADMIN=%SVN_HOME%\svnadmin.exe set SVN_LOOK=%SVN_HOME%\svnlook.exe rem 设置svn代码库所在的驱动器盘符 set SVN_DRV=d: set SVN_REPOROOT=%SVN_DRV%\test\svnrepo set SVN_BAKROOT=%SVN_DRV%\test\svnbak setlocal enabledelayedexpansion set bat_home=%cd% %SVN_DRV% cd %SVN_REPOROOT% 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 ) ECHO Delete temp files... DEL /Q DIRS DEL /Q TEMP_DIRECTORY DEL /Q A.TMP pause ECHO End. GOTO :EOF :DUMP_REPOS if not exist %SVN_BAKROOT%\%REPOS_NAME% ( mkdir %SVN_BAKROOT%\%REPOS_NAME% ) SET LOWER=0 set UPPER=0 %SVN_LOOK% youngest %SVN_REPOROOT%\%REPOS_NAME% > A.TMP FOR /f %%D IN (A.TMP) DO ( set UPPER=%%D ) SET DUMP_FILE=%REPOS_NAME%_%LOWER%_%UPPER%.dump rem 非空库才要备份 if %LOWER% LSS %UPPER% ( rem 有新提交才要重新全备份 if not exist %SVN_BAKROOT%\%REPOS_NAME%\%DUMP_FILE% ( ECHO Dumping repository ^"%REPOS_NAME%^" ... %SVN_ADMIN% dump ^"%REPOS_NAME%^" -r %LOWER%:%UPPER% --incremental > %SVN_BAKROOT%\%REPOS_NAME%\%DUMP_FILE% IF %errorlevel%==0 ( echo %UPPER% > %SVN_BAKROOT%\%REPOS_NAME%\%REPOS_NAME%_last_revision.txt ECHO Dump ^"%REPOS_NAME%^" successed. echo %date% %time% Dump ^"%REPOS_NAME%^" successed. >> %bat_home%\FullVersionBackup.bat.successed.log ) ELSE ( DEL /Q %SVN_BAKROOT%\%REPOS_NAME%\%DUMP_FILE% echo %date% %time% Dump ^"%REPOS_NAME%^" failed. >> %bat_home%\FullVersionBackup.bat.failed.log ECHO Dump ^"%REPOS_NAME%^" failed. ) ) )
IncrementalBackup.bat
@echo off echo IncrementalBackup rem VisualSVN 根目录 set SVN_HOME="C:\Program Files (x86)\Subversion\bin" set SVN_ADMIN=%SVN_HOME%\svnadmin.exe set SVN_LOOK=%SVN_HOME%\svnlook.exe rem 设置svn代码库所在的驱动器盘符 set SVN_DRV=d: set SVN_REPOROOT=%SVN_DRV%\test\svnrepo set SVN_BAKROOT=%SVN_DRV%\test\svnbak setlocal enabledelayedexpansion set bat_home=%cd% %SVN_DRV% cd %SVN_REPOROOT% 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 ) ECHO Delete temp files... DEL /Q DIRS DEL /Q TEMP_DIRECTORY DEL /Q A.TMP pause ECHO End. GOTO :EOF :DUMP_REPOS if not exist %SVN_BAKROOT%\%REPOS_NAME% ( mkdir %SVN_BAKROOT%\%REPOS_NAME% ) SET LOWER=0 SET UPPER=0 %SVN_LOOK% youngest %SVN_REPOROOT%\%REPOS_NAME%> A.TMP FOR /f %%D IN (A.TMP) DO ( set UPPER=%%D ) if not exist %SVN_BAKROOT%\%REPOS_NAME%\%REPOS_NAME%_last_revision.txt ( set LOWER=0 ) else ( FOR /f %%C IN (%SVN_BAKROOT%\%REPOS_NAME%\%REPOS_NAME%_last_revision.txt) DO ( set /A LOWER=%%C+1 ) ) SET DUMP_FILE=%REPOS_NAME%_%LOWER%_%UPPER%.dump rem 非空库,并且上次增量备份后有新提交的才重新增量备份 if %LOWER% LEQ %UPPER% ( rem 没有全备的则先做全备 if not exist %SVN_BAKROOT%\%REPOS_NAME%\%DUMP_FILE% ( ECHO Dumping repository ^"%REPOS_NAME%^" ... %SVN_ADMIN% dump %REPOS_NAME% -r %LOWER%:%UPPER% --incremental > %SVN_BAKROOT%\%REPOS_NAME%\%DUMP_FILE% IF %errorlevel%==0 ( echo %UPPER% > %SVN_BAKROOT%\%REPOS_NAME%\%REPOS_NAME%_last_revision.txt ECHO Dump ^"%REPOS_NAME%^" successed. echo %date% %time% Dump ^"%REPOS_NAME%^" successed. >> %bat_home%\IncrementalBackup.bat.successed.log ) ELSE ( DEL /Q %SVN_BAKROOT%\%REPOS_NAME%\%DUMP_FILE% echo %date% %time% Dump ^"%REPOS_NAME%^" failed. >> %bat_home%\IncrementalBackup.bat.failed.log ECHO Dump ^"%REPOS_NAME%^" failed. ) ) )