Subversion backup of multiple repositories via DOS .bat file

I recently had to implement Subversion at work to manage our source code. Part of that implementation was coming up with a way to automate the backup process. Since developers could be accessing code repositories at any time there is a special command (called hotcopy) in Subversion for copying a repository to another location, which you can then backup to disk, tape, etc.

I started out just doing a simple DOS .bat file to run the hotcopy. My plan was to schedule this to run in the Windows scheduler prior to the nightly backup. As I started working on the script though I found myself trying to improve it to be as generic and hands off as possible. I also wanted to backup multiple repositories.

Here are some “features” of the script:

  • It will hotcopy multiple repositories in a specified directory.
  • It can be run attended or unattended
  • It has some basic log functionality

Here is a look at the actual hotcopy command in the .bat file:
ECHO Starting SVN backup for %%G... >> %repolog% & ^
svnadmin hotcopy %repodir%%%G %repodirhot%%%G --clean-logs >> %repolog% & ^

Download it here and rename it from svn_backup.txt to svn_backup.bat.

10 thoughts on “Subversion backup of multiple repositories via DOS .bat file

  1. Thanks, the script got me started off.

    The date naming of the log file is wrong on my computer, probably due to different local date format.

    An addition to the script could be to concider keeping the previous hotcopy while producing a new one (hot copy history). Worst case… system crashes while making hotcopy…EVIL…

  2. Thanks for the script Chris. Anders had a good suggestion about keeping the previous while making the copy. I modified the script to keep the last n backups by appending the time stamp to the hotcopy name, mapping the drive I was copying to, and using (example for deleting backups older than 8 days and mapping backup folder to R:):
    NET USE R: /delete
    SET repodirhot=%repodirhot:~0,-1%
    NET USE R: %repodirhot%
    FORFILES /P R:\ /D -8 /C “cmd /c IF @isdir==TRUE RMDIR /S /Q @path” >> %repolog% 2>&1
    NET USE R: /delete

  3. thanks a lot for the batch file! It has been extremely helpful.

    I got it backing up to an Amazon S3 bucket in no time.

  4. Thanks for the script.

    Can someone suggest how to backup to a cloud or a network folder. Also, if one could backup specific repositories instead of all the folders.

    Thanks again.

  5. Great script! I had a very similar one working on MacOS shell, but (more ellaborate) Windows batches are a bit new to me. I also had to tweak the filename a bit because of date locales.
    I added a pass to zip the hotcopy and rename it, so no worries to overwrite the current hotcopy:

    SET zipdir=D:\
    SET WinRAR=C:\Progra~1\WinRAR\WinRAR.exe
    %WinRAR% a -ibck %repodirhot%%%G >> %repolog% & ^

    I’ll add the step suggested by Eric Douglas, to send to a network map.

  6. Dear Sir,
    I would like to take the backup of all repositories, which hosted on Visual SVN Server using your batch file but it is not at all working for me. I am using tortoise SVN as client.
    :: =================== COPYRIGHT =========================================
    :: File: svn_backup.bat
    :: Author: Christopher C. Simmons (CCS)
    :: Date: 05.01.2008
    :: Purpose: To make backups (“hot copies”) of multiple SVN repos
    :: History: 0.1 Initial Release
    :: Assumes: Your path contains: C:\Program Files\Subversion\bin
    :: Your repodir contains only repos
    :: Copyright: 2008
    :: NOTICE!!!
    ::, LLC supplies this software AS IS and makes no guarantees
    :: for your use of it., LLC is not responsible for any damage
    :: or pain the use of this product may cause you. Please give credit if
    :: you use this or create a derivative work.
    :: =================== COPYRIGHT =========================================

    :: =================== CONFIG ============================================
    :: Path of the dir containing your repos [Note Trailing slash]
    SET repodir=C:\code-repo\
    :: Path of the dir in which to create you hotcopies [Note Trailing slash]
    SET repodirhot=c:\svnrepos_hotcopy\
    :: Path for log file [Note Trailing slash]
    SET logdir=C:\
    :: User mode – 1=Interactive | 0=NonInteractive
    SET imode=1
    :: =================== CONFIG ============================================

    :: =================== SCRIPT ============================================

    :: Make a date_time stamp like 030902_134200
    SET hh=%time:~0,2%

    :: Add a zero when this is run before 10 am.
    IF “%time:~0,1%”==” ” set hh=0%hh:~1,1%
    SET yymmdd_hhmmss=%date:~12,2%%date:~4,2%%date:~7,2%_%hh%%time:~3,2%%time:~6,2%

    :: Make a name for the log file
    SET repolog=%logdir%%yymmdd_hhmmss%repobackup.log

    :: Start log
    ECHO START %date% %time% >> %repolog%

    :: IF a hotcopy exists delete it first ELSE
    :: Create directories and svn hotcopy repos to them
    FOR /F %%G IN (‘dir /b /ad %repodir%’) DO ^
    IF EXIST %repodirhot%%%G (rmdir /S /Q %repodirhot%%%G & mkdir %repodirhot%%%G >> %repolog% & ^
    ECHO Starting SVN backup for %%G… >> %repolog% & ^
    svnadmin hotcopy %repodir%%%G %repodirhot%%%G –clean-logs >> %repolog% & ^
    IF %imode%== 1 ECHO FINISHED…%%G) ^
    ELSE (mkdir %repodirhot%%%G >> %repolog% & ^
    ECHO Starting SVN backup for %%G… >> %repolog% & ^
    svnadmin hotcopy %repodir%%%G %repodirhot%%%G –clean-logs >> %repolog% & ^
    IF %imode%== 1 ECHO FINISHED…%%G)

    :: Finish log
    ECHO END %date% %time% >> %repolog%
    :: Display log message if interactive mode otherwise EXIT
    ECHO Done… Logs available here: %repolog%
    if %imode%== 1 pause
    :: =================== SCRIPT ============================================

  7. Hi, thanks for the script.

    I modified the script to replace “svnadmin hotcopy” command by “svnadmin dump”, but I getting the next error:
    “La sintaxis del comando no es correcta.”
    I modified this line:
    svnadmin hotcopy %repodir%%%G %repodirhot%%%G –clean-logs >> %repolog% & ^
    By this:
    svnadmin dump %repodir%%%G > %repodirhot%%file_dump% >> %repolog% & ^

    I set de var file_dump: file_dump=%yyyymmdd_hhmmss%repobackup.dump
    Can Somebody helpme please…

  8. @REM “04-04-2011”
    @REM “Author: Asad Umar & Mustafa Ali”
    @REM “The Script shall be used to make backups”
    @REM “Two Levels of backup shall be ensured”
    @REM “1) SVN Dump”
    @REM “2) Zip BackUp of repository”
    @REM “Once when the confidence is developed, only one mode of backup shall be kept and rest will be removed”

    SET VISUAL_INSTALL=C:\Progra~1\Visual~1\bin\
    SET RAR_PATH=C:\Progra~1\WinRAR\

    SET REP_PATH=d:\Reposi~1\NCSSDev
    SET REP_PATH_AUTH=d:\Reposi~1\authz
    SET REP_PATH_HTPASS=d:\Reposi~1\htpasswd
    SET TERMINAL_IP=\\\SVN_Hot_Backup

    SET CURR_DATE=%date:~-4,4%%date:~-10,2%%date:~-7,2%


    SET username=svn
    SET password=svn123

    SET Commands=”D:\FTP_commands.txt”

    @REM “Create directory for today”

    cd %BACKUP_PATH%
    mkdir %BACKUP_DIR%

    %VISUAL_INSTALL%svnadmin dump %REP_PATH% > %COMP_PATH%\%NCSSDev.snv

    cd \

    %RAR_PATH%rar a %COMP_PATH%\NCSSDev.rar %REP_PATH%

    cd \


    cd \


    cd \


    cd \


    cd \

    @REM Start of Sending Files to FTP server

    cd %COMP_PATH%

    ECHO %username%> %Commands%
    ECHO %password%>> %Commands%

    ECHO binary >> %Commands%
    ECHO mkdir %BACKUP_DIR% >> %Commands%
    ECHO CD %BACKUP_DIR% >> %Commands%
    ECHO mput “*.*”>> %Commands%
    ECHO by >> %Commands%

    FTP -d -i -s:%Commands% %Server%
    IF EXIST %Commands% DEL %Commands%

    cd /

    @REM End of Sending Files to FTP server

    @REM Testing the status of the backup

    %RAR_PATH%unrar t -p- “*.rar” >> %COMP_PATH%\myfile.txt

    @REM Sending Via email the status of the backup

    Blat -install

    blat -to -subject SVN_BackUp_Status -body Please_Find_Out_the_attached_status_of_archive -attacht %COMP_PATH%\myfile.txt

    IF EXIST %COMP_PATH%\myfile.txt DEL %COMP_PATH%\myfile.txt

    cd \

    xcopy %COMP_PATH% %TERMINAL_IP% /Y

    cd \

Leave a Comment