Windows Subversion Maintenance Scripts

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!

create_repo.bat

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

dump_all.bat

@echo off
 
SET DUMP_DIR=dumps
SET REPOS_DIR=repos
 
IF NOT EXIST %DUMP_DIR% (
echo Creating directory %DUMP_DIR%
md %DUMP_DIR%
)
 
echo Dumping...
 
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.

restore_all.bat

@echo off
 
SET DUMP_DIR=dumps
SET REPOS_DIR=repos
 
echo Restoring....
 
REM %%~ni becomes the dump name without .dump
 
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.

About Geoff Lane

I’m Geoff Lane and I write Zorched.net as I figure things out about software development in the hopes that it can help other people facing similar situations. Also as a thanks to the larger web community for all of the information and knowledge that they have shared. I’ve been a professional software developer since 1999 working with a variety of different technologies. I’ve worked for startups in the Silicon Valley and Chicago, IL and now work as a consultant building custom applications for clients.
This entry was posted in Automation and tagged , , . Bookmark the permalink.

3 Responses to Windows Subversion Maintenance Scripts

  1. Grant Rettke says:

    Writing DOS Batch files is fun for the first 10 minutes, but you should have just done it with Perl :).

  2. Geoff Lane says:

    Ahh yes Grant, Perl would have worked. (I would have done it in Ruby probably.) But the whole endeavor was about not having to install extraneous stuff on the server.

  3. Grant Rettke says:

    > the whole endeavor was about not having to install extraneous
    > stuff on the server

    In that case, a Scheme->DOS-BATCH compiler seems more than appropriate here.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code lang="" line="" escaped=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>