Monday, December 04, 2006

Automatic Directory Backups w/ VBScript - AutoBackup.vbs

Recently I've realized that I should start backing up more of my work. Especially all this web work that I have hours and hours of scripting and design into. Since manually backing up my work is a sketchy and unreliable business at best, I thought I'd write a quick script that I could just schedule in Windows Task Scheduler and have it use 7Zip to do all the work for me.

Well I already did part of the work with a script that will zip files for me via 7Zip's 7za.exe. So I just used that function and whipped up the rest of the script to backup just the directories that I'm interested in.

You could use this script to backup your "My Documents" folder, user directories out on a network share, or like I am, all of the directories in your Inetpub directory.

So here is the AutoBackup.vbs that I should have written and been using years ago:

'AutoBackup.vbs 12-04-06
'This script is provided under the Creative Commons license located
'at http://creativecommons.org/licenses/by-nc/2.5/ . It may not
'be used for commercial purposes with out the expressed written consent
'of NateRice.com


'---------------Script Configuration----------------
'sInputFile is a text file with a list of directories
'that need backed up.
sInputFile = "c:\users.txt"

'sInputDirectory is the working input directory.
'The script will backup folders listed in the
'sInputFile that are present in this directory.
sInputDirectory = "\\server\share\"

'This is the location of the backup directory. The
'zip files created durring the backup will be
'copied to this directory for storage.
sBackupDirectory = "c:\backups\"

'bIncludeDate is a boolean true/false value.
'If you'd like to include the date in the backup
'file name (so you can maintain incrimental
'backups) then set this value to true.
bIncludeDate = True
'---------------------------------------------------

Const OpenAsDefault = -2
Const FailIfNotExist = 0
Const ForReading = 1

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set fFile = oFSO.OpenTextFile(sInputFile, ForReading, FailIfNotExist, OpenAsDefault)

sResults = fFile.ReadAll
aResults = Split(sResults,vbCRLF)

For Each sDirectory In aResults
  If bIncludeDate Then
    sNow = Replace(Replace(Now(),"/","-"),":",".")
      sResults = Zip("""" & sInputDirectory & sUser & """", sBackupDirectory & sDirectory & "-" & sNow & ".zip")
  Else
      sResults = Zip("""" & sInputDirectory & sUser & """", sBackupDirectory & sDirectory & ".zip")
  End If
  'WScript.Echo sResults
Next

Function Zip(sFile,sArchiveName)
  'This script is provided under the Creative Commons license located
  'at http://creativecommons.org/licenses/by-nc/2.5/ . It may not
  'be used for commercial purposes with out the expressed written consent
  'of NateRice.com

  Set oFSO = WScript.CreateObject("Scripting.FileSystemObject")
  Set oShell = WScript.CreateObject("Wscript.Shell")

  '--------Find Working Directory--------
  aScriptFilename = Split(Wscript.ScriptFullName, "\")
  sScriptFilename = aScriptFileName(Ubound(aScriptFilename))
  sWorkingDirectory = Replace(Wscript.ScriptFullName, sScriptFilename, "")
  '--------------------------------------

  '-------Ensure we can find 7za.exe------
  If oFSO.FileExists(sWorkingDirectory & " " & "7za.exe") Then
    s7zLocation = ""
  ElseIf oFSO.FileExists("C:\Program Files\7-Zip\7za.exe") Then
    s7zLocation = "C:\Program Files\7-Zip\"
  Else
    Zip = "Error: Couldn't find 7za.exe"
    Exit Function
  End If
  '--------------------------------------

  oShell.Run """" & s7zLocation & "7za.exe"" a -tzip -y """ & sArchiveName & """ " _
  & sFile, 0, True 

  If oFSO.FileExists(sArchiveName) Then
    Zip = 1
  Else
    Zip = "Error: Archive Creation Failed."
  End If
End Function
Send this to:                          

Comments

Name
URL
Email
Email address is not published
Remember Me
Comments

CAPTCHA
Write the characters in the image above