Click here to Skip to main content
12,760,703 members (32,440 online)
Click here to Skip to main content
Add your own
alternative version

Stats

26.6K views
685 downloads
23 bookmarked
Posted 16 Jun 2009

Using Powershell to Backup your SVN Repositories

, 23 Jun 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
How to automate backing up Subversion repositories using Powershell

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)

Share

About the Author

TexasMensch
Software Developer RocketDillo
United States United States
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 web development. The lime green text burned into my eyeballs may never go away.

You may also be interested in...

Pro
Pro

Comments and Discussions

 
QuestionCould you please share some powershell script for incremental backup of SVN Repositories? Pin
Member 1275128121-Sep-16 3:54
memberMember 1275128121-Sep-16 3:54 
AnswerRe: Could you please share some powershell script for incremental backup of SVN Repositories? Pin
TexasMensch24-Sep-16 17:55
professionalTexasMensch24-Sep-16 17:55 
SuggestionIf Svn folders are readonly - will not work. But in general - great job. Thanks. Pin
Dmitry Barovik19-May-13 21:26
memberDmitry Barovik19-May-13 21:26 
GeneralMy vote of 5 Pin
GonzaloContento13-Dec-12 7:01
memberGonzaloContento13-Dec-12 7:01 
GeneralSynchronous ZIP Pin
Member 63120920-Sep-09 21:52
memberMember 63120920-Sep-09 21:52 
GeneralThank You Pin
maxwilson844-Aug-09 10:33
membermaxwilson844-Aug-09 10:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170217.1 | Last Updated 23 Jun 2009
Article Copyright 2009 by TexasMensch
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid