You are not currently logged in.  Please Log in or Register.

Backing up Subversion Repositories with Windows Script Host

Wednesday, March 28, 2007
12:57 AM

This article has been accessed 3726 times.

This article was originally posted 10/19/2006. Since then I ran into a problem with the script hanging on full backups. I made a fairly small change to the script and updated the zip file accordingly.

Wow, I just looked at the site and hadn't realized that I haven't written an article since mid August! Aprimo has been keeping me busy during the day with work for v7.5.1. I've also been keeping busy in the evenings working on the next version of the Child Support Prognosticator for Springfield Publications.

This will be a major upgrade for the Child Support Prognosticator as we migrate from VB5 to the .NET platform. As part of the upgrade I've implemented a better source control solution. Most of my source control experience has been on two platforms, Perforce and Visual SourceSafe (VSS). I never really liked Perforce all that much with its change lists and the like but it has been a few years since I last used it. I was always a big fan of VSS's integration with Visual Studio and never really had much trouble with it (I guess I've been lucky from what I've read) but it always seemed to be pretty inflexible.

We recently started using Subversion at Aprimo and I immediately fell in love with it, especially after getting Tortoise SVN installed. In fact, I like Subversion so much that I decided to implement it for Springfield Publications. Getting the repository set up was a breeze and the general administration has been pretty simple so far. All of the other version control systems that I've used in the past have used the Lock/Modify/Unlock paradigm to avoid accidentally overwriting other user's changes. I've always found that methodology to be cumbersome at best so working with a version control system that uses the Copy/Modify/Merge paradigm has been a welcome change.

One of the things that was giving me a bit of trouble at first was backing up the repository. For a while I was just backing up the folder where the repository was being stored but I didn't like doing it that way. The svnadmin utility has a nice "dump" command that provides a nicer way to do full and incremental backups but if you want to keep a running log of backup files, say a weekly full backup with daily incremental backups, you'll need to do some work. To address the problem, I wrote a WSH script that may be configured to run as a scheduled task.

The script defines a simple JavaScript Object (JSO) that can perform full or partial backups and writes status to a log file. It's not necessarily the most efficient script in that each method is responsible for creating it's own instance of the FileSystemObject but I just needed something simple. The table below defines the svn_backup JSO. The entire script may be downloaded from the link at the bottom of this page.

svn_backup JSO
Member Description
Properties
repository Identifies the location of the Subversion repository. This is set by the constructor.
targetFolder Identifies the location where the backup and log files will be saved.
lastRevFileName Identifies the name of the file that will store the number of the last revision that is backed up.
logFileName Identifies the name of the log file.
dateStamp Defines a textual representation of the date on which the script is being run. This is set by the constructor.
Methods
.ctor(repLoc) Constructor.
repLoc: location of the Subversion repository that will be backed up.
GetYoungest() Retrieves the latest version number of the Subversion repository.
ReadLastRev() Reads the file defined by lastRevFileName to retrieve the version number of the last successful backup. If the file cannot be read, this method returns 0.
DoFullBackup() Builds the command and file name that will be passed to PerformBackup() to perform a full backup.
DoIncrementalBackup() Builds the command and file name that will be passed to PerformBackup() to perform an incremental backup.
PerformBackup(cmd, dumpFileName) Executes the command built by DoFullBackup() or DoIncrementalBackup() and sends the content of StdOut to a file.
cmd: the command to execute.
dumpFileName: the full path and file name of the target file.
WriteRevFile(lastRev) Writes the specified revision number to the file defined by lastRevFileName.
lastRev: the revision number to write.
WriteLog(message) Writes text to the file defined by logFileName. A timestamp will automatically be prepended to the text.
message: the text to write to the log.

Usage

The svn_backup script (download below) is easy to use and should be simple enough to adapt to your needs. There are a number of options for running the script, all of which require creating an instance of the svn_backup jso and calling the appropriate method. The simplest way to invoke the methods is to just append the required lines to the end of the js file and execute the file by via the cscript utility.

Simple Example: cscript svn_backup.js

A more flexible alternative is to define a wsf file that contains individual jobs that may be selectively executed via the cscript utility.

svn_backup.wsf

<?xml version="1.0" encoding="utf-8" ?>
<package xmlns="http://www.sppub.com">

	<job id="FullBackup">
		<script language="JScript" src="svn_backup.js" />
		<script language="JScript">
			var bak = new SVNBackup("c:\\svn_repo");
			bak.DoFullBackup();
		</script>
	</job>
	
	<job id="IncrementalBackup">
		<script language="JScript" src="svn_backup.js" />
		<script language="JScript">
			var bak = new SVNBackup("c:\\svn_repo");
			bak.DoIncrementalBackup();
		</script>
	</job>
</package>

To execute the an individual job from the WSF file above, run the following command:

WSF Example: cscript /job:FullBackup svn_backup.wsf

By creating a WSF file and running the cscript utility with the job parameter, it is trivial to configure the script to be executed as a scheduled task to create weekly full backups and daily incremental backups.

Feel free to use and adapt the script to meet your needs. I know there are portions that could be improved upon but again, I just needed something quick and simple. I'd love to receive any feedback or improvements to the script but please, help yourself!

Download: svn_backup.zip [2K]


Rate This Article
Overall Rating:  Fair My Rating: 


Comments


Great code! One small thing, the incremental backup does not increment the revision number in last_rev.log, or is that by design?
Anonymous Poster - 12/11/2007 3:25:48 PM


A feature to build in is compression via WinZIP or 7-Zip of the dump files.

- Johan Känngård, http://johankanngard.net
Anonymous Poster - 12/11/2007 3:30:18 PM


Dave,

This is a neat bit of code. I have a suggestion though. Is there any chance of adding a job that does all repositories in a specific folder? I'm guessing that you would need to loop through the relevant folder and run the backup command for each one? Could you give me a pointer?

Cheers

Pete
Anonymous Poster - 5/15/2008 10:14:19 AM


I have made a modification to the script to enhance its functionality:
1- enable waiting for the dump commands to complete
2- creating destination folders if they are absent
3- Compress the output to rar file
I don't know how to post this, but I will add it to our blog http://nebrassw.blogspot.com/
Anonymous Poster - 7/1/2008 3:10:21 PM


Name Anonymous Poster [Login]
Comment