Sunday, April 19, 2009

SVN Automated repeated backup / dump – Windows / Linux

Every project using SVN should have an automated backup system in place for their SVN repository. We decided we wanted a weekly incremental backup. But we also wanted to easily create a new initial backup. I scripted it on Windows and Linux with the Shell/Batch scripts shown below.

For both scripts, the same methods is used: we keep track of the last backed up revision in a file called svn_backup_delta.txt. Every time the script is started, an incremental dump is exported and archived, starting from the last dumped revision till the latest available revision. If the latest revision is the same as the last dumped revision, nothing will happen.

If the svn_backup_delta.txt doesn’t exist or the revision could not be read correctly, a complete initial dump from revision 0 till the latest revision will be exported and archived. The scripts are called daily or weekly with a standard OS scheduler. When we want to have a new initial complete dump, we simply remove the svn_backup_delta.txt file.

The scripts can be a good starting point to create your own automated SVN dumping mechanism.

Windows backup_svn.bat Batch script. This script now expects the svn.exe and svnrdump.exe to be in the same folder as the script. It also requires a repos.txt and files in the same folder. The output of the script will be stored in the same folder within the file backup_svn.log.

@echo off 
  TITLE %~n0 - see log %~n0.log 
  echo All details will be logged into %~n0.log 
  CALL :STARTBATCH >> %~dp0%~n0.log 2>&1 

:STARTBATCH     REM All properties are set in     REM Variables that change in a for loop, should be marked with !VARIABLE! instead of %VARIABLE%     REM Necessary to use this, otherwise SET !VARIABLE! in for loop fails     setlocal enabledelayedexpansion     ::CONFIGURATION     ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% CHECKING FOR CHANGES AT %DATE%: %TIME%     REM Get properties from and set as variables     FOR /F "tokens=1,2 delims==" %%A IN ( DO (     echo %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% %%A: %%B     set %%A=%%B     ) 

REM Loop through all repos 
  for /f "delims=," %%r in (%~dp0repos.txt) DO ( 
  ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% Starting SVN backup 
  ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% Using following configuration: 
  ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% -Repository URL: "%REPO_BASE%/%%r" 
  SET LAST_DELTA_FILE=%DUMP_BASE%\%%r\svn_backup_delta_%%r.txt 
  ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% -File to keep track of last revision backed up: !LAST_DELTA_FILE! 
  SET Today=%Date: =0% 
  SET Today=!Today:~-4!%Date:~-7,2%%Date:~-10,2% 
  SET Now=%Time: =0% 
  FOR /F "tokens=1,2 delims=:.," %%A IN ("!Now!") DO SET Now=%%A%%B

  %~dp0svn --username %SVN_USERNAME% --password %SVN_PASS% --non-interactive --trust-server-cert --no-auth-cache info !REPO_URL! > %TEMP%\repoinfo.tmp

  for /f "usebackq tokens=1,2 delims=: " %%i in (`find "Revision: " "%TEMP%\repoinfo.tmp"`) do (     SET LAST_REV=%%j     )     REM Check if !LAST_REV! retrieved of SVN is a number. If not, exit script.     echo !LAST_REV!| findstr /r "^[0-9]*$">nul || ( ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% ERROR: Last revision number is !LAST_REV! of %%r doesn't seem to be valid, aborting.     GOTO :END )     ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% Last revision: !LAST_REV!     ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% Last delta file: !LAST_DELTA_FILE!     REM Check if full backup exists, if true, do an incremental backup     IF NOT EXIST !LAST_DELTA_FILE! (         ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% Not delta file found, processing full export         call :FULL     ) ELSE (         ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% Delta found, processing delta         call :DELTA     ) 
  ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% All repo's done, backup finalized 

REM Do an incremental backup 
  :DELTA     ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% DELTA     SET LAST_DELTA=     for /f "usebackq tokens=1,2 delims=: " %%i in (`find "Revision: " "!LAST_DELTA_FILE!"`) do (         SET LAST_DELTA=%%j     )     ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% Last delta:!LAST_DELTA!     ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% Last revision:!LAST_REV!     IF "!LAST_DELTA!"=="" GOTO FULL     IF "!LAST_REV!"=="" GOTO :END     IF "!LAST_DELTA!"=="!LAST_REV!" GOTO :END     REM Check if !LAST_DELTA! is a number. If not, exit script.         echo !LAST_DELTA!| findstr /r "^[1-9][0-9]*$">nul || ( ECHO ERROR: DELTA REVISION NUMBER IN !LAST_DELTA_FILE! OF %REPO_URL% DOESN'T SEEM TO BE A NUMBER... ABORTING BACKUP...     GOTO :END )     SET /A INCRE_LAST_DELTA=LAST_DELTA+1     SET DUMP_FILE_NAME=!Today!-!Now!-INCREMENTAL-R!INCRE_LAST_DELTA!-TO-R!LAST_REV!.dump     SET DUMP_FILE=!DUMP_FILE!\!DUMP_FILE_NAME!     ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% Creating dump file name: !DUMP_FILE_NAME!, !DUMP_FILE!     %~dp0svnrdump --username %SVN_USERNAME% --password %SVN_PASS% --non-interactive --trust-server-cert --no-auth-cache dump !REPO_URL! -r !INCRE_LAST_DELTA!:!LAST_REV! --incremental > !DUMP_FILE!     echo %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% %~dp0%ZIP% a -tzip  !DUMP_FILE!.zip !DUMP_FILE! -v%MAX_SIZE%     call %~dp0%ZIP% a -tzip  !DUMP_FILE!.zip !DUMP_FILE! -v%MAX_SIZE%     IF EXIST !DUMP_FILE!.zip DEL /F !DUMP_FILE!     IF EXIST !DUMP_FILE!.zip.* DEL /F !DUMP_FILE!     ECHO Revision: !LAST_REV! > !LAST_DELTA_FILE!     IF EXIST "!LAST_DELTA_FILE!" GOTO DELTA     GOTO :END

REM Do a full backup 
  :FULL     ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% FULL     ECHO FULL > !LAST_DELTA_FILE!     SET DUMP_FILE_NAME=!Today!-!Now!-INITIAL-R!LAST_REV!.dump     SET DUMP_FILE=!DUMP_FILE!\!DUMP_FILE_NAME!     ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% Dump file name: !DUMP_FILE_NAME!     ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% Dump file: !DUMP_FILE!     %~dp0svnrdump --username %SVN_USERNAME% --password %SVN_PASS% --non-interactive --trust-server-cert --no-auth-cache dump !REPO_URL! -r 0:!LAST_REV! > !DUMP_FILE!     echo %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% %~dp0%ZIP% a -tzip !DUMP_FILE!.zip !DUMP_FILE! -v%MAX_SIZE%     call %~dp0%ZIP% a -tzip !DUMP_FILE!.zip !DUMP_FILE! -v%MAX_SIZE%     IF EXIST !DUMP_FILE!.zip DEL /F !DUMP_FILE!     IF EXIST !DUMP_FILE!.zip.* DEL /F !DUMP_FILE!     ECHO Revision: !LAST_REV! > !LAST_DELTA_FILE!     IF EXIST "!LAST_DELTA_FILE!" GOTO DELTA     GOTO :END

:END     ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% END     GOTO :EOF

Linux Shell script:

date=$(date +%Y%m%d)
LAST_REV=$(svn info $REPO_URL |grep Revision |gawk ' { print $2}')
#Both files MUST exist, otherwise, wer'e going to init new "delting"
if [ -e $LAST_DELTA_FILE ] ; then
if [ $LAST_DELTA -ne $LAST_REV ] ; then
svnadmin dump $REPO_PATH -r $((LAST_DELTA +1)):$LAST_REV --incremental --deltas 2>>$LOG |gzip -c > $DUMP_FILE
echo nothing to do : last revision number is same as last delta number, run it again later !
exit 0
svnadmin dump $REPO_PATH 2>>$LOG | gzip -c > $DUMP_FILE
#Is all ok ?
if [ $? -eq 0 ] ; then
echo "ERROR :" $date "Problem occured while saving dump" >>$LOG

Update 02/07/2010: Script tested and working in Windows 7 (in combination with Visual SVN server). Make sure to add the bin folder of Visual SVN server to your 'path' system variable to make the 'svn' command available system wide. Run the script with right-click 'Run as administrator'.


Update 27/06/2016 on Windows script to use a backup_properties.txt, repo.txt files and validate the retrieved revision and delta numbers before executing the windows backup

1 comment:

  1. removed line that removed the dump file in shell script, it was debug code