Subversion Maintenance Scripts

With the recent release of Subversion 1.4 I’m going through the process of updating repositories. I often like to use multiple repositories because Subversion versions the entire repository with each checkin (Atomic commits rule!). When I do that, I group all of the repositories in the same directory. It makes it really easy to configure Apache with the SVNParentPath directive. And if you’re using svn+ssh or something like that, then it’s just easier to remember where they are.

SVNParentPath Example

With this configuration it’s very easy to bring new repositories up because it doesn’t require any configuration change in Apache.

<Location /svn>
    DAV svn
    SVNParentPath s:/svn
    SVNListParentPath on
    SVNAutoversioning on
 
    # authentication
    AuthType Basic
    AuthName "Subversion Access"
    AuthUserFile s:/logins.txt
    # Access is configured in the access file.
    AuthzSVNAccessFile s:/access.txt
    Require valid-user
</Location>

The one obvious downside to having multiple repositories is managing things like backups and upgrades. You have a lot more to do than just dump and load a single repository. So to help handle that task, I created some simple shell scripts.

Dump All Repositories

This script will dump all of the SVN repositories in a given directory.

#!/bin/sh
 
# Assumes each directory is an SVN repository
# and creates a dump file for each of them.
 
for i in *; do
    svnadmin dump $i > ${i}.dump;
done;

Recreate/Load All Dump Files

This script will take all of the dump files and create new repositories and then load the dump file into them. This is good for major revision changes such as to 1.4 where they have changed some structures and improved the efficiency of storing binary files for example.

#!/bin/sh
 
# Assumes a directory full of dump files
# Creates a new SVN repository and loads the dump file into it
 
for i in *.dump; do
    # The %% syntax is a substring command in bash to strip off the 
    # last occurrence of the string that follows so Foo.dump -> Foo
    repos=${i%%.dump};
    svnadmin create $repos;
    svnadmin load $repos < $i;
done

Create New Repository Script

This is a handy one I keep around to make it easy to help enforce best practices. This creates a repository and then automatically creates the trunk/ branches/ and tags/ directories.

#!/bin/sh
 
if [ -z "$1" ]
then
    echo "Usage: $0 <repository>";
    exit 1;
fi
 
# set up directories
cur_dir=`pwd`
# use these 2 if you're running on Cygwin under Windows
# cur_cyg_dir=`pwd`
# cur_dir=`cygpath -m $cur_cyg_dir`
svn_dir=${cur_dir}/${1}
 
svnadmin create "$1"
svn -m "Create default structure." mkdir file:///${svn_dir}/trunk file:///${svn_dir}/tags file:///${svn_dir}/branches
 
echo "done"

Not that these are incredibly complex shell scripts, but they can do a lot of work while you do other things (like post to your blog). I hope they’ll help someone out there.

And just to be warned, if you’re doing this on a bunch of big repositories it can take some time!

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, Code and tagged , , , . Bookmark the permalink.

5 Responses to Subversion Maintenance Scripts

  1. You may also want to note that your build integration server (CruiseControl) should be off during the upgrade because it will just slow down the process. I may wait till 1.4.1 before I make the jump.

  2. Daniel says:

    Wouldn’t your “dump all” script select files too and attempt to run the commands on them? If you are leaving all the dump files in the svn directory, then the ls would pick those up too so you’d have errors where the svnadmin command would be ‘svnadmin dump x.dump > x.dump.dump’

    Here’s a one liner that should avoid this problem:

    find * -maxdepth 0 -type d -print0 | xargs -0 -I{} sh -c 'svnadmin dump {} &gt; {}.dump'
  3. Geoff Lane says:

    Daniel,
    Thanks for another way to do it. You are right, if you left the dump files in there and re-ran the script it would complain about those. I generally run it and then move all the dumps somewhere else.

    An easy way around the problem is just to redirect the dump output to another directory:

    for i in *; do
        svnadmin dump $i > ../dumps/${i}.dump;
    done;
  4. Pingback: Windows Subversion Maintenance Scripts | Zorched / One Line Fix

  5. tbender says:

    Thanks for these scripts. I simply don’t understand why the creation of the recommended folder structure isn’t available as commandline arg to svnadmin create

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="" highlight=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>