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 backup.properties 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 GOTO :EOF :STARTBATCH REM All properties are set in backup.properties 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 backup.properties and set as variables FOR /F "tokens=1,2 delims==" %%A IN (%~dp0backup.properties) DO ( echo %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% %%A: %%B set %%A=%%B ) :END_CONFIGURATION REM Loop through all repos for /f "delims=," %%r in (%~dp0repos.txt) DO ( echo. 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: SET REPO_URL=%REPO_BASE%/%%r 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 DUMP_FILE=%DUMP_BASE%\%%r 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 IF NOT EXIST "!DUMP_FILE!" mkdir "!DUMP_FILE!" %~dp0svn --username %SVN_USERNAME% --password %SVN_PASS% --non-interactive --trust-server-cert --no-auth-cache info !REPO_URL! > %TEMP%\repoinfo.tmp SET LAST_REV= 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 GOTO :END 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 svnbackup.sh Shell script:
#!/bin/sh#SETUPREPO_URL=https://url:8443/base/projectREPO_PATH=c:/repository/projectDUMP_DIR=/home/user/dumpsLAST_DELTA_FILE=svn_backup_delta.txt#BEGINING OF THE SCRIPT ITSELF
date=$(date +%Y%m%d)LOG=dump-$date.log
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 ] ; thenLAST_DELTA=$(cat $LAST_DELTA_FILE)if [ $LAST_DELTA -ne $LAST_REV ] ; thenDUMP_FILE_NAME=$date-INCREMENTAL-R$((LAST_DELTA+1))-TO-R$LAST_REV.dump.gzDUMP_FILE=$DUMP_DIR/$DUMP_FILE_NAMEsvnadmin dump $REPO_PATH -r $((LAST_DELTA +1)):$LAST_REV --incremental --deltas 2>>$LOG |gzip -c > $DUMP_FILEelse
echo nothing to do : last revision number is same as last delta number, run it again later !exit 0
fielse
DUMP_FILE_NAME=$date-INITIAL-R$LAST_REV.dump.gz
DUMP_FILE=$DUMP_DIR/$DUMP_FILE_NAMEsvnadmin dump $REPO_PATH 2>>$LOG | gzip -c > $DUMP_FILEfi#Is all ok ?
if [ $? -eq 0 ] ; thenecho -n $LAST_REV > $LAST_DELTA_FILEelse
echo "ERROR :" $date "Problem occured while saving dump" >>$LOGfi
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
removed line that removed the dump file in shell script, it was debug code
ReplyDelete