Click here to Skip to main content
6,629,885 members and growing! (22,059 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Windows Powershell » General     Intermediate License: The Code Project Open License (CPOL)

Using Powershell to Backup your SVN Repositories

By TexasMensch

How to automate backing up Subversion repositories using Powershell
Windows, PowerShell, Dev, SysAdmin
Version:9 (See All)
Posted:16 Jun 2009
Updated:23 Jun 2009
Views:3,831
Bookmarked:19 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
2 votes for this article.
Popularity: 1.20 Rating: 4.00 out of 5

1

2

3
2 votes, 100.0%
4

5

Introduction

Backing up your SVN repositories is a good idea. Automating it using Powershell and svnadmin is even better.

Background

I have been using Subversion as my main source control tool for a while. It has worked out very well for me. During this time, I have never lost anything that I wasn't intentionally trying to lose. But, it is always a good practice to back up “early and often”. I find that if I don't automate a process like backing up my repositories, I tend to forget about it until it’s too late.

With that in mind, I created a Powershell script that I could schedule to run daily and make sure this is taken care of. I decided to use Powershell because of the power it brings to the Windows environment. As a .NET developer, I like its ability to use the .NET Framework. I am by no means a Powershell Guru, but I was able to get this going by piecing together different examples from the Microsoft website.

Using the Code

My assumptions with this article are that you are already familiar with Powershell and with Subversion. If not, a good starting point for Powershell is here. Information about Subversion is located here.

Before you can use this script, you will need to have access to svnadmin.exe. We will use svnadmin’s “hotcopy” function to backup the SVN repositories. I use TortoiseSVN and it does not include svnadmin. I was able to get it from the latest version of SlikSVN.

What this script does is it will look in a directory that you set and it will loop through each directory under it making the assumption that it is a repository. It will run svnadmin to copy the repository a temp directory. Then it will zip the copy and add a timestamp to the filename and save it in the backup directory that you will also set. It will also remove any older backup files based on the number of days since it was created.

The Script

Download the script and take a look at it. The first thing to notice is that all of the functions have to be at the top of the script file. Scroll down to where the logic will begin executing. The things that gave me the most trouble were getting Powershell to execute svnadmin and zipping a directory. It turns out that the “&” and placing everything into string variables was important to running an executable with parameters from within Powershell. The zip function is akin to using the send to compressed folder function in Windows explorer.

Set up Variables

Located at the top of the script file find and modify these variables to get this to work for your environment.

$RepositoryPath = "\\SVN\Repositories"
$RepoBackupPath = \\SVN\Backups\ 
$svnAdminexe = "C:\Utils\SlikSvn\bin\svnadmin" 
$DaysToKeepBackups = 14 

In the interest of using “configuration over convention”, the $RepositoryPath variable is the location of the repositories you want to backup. All of your repositories must be in this directory for this script to back them up.

The $RepoBackupPath variable is the location where all of the zipped files will be saved. They will all be in separate directories based on repository name.

$svnAdminexe is self explanatory. It’s the location of svnadmin.exe.

$DaysToKeepBackups is also self explanatory. This script will delete any backup files with a create date older than the number in days set here unless you set it to 0, then nothing is removed.

Trouble Running the Script

If you have trouble getting it to run and you see this message “The execution of scripts is disabled on this system. Please see "Get-Help about_signing" for more details.” Check out this page about signing your scripts.

Update

Some my repositories were starting to get to the point where the zip function was taking more than a couple of seconds to complete. Since I am using Windows task scheduler to run this, my script was ending before the last zip completed which aborted the zip.

The problem is a result of the .copyHere method being asynchronous and it was not having enough time to complete before the task ended.

$_zipName.copyHere($_dirToZip)

One work around for this is to sleep for a certain amount of time guessing how long the longest zip will take. I decide to also check to see if all the files I am compressing had been added to the zip. Since I am sending a directory to the zip file, the count when done will be one. The following code will wait until the zip file has at least one item:

do {
     $zipCount = $_zipName.Items().count
     "Waiting for compression to complete ..." 
     Start-sleep -Seconds 2
   }
While($_zipName.Items().count -lt 1) 

I added a wait of 2 seconds between loops to give it some extra time to finish. I also added a 5 second wait at the end of the script file because even after doing this workaround, the task was still ending too quickly to let the compression complete.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

TexasMensch


Member
I have been developing software since 1991. Most of my early years were using COBOL on a mainframe. Fortunately, several years back I made the switch to .NET using both VB and C#. The lime green text burned into my eyeballs may never go away.
Occupation: Software Developer
Company: TBAE
Location: United States United States

Other popular Windows Powershell articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 2 of 2 (Total in Forum: 2) (Refresh)FirstPrevNext
GeneralSynchronous ZIP PinmemberMember 63120921:52 20 Sep '09  
GeneralThank You Pinmembermaxwilson8410:33 4 Aug '09  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 23 Jun 2009
Editor: Deeksha Shenoy
Copyright 2009 by TexasMensch
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project