Tuesday, May 19, 2009

Automatic SVN Updates for Offsite Backup

SubversionMy company has something of an offsite backup strategy. We have two external hard drives which are manually swapped out on a daily basis and someone takes the inactive one home.

We're a pretty small company and that works fine. Of course, while said person is in the office, both drives (the current drive and the offsite backup) are onsite. We also have a data center where we send our really important stuff too.

I've been doing my part to help in disaster recovery by keeping my own copy of the company SVN repository on an external drive at my house. The annoying thing was that I was always attaching the drive to my laptop and copying it over manually.

That process grew irritating so I installed TortoiseSVN on my home server and now I can just update the SVN folder and our other offsite backup is up to date. But then, I got a little tired of doing that too!

I decided to automate the entire process. First, I needed to connect to the office VPN (which is an OpenVPN server). Then, I needed to update the two repositories. Finally, I needed to disconnect from the office VPN.

The OpenVPN was really the hard part because if I opened a connection on the command line, I couldn't close the connection without sending an F4 keypress. I didn't want to write a windows app for this 'cause I should really be able to do this with a batch file.

Turns out, OpenVPN has a windows service and I can start those command line, so here's my script:
@echo off

echo net start "OpenVPN Service" >> %esg%\tempGetESGSVN.bat
echo ipconfig /flushdns >> %esg%\tempGetESGSVN.bat
echo svn up %esg%\Contractor >> %esg%\tempGetESGSVN.bat
echo svn up %esg%\Internal >> %esg%\tempGetESGSVN.bat
echo net stop "OpenVPN Service" >> %esg%\tempGetESGSVN.bat
echo del %esg%\tempGetESGSVN.bat /F >> %esg%\tempGetESGSVN.bat

%esg%\tempGetESGSVN.bat >> %esg%\GetESGSVN.log

@echo on

This batch file looks a little funny and that's because it is. Basically, it's creating another batch file which executes all of the steps and then deletes itself. I did this 'cause it made it a little easier for me to log. The batch file it creates looks like this:
net start "OpenVPN Service"
ipconfig /flushdns
svn up %esg%\Contractor
svn up %esg%\Internal
net stop "OpenVPN Service"
del %esg%\tempGetESGSVN.bat /F

Nota bene, if you don't have a command line SVN client installed, you can use Slik Subversion.

No comments:

Post a Comment