Wednesday, 6 August 2008

Backing Up Visual SourceSafe with a batch script, WinZip and FTP

Obviously source code is the most important thing to any software project and I wanted to make sure that we had automated process in-place which would ensure we had an archive of weekly, off-site backups of our work. 

So I assembled a batch script like this:

@For /F "tokens=1,2,3 delims=/ " %%A in ('Date /t') do @(
    Set Month=%%B
    Set Day=%%A
    Set Year=%%C
echo Creating %filename%...
"C:\Program Files\WinZip\wzzip.exe" -p -r "C:\VSS_BACKUP_ZIPS\%filename%" "c:\VSS_BACKUP\*.*"
echo Done


set SITEBACKUPFILE=%filename%

REM set /p FTPUSERNAME=Enter FTP User Name:
REM set /p FTPPASSWORD=Enter FTP Password:

set FTPUSERNAME=myusername
set FTPPASSWORD=mypassword

> script.ftp USER
>>script.ftp ECHO %FTPUSERNAME%
>>script.ftp ECHO %FTPPASSWORD%
>>script.ftp ECHO binary
>>script.ftp ECHO prompt n
:: Use put instead of get to upload the file
>>script.ftp ECHO CD VSSBackups
>>script.ftp ECHO put %SITEBACKUPFILE%
>>script.ftp ECHO bye
FTP -v -s:script.ftp %FTPADDRESS%
TYPE NUL >script.ftp
DEL script.ftp

This script depends on the following:

  • Visual SourceSafe is configured to maintain a Shadow directory, which contains all the latest files.
  • WinZip and the WinZip Command Line Add-on (although I'm sure this can easily be replaced by something free)
  • Microsoft's FTP.exe program in Windows\System32.

It does the following:-

  1. It parses out the various components of the current date into variables: month, day and year. 
  2. Creates the zip of the VSS shadow folder, with a filename containing today's date in the format 'yyyyMMdd'
  3. Generates a temporary FTP command file called 'script.ftp'.  This commands the FTP program to upload the zip file.
  4. Invokes FTP.exe to process the command file, thus uploads the zip off-site.


Once I'd tested it, I used the Windows Task Scheduler to invoke it every Sunday night at 1am.

Now we have peace of mind.