Windows Subversion Maintenance Scripts

November 19, 2008 - 3 minute read -
subversion batch files windows

Previously I wrote about the Subversion Maintenance Scripts that I use for doing things like backups and upgrades. They were bash shell scripts that automated dealing with multiple Subversion repositories. The secret was that those guys were being run using Cygwin on Windows. Recently we got a new, more powerful server to run our Virtuals on. I decided to rewrite the scripts using native Windows Batch files so that I wouldn't have to install Cygwin again. Hopefully someone else will find this useful too.

Windows Batch Script for Creating a Default Repository Structure

I work for a custom software development consulting firm so we have a lot of different clients. We don't want to mix different clients' code so we create a repository per client. We often get multiple projects per client though (yeah, they like us) so we go ahead and create a project under the client. We also like to use the trunk, tags and branches directories by default.

With all of these rules: Automate!


@echo off</p>
<p>REM Check command line args to make sure a customer and project name were passed
IF (%2)==() GOTO CMDLINE</p>
<p>REM If svnadmin is not in your path, uncomment the following
REM PATH=%PATH%;"D:\Program Files\Subversion\bin"</p>
<p>REM Set up variables with the directories
<p>echo Creating directory in %SVN_DIR%</p>
<p>svnadmin create "%SVN_DIR%"
svn -m "Create default structure." mkdir %PROJ_DIR% %PROJ_DIR%/trunk %PROJ_DIR%/tags %PROJ_DIR%/branches
echo Usage: %0 ^<customer^> ^
echo Done

Some of the interesting pieces: %~dp0 gives you the directory that the current script lives in. %CUR_DIR:\=/% replaces the backslash (\) with forward slashes (/) so that the SVN file:// URL will work correctly.

Windows Batch Script to Dump Your Subversion Repositories

The next thing you might want to do is to dump all of your repositories so that you can back them up or upgrade SVN or move to a new server. With just one repository it's easy to do by hand. With 20+ repositories it's much nicer to run a script and then go do something else.


@echo off</p>
<p>SET DUMP_DIR=dumps
SET REPOS_DIR=repos</p>
echo Creating directory %DUMP_DIR%
<p>echo Dumping...</p>
<p>FOR /f %%i IN ('dir /B /A:D %REPOS_DIR%\*') DO (
    echo Dumping %REPOS_DIR%\%%i to %DUMP_DIR%\%%i.dump
    svnadmin dump %REPOS_DIR%\%%i > %DUMP_DIR%\%%i.dump

Some of the interesting pieces: FOR /f %%i IN ('dir /B /A:D %REPOS_DIR%\*') DO is the part that finds each directory (in this case an SVN repository). Then for each repository you run the svnadmin dump command and send the dump file to a different directory.

Windows Batch Script to Load Your Subversion Repositories

After you've dumped all of your repositories and you need to restore them, because you've upgraded or moved them to a new server we need to do the reverse of the dump script.


@echo off</p>
<p>SET DUMP_DIR=dumps
SET REPOS_DIR=repos</p>
<p>echo Restoring....</p>
<p>REM %%~ni becomes the dump name without .dump</p>
<p>FOR /f %%i IN ('dir /B %DUMP_DIR%\*.dump') DO (
    echo Restoring %DUMP_DIR%\%%i to %REPOS_DIR%\%%~ni
    svnadmin create %REPOS_DIR%\%%~ni
    svnadmin load %REPOS_DIR%\%%~ni < %DUMP_DIR%\%%i

Some of the interesting pieces: FOR in this case is the same as the dump, except it's looking for all the dump files. %%i holds the name of the dump file (e.g. repo.dump). %%~ni strips off the extension to give you just the name of the file (e.g. repo).

Now you can manage your repositories with ease if you're stuck on a Windows machine.