Friday, April 11, 2008

CMD Script to automate subversion backups

This is nothing fancy, just a .cmd script to back up my production Subversion repository on a nightly basis. There are some good ones via Google results but none that really fit my needs (too complex or too simple).

Pre-Requisites:
  1. Windows 2003 (not tested on 2000 or 2008)
  2. Subversion installed locally (tools and repository) with the \bin folder in the path

What this script does:
  1. If I call it with "FULL" it will:
    1. delete all previous subversion dumps
    2. create a new dump, using deltas, named "FULL_REV-#_YYYY-MM-DD.svndump" where # is the current revision number
  2. If I call it with "INC" it will:
    1. create a new dump incremental from the previous revision number, named "INC_REV-#_YYYY-MM-DD.svndump"
    My future enhancements will be:
    1. Store the revision number of the last successful dump, and then perform the next incremental off that instead of assuming just N-1 (in case previous night's job failed)
    2. Utilize the "ERR_FLAG" to send errors to our monitoring system.
Script Files:
Steps:
  1. Download the code
  2. Extract it anywhere you want to run it
  3. Modify the file to replace the values of "RepoPath" and "DumpPath" with the proper locations for your environment
  4. Execute either as "svn_dump INC" or "svn_dump FULL" (see above for what each does).

For reference, here's the script code:
@ECHO OFF
SETLOCAL
SET VERSION=v8.4.11 Aaron Dodd
SET USAGE=svn_dump.cmd [FULL -or- INC]

:: Settings
::===========================================================================

SET RepoPath=D:\state_management\repository
SET DumpPath=D:\state_management\repository_dumps

:: Date/Time formatting
::---------------------
FOR /F "Tokens=2" %%I in ( " %date% " ) Do Set StrDate=%%I
FOR /F "Tokens=1 delims=/ " %%M in ( " %StrDate% " ) Do Set Month=%%M
FOR /F "Tokens=2 delims=/ " %%D in ( " %StrDate% " ) Do Set Day=%%D
FOR /F "Tokens=3 delims=/ " %%L in ( " %StrDate% " ) Do Set Year=%%L
FOR /F "Tokens=1 delims=. " %%I in ( " %time% " ) Do Set StrTime=%%I
FOR /F "Tokens=1 delims=: " %%H in ( " %StrTime% " ) Do Set Hour=%%H
FOR /F "Tokens=2 delims=: " %%M in ( " %StrTime% " ) Do Set Minute=%%M
FOR /F "Tokens=3 delims=: " %%S in ( " %StrTime% " ) Do Set Second=%%S

:: Process Arguments
::------------------
IF '%1' == '' GOTO USAGE

IF "%1" EQU "FULL" SET DumpType=FULL
IF "%1" EQU "full" SET DumpType=FULL
IF "%1" EQU "INC" SET DumpType=INC
IF "%1" EQU "inc" SET DumpType=INC

IF '%DumpType%' == '' GOTO USAGE

:: Verify Dependencies
IF NOT EXIST "%RepoPath%" CALL :ERROR_PATH REPOSITORY
IF NOT EXIST "%DumpPath%" CALL :ERROR_PATH DUMP

SVNADMIN 2> NUL
IF %ErrorLevel% EQU 9009 CALL :ERROR_DEP SVNADMIN

SVNLOOK 2> NUL
IF %ErrorLevel% EQU 9009 CALL :ERROR_DEP SVNLOOK


:: MAIN
::===========================================================================
::Determine latest subversion revision number
::-------------------------------------------
FOR /F %%Y IN ('SVNLOOK youngest "%RepoPath%"') DO SET RepoVer=%%Y
SET /A RepoPriorVer=%RepoVer% - 1 >NUL

SET DumpFile=%DumpPath%\%DumpType%_REV-%RepoVer%_%Year%-%Month%-%Day%.svndump

:: Clean up old svn dumps
::-----------------------
IF "%DumpType%" EQU "FULL" DEL /Q "%DumpPath%\*.svndump" >NUL

:: Perform backup
::---------------
IF "%DumpType%" EQU "FULL" SVNADMIN dump "%RepoPath%" --deltas > %DumpFile%
IF "%DumpType%" EQU "INC" SVNADMIN dump "%RepoPath%" -r %RepoPriorVer%:%RepoVer% --incremental > %DumpFile%

GOTO END

:: Subroutines
::===========================================================================
:USAGE
ECHO %VERSION%
ECHO %USAGE%
ECHO Edit this script to change where the repository path is, and where to save the dump
GOTO END

:ERROR_DEP
ECHO !!ERROR!! - Dependency not found
IF "%1" EQU "SVNADMIN" ECHO svnadmin.exe cannot be found.
IF "%1" EQU "SVNLOOK" ECHO svnlook.exe cannot be found.
ECHO Verify Subversion is installed and the \bin folder is in the system-wide PATH environment variable.
SET ERR_FLAG=1
GOTO END

:ERROR_PATH
ECHO !!ERROR!!
IF "%1" EQU "REPOSITORY" ECHO Cannot find %RepoPath%
IF "%1" EQU "DUMP" ECHO Cannot find %DumpPath%
SET ERR_FLAG=1
GOTO END

:END

ENDLOCAL

2 comments:

Anonymous said...

here's a modification for french servers ...dates :-)

Set StrDate=%date%
FOR /F "Tokens=1 delims=/ " %%D in ( " %StrDate% " ) Do Set Day=%%D
FOR /F "Tokens=2 delims=/ " %%M in ( " %StrDate% " ) Do Set Month=%%M
FOR /F "Tokens=3 delims=/ " %%L in ( " %StrDate% " ) Do Set Year=%%L
FOR /F "Tokens=1 delims=. " %%I in ( " %time% " ) Do Set StrTime=%%I
FOR /F "Tokens=1 delims=: " %%H in ( " %StrTime% " ) Do Set Hour=%%H
FOR /F "Tokens=2 delims=: " %%M in ( " %StrTime% " ) Do Set Minute=%%M
FOR /F "Tokens=3 delims=: " %%S in ( " %StrTime% " ) Do Set Second=%%S


Thx a lot for this great script that doesn't hang unlike vbs ...

IZ

http://www.gpit.fr

Mikedopp said...

Any ideas on how to get the expected FS format to be 3 instead of 2?