Saturday, June 16, 2007

Double System State Backup

We have a scheduled System State backup running on our Web Server.

To save the extra cost of software and hardware backups, I wrote a script that copies the System State backup to folders named after the date the backup was performed.

With this, it's easier for us to restore backups for particular days without having too much hassels.

Code Snippet:

'---------------------------------------------
' System State Backup Configuration
' Logfile naming, Source and Destination folders
'---------------------------------------------

  1. Const ForReading = 1, ForWriting = 2, ForAppending = 8
  2. Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
  3. Dim SysBakName, CurDate, SysBakSrc, SysBakRoot, mssg
  4. Dim hTime, mTime, sTime, nTime
  5. 'System State Backup Filename
  6. SysBakName = ""
  7. 'System State Backup Source Path
  8. SysBakSrc = ""
  9. 'System State Backup Destination Root
  10. SysBakRoot = ""
  11. 'System State Log File
  12. SysBakLog = ""
  13. hTime = Hour(Time)
  14. mTime = Minute(Time)
  15. sTime = Second(Time)
  16. if hTime < 10 then
  17. hTime = "0" & hTime
  18. end if
  19. if hTime = "00" then
  20. hTime = "24"
  21. end if
  22. if mTime < 10 then
  23. mTime = "0" & mTime
  24. end if
  25. if sTime < 10 then
  26. sTime = "0" & sTime
  27. end if
  28. nTime = hrs & hTime & ":" & mTime & ":" & sTime


'----------------------------------------
' System State Backup Modules
'----------------------------------------


  1. Sub BackDest(CurDate)
  2. Select Case CurDate
  3. Case "MON"
  4. BakDestination = "Monday"
  5. Case "TUE"
  6. BakDestination = "Tuesday"
  7. Case "WED"
  8. BakDestination = "Wednesday"
  9. Case "THU"
  10. BakDestination = "Thursday"
  11. Case "FRI"
  12. BakDestination = "Friday"
  13. Case "SAT"
  14. BakDestination = "Saturday"
  15. Case "SUN"
  16. BakDestination = "Sunday"
  17. Case Else
  18. mssg = "Unable to determine backup destination path!"
  19. WriteMssg mssg
  20. End Select
  21. CopyBak BakDestination
  22. End Sub

Start the backup process:

  1. Sub StartBackup()
  2. CurDate = (FormatDateTime(Date(),1))
  3. CurDate = UCase(Trim(CurDate))
  4. CurDate = Left(CurDate, "3")
  5. BackDest CurDate
  6. End Sub
  7. Sub CopyBak(BakDestination)
  8. Dim filesys, SysBakFile, SysBakSrcFull
  9. SysBakSrcFull = SysBakSrc & "\" & SysBakName
  10. Set filesys = CreateObject("Scripting.FileSystemObject")
  11. Set SysBakFile = filesys.GetFile(SysBakSrcFull)
  12. BakDestination = SysBakRoot & "\" & BakDestination &amp; "\"
  13. SysBakFile.Copy(BakDestination)
  14. mssg = SysBakSrcFull & " copied to " & _
  15. BakDestination & SysBakName &amp;amp;amp;amp;amp; _
  16. " - " & nTime
  17. WriteMssg mssg
  18. End Sub
Write the process into a log file:
  1. Sub WriteMssg(mssg)
  2. Dim fso, f, ts
  3. Set fso = CreateObject("Scripting.FileSystemObject")
  4. If Not fso.FileExists(SysBaklog) then
  5. fso.CreateTextFile SysBakLog
  6. End If
  7. Set f = fso.GetFile(SysBakLog)
  8. Set ts = f.OpenAsTextStream(ForAppending, TristateUseDefault)
  9. ts.WriteLine mssg
  10. ts.Close
  11. 'mssg = ""
  12. End Sub
  13. Sub StartLog()
  14. Dim TimeNow
  15. TimeNow = (FormatDateTime(Date(),1))
  16. mssg = "System State Backup started on " & _
  17. TimeNow & " - " & nTime
  18. WriteMssg mssg
  19. End Sub

If at first you fail, call it version 1.0

No comments: