Background
I’m using Subversion (via VisualSVN Server) for source control. I recently decided to add daily offsite backup to my setup. This was relatively easy to accomplish with 7-zip, Dropbox, and a batch file.
VisualSVN Server is a free package that makes setting up and managing Subversion on Windows really easy. It includes Subversion, an Apache web server, and a GUI management console.
Dropbox is an easy to use online file storage and synchronization service. It’s free for accounts up to 2GB.
7-zip is a free, open source zip library that can be controlled from the command line. [more]
Setup
First I signed up for a Dropbox account, and downloaded and installed their synchronization software. After setup, I opted to move the “My Dropbox” folder to C:\Dropbox so it wouldn’t be located in my user profile directory. This was easy to do – I just selected Preferences… from the Dropbox system tray icon, clicked the Move… button, and specified the new directory.
Next I downloaded and installed 7-zip. Pretty self explanatory.
After all the components were installed, I made sure that the locations of the svnadmin and 7-zip utilities were added to the PATH environment variable. Right-click on My Computer, and choose Properties… Then click the Advanced tab and the Environment Variables button. Under the System Variables list, scroll to find the Path variable, select it and click Edit. On my system, I added the follow two entries to the end of the Path variable: “C:\Program Files\VisualSVN Server\bin\;C:\Program Files\7-Zip\;” (no quotes). When finished click OK 3 times.
Batch File
Now that all the pieces are in place, here’s a simple batch file that exports a repository to a temporary folder, zips it up, copies the zip file to the Dropbox folder and cleans up after itself. The script is called svnbackup.bat and takes a single argument, which is the name of the Repository to backup.
mkdir C:\Temp\%1
svnadmin hotcopy D:\Repositories\%1 D:\Temp\%1
7z a C:\Temp\%1.zip D:\Temp\%1*
copy C:\Temp\%1.zip "C:\Dropbox\My Dropbox\Repositories\%1.zip"
del C:\Temp\%1.zip
attrib -r C:\Temp\%1\format
attrib -r C:\Temp\%1\db\format
del /q /s C:\Temp\%1\*.*
rd /q /s C:\Temp\%1
Once the zip file is dropped in the Dropbox folder, synchronization automatically kicks in and the zip file is backup up to the Dropbox servers.
Notes
A couple of things to note. On my system, Subversion is set up to keep all repositories in the D:\Repositories directory. Also, I realize I could have made a variable for the temp directory, but I was being lazy. Finally, when deleting the temporary directory, I was trying to figure out how to tell attrib to recursively walk the subdirectories to clear all of the read only flags, but I couldn’t get it to work. Since there were only two read only files in the tree, I opted to punt and make two calls.
Automation
The final step to this solution is to automate the backup of all repositories on a daily basis. I created a second script which calls the svnbackup script with the name of each repository. It’s called svnbackup_all.bat, and here’s an example of what would go in there.
call C:\Scripts\svnbackup MyRepository
call C:\Scripts\svnbackup My2ndRepository
Once this script was in place I added a Scheduled Task to run it every night – while I’m sleeping more soundly with the knowledge that my source code will not be lost in the event of a major disaster.
Resources
VisualSVN Server
7-zip
Dropbox
Chad Myers DevEx article on Repeatability which includes a nice overview of VisualSVN Server
Google Search: 7-zip command line