Using WSH to create an off-line backup of a FrontPage-enabled Web Site

As the old adage says, "Necessity is the mother of invention". With that in mind, I had a friend drop by my office the other day and ask me a question that started me on another quest for code.

What he asked me was whether there was a way where he could create an off-line backup of his web site. Of course, there are whole sections of the industry these days that are devoted to such things, but he wanted a simple way to create a backup on his home or work computer of his web site that is hosted at an ISP. Some time ago I wrote a FrontPage VBA macro for another friend that could be used to automate publishing, but only from within the FrontPage application itself. Since the FrontPage application exists as a COM object, I theorized that I could rewrite the code from the macro into a Windows Script Host (WSH) application that should do the trick. The code that you see below is the results of my little 'experiment'.

Usage Notes:

  • To use this script, you need to have a copy of FrontPage installed on the computer where you run the script.
  • In the script code you will need to update the source web site's URL, as well as the user name and password to use when opening the source web site.
  • The WSH script will create a folder under "c:\backups" that will contain a folder named for your web site, and then it will create folders underneath the web site folder that are a concatenation of the current date and time. (This allows automating or manually re-running the script several times.)

Once you have taken the above items into account, copy & paste the script code into Notepad and save it to your computer with a "*.vbs" file extension. To execute the code, just double-click the script. The script will pop-up a message box when it has finished publishing a copy of the web site to your computer.

Option Explicit

' **************************************************
'
' Declare our contants.
'
' **************************************************

Const fpPublishAddToExistingWeb = 2
Const fpPublishCopySubwebs = 4
Const fpPublishLogInTempDir = 8
Const fpPublishCopyAllFiles = 64

' **************************************************
'
' This section defines the publishing variables.
'
' **************************************************

Dim strSourceUrl, strDestinationFolder
Dim strUsername, strPassword
Dim strBackupDate, strBackupTime

strBackupDate = Cstr(Year(Date())) & _
  Right("00" & Cstr(Month(Date())),2) & _
  Right("00" & Cstr(Day(Date())),2)
strBackupTime = Right("00" & Cstr(Hour(Time())),2) & _
  Right("00" & Cstr(Minute(Time())),2) & _
  Right("00" & Cstr(Second(Time())),2)

strSourceUrl = "http://example.com/"
strDestinationFolder = "c:\Backups\" & _
  Mid(strSourceUrl,InStr(strSourceUrl,"://")+3) & _
  "\" & strBackupDate & "_" & strBackupTime

strUsername = "servera\administrator"
strPassword = "Password1"

' **************************************************
'
' This section checks to see if FrontPage is
' installed, and exits if it is not installed.
'
' **************************************************

' wait 10 seconds to "debounce" the server
WScript.Sleep 10000

' get a FrontPage Application object
Dim objFP: Set objFP = WScript.CreateObject("FrontPage.Application")

' exit if the object does not exist
If Err.Number = -2147352567 Then WScript.Quit

' **************************************************
'
' This section publishes the webs.
'
' **************************************************

' sanitize the publishing path
strDestinationFolder = CleanPath(strDestinationFolder)

' only continue the path can actually be created
If MakePath(strDestinationFolder) = True Then
  ' open the root web on the source
  objFP.Webs.Open strSourceUrl, strUsername, strPassword
  ' publish the root web to the destination
  objFP.ActiveWeb.Publish strDestinationFolder, fpPublishAddToExistingWeb + fpPublishCopySubwebs + fpPublishCopyAllFiles + fpPublishLogInTempDir
  ' close the root web
  objFP.ActiveWeb.Close
End If

' **************************************************
'
' This section cleans up and exits.
'
' **************************************************

Set objFP = Nothing
WScript.Quit

' ****************************************
'
' This function builds a path
'
' PASS:   File path to construct
' RETURN: TRUE/FALSE for success/failure
'
' ****************************************

Function MakePath(tmpText)
  On Error Resume Next
  Dim tx,ty,tz
  Dim tmpFSO
  Dim blnTempStatus
  Set tmpFSO = WScript.CreateObject("Scripting.FileSystemObject")
  blnTempStatus = True
  ty = Split(tmpText,"\")
  For tx = 0 To UBound(ty)
    tz = tz & ty(tx) & "\"
    If tmpFSO.FolderExists(tz) = False Then
      tmpFSO.CreateFolder(tz)
      If Err.Number <> 0 Then blnTempStatus = False
    End If
  Next
  MakePath = blnTempStatus
End Function

' ****************************************
'
' This function sanitizes a path for valid characters
'
' PASS:   File path to construct
' RETURN: New path
'
' ****************************************

Function CleanPath(tmpText)
  On Error Resume Next
  Const tmpValid = "\.-1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
  Dim tx,ty,tz
  For tx = 1 To Len(tmpText)
    ty = Mid(tmpText,tx,1)
    If (InStr(tmpValid,ty)>0) Or (tx=2 and ty=":") Then
      tz = tz & ty
    Else
      tz = tz & "_"
    End If
  Next
  CleanPath = tz
End Function

Happy coding!

No Comments