Saturday, December 8, 2007

Automating Backing up SharePoint Front End Web Server

In my previous blog post, titled Comprehensive SharePoint Disaster/Recovery I outline the details on what needs to be backed up in a SharePoint environment. Although the post provides all the details, I was starting to wonder if there was a way to automate backing up at least the Web Server. After a bit of tinkering, I was able to successfully backup all the data on my SharePoint Web Server. It backs up a bit more data than it needs to, but this is a full proof method that can be used in your environments. All you really need to do to the code is change the output directory and make sure the default directories are accurate.

Warning: This was a quick script that I tested in two virtual environments. If you experience problems, e-mail me and I'll do my best to respond quickly. Also, there is no error trapping in here....:/. You will have to implement that on your own.

Here is a the sample VBS script:


Option Explicit
'-------------------------------------------------------------------------------
'
' Backs up SharePoint 2007 Front End Web Servers
' Written by: Pirooz Javan
' Create Date: 08/13/2007
' Last Updated Date: 12/05/2007
'
' Description:
' This script is used for to help automate the process of
' backups for your SharePoint 2007 Front End Web Servers. To run this script
' there are serveral dependencies:
' - iisback.vbs
' -STSADM.EXE
'
'-------------------------------------------------------------------------------
'Set the default SharePoint installation path
Dim sharePointInstallationPath
sharePointInstallationPath = "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions"

'Set the path to iisback.vbs system file
Dim iisBackupTool
iisBackupTool = "C:\WINDOWS\system32\iisback.vbs"

'Set the path to default IIS log folder
Dim iisDefaultLogsPath
iisDefaultLogsPath = "C:\WINDOWS\system32\LogFiles"

'Set the path to the backup output
Dim outputPath
outputPath = "C:\SharePointBackups"

'IIS Configuration file backup names
Dim iisConfigurationBackupFile
iisConfigurationBackupFile = "IISConfigurationBackup"

'Set Path to IIS configuration backup foloder
Dim metaBackPath
metaBackPath ="C:\WINDOWS\system32\inetsrv\MetaBack"

'Set Path to GAC Assemblies foloder
Dim gacAssembliesPath
gacAssembliesPath ="C:\WINDOWS\assembly"

'Set string that will detect a SharePoint application in IIS HttpCustomHeaders
Dim strDetectSharePointApp
strDetectSharePointApp = "MicrosoftSharePointTeamServices"
'-------------------------------------------------------------------------------
Dim objShell 'Shell Object
Dim objFSO 'File System Object
Dim objXmlDoc 'XML Object
Dim objNodeList 'XML Object
Dim xNode 'XML Object
Dim objNet 'Network Object

Set objShell = CreateObject("wscript.shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objXmlDoc = CreateObject("Msxml2.DOMDocument")
Set objNet = CreateObject("WScript.NetWork")

'-------------------------------------------------------------------------------
' MAIN PROGRAM CALLS
'-------------------------------------------------------------------------------
Call BuildDirectory(outputPath)
WScript.Sleep(2000) 'Wait 2 Seconds

'Call BackupSystemState("SystemState")

Call BackupGACAssemblies("GACAssemblies")

Call BackupIISConfiguration("IISConfiguration")

' In order to run these, BackupIISConfiguration must be executed first
' so that the XML will be loaded
Call BackupDefaultLogPath("DefaultLogs")
WScript.Sleep(2000) 'Wait 2 Seconds

Call BackupDefaultSharePointFiles("SharePointFiles")
WScript.Sleep(2000) 'Wait 2 Seconds

Call BackupIISLogs("IISFiles")
WScript.Sleep(2000) 'Wait 2 Seconds

Call BackupIISWebs("IISFiles")
WScript.Sleep(2000) 'Wait 2 Seconds

Call BackupSharePointWebs("SharePointWebs")

'-------------------------------------------------------------------------------
' SUBROUTINES AND FUNCTIONS
'-------------------------------------------------------------------------------

Sub BackupSystemState(folder)
WScript.Echo "Backing up System State..."
BuildDirectory outputPath & "\" & folder

'Backups up system state and overwites the file
objShell.Run "ntbackup backup systemstate /J " & chr(34) & "Backup Job" & chr(34) & " /F " & outputPath & "\" & folder & "\SystemStateBackup.bkf /a", 0, True
End Sub

Sub BackupGACAssemblies(folder)
WScript.Echo "Backing up GAC Assemblies..."
BuildDirectory outputPath & "\" & folder

'Backups up GAC assemblies
objShell.Run "xcopy " & gacAssembliesPath & " " & outputPath & "\" & folder & " /e", 0, True
End Sub

Sub BackupIISConfiguration(folder)
WScript.Echo "Backing up IIS Configuration..."
BuildDirectory outputPath & "\" & folder

'Backups up IIS metabase configuration file (Metabase.xml) and the matching metabase schema file (MBSchema.xml)
objShell.Run "Cscript.exe " & iisBackupTool & " /backup /b " & iisConfigurationBackupFile & " /overwrite", 0, True

WScript.Sleep(3000) 'Wait 3 seconds for IIS to finish backing up
objFSO.CopyFolder metabackPath, outputPath & "\" & folder
objXmlDoc.load(getLatestIISBackupFile())
End Sub

Sub BackupDefaultLogPath(folder)
WScript.Echo "Backing up Default log folder at " & iisDefaultLogsPath
BuildDirectory outputPath & "\" & folder

'Backups up the default log folder
objFSO.CopyFolder iisDefaultLogsPath, outputPath & "\" & folder, True
End Sub

Sub BackupDefaultSharePointFiles(folder)
WScript.Echo "Backing up Default SharePoint folder at " & sharePointInstallationPath
BuildDirectory outputPath & "\" & folder

'Backups up the default SharePoint folder
objFSO.CopyFolder sharePointInstallationPath, outputPath & "\" & folder, True
End Sub

Sub BackupIISLogs(folder)
WScript.Echo "Backing up IIS Log files..."
BuildDirectory outputPath & "\" & folder
Set objNodeList = objXmlDoc.getElementsByTagName("IIsWebServer")

WScript.Echo objNodeList.length
If objNodeList.length > 0 Then
For each xNode In objNodeList
If len(xNode.getAttribute("LogFileDirectory"))>"0" Then
'Does not backup logs in default log path.
If InStr(xNode.getAttribute("LogFileDirectory"), iisDefaultLogsPath) = "0" Then
CopyFolder xNode.getAttribute("Location"), xNode.getAttribute("LogFileDirectory"), outputPath & "\" & folder, "_Logs"
End If
End If
Next
End If
End Sub

Sub BackupIISWebs(folder)
WScript.Echo "Backing up IIS Web files..."
BuildDirectory outputPath & "\" & folder
Set objNodeList = objXmlDoc.getElementsByTagName("IIsWebVirtualDir")

If objNodeList.length > 0 Then
For Each xNode In objNodeList
If InStr(xNode.getAttribute("Path"), sharePointInstallationPath) = "0" Then
CopyFolder xNode.getAttribute("Location"), xNode.getAttribute("Path"), outputPath & "\" & folder, "_Web"
End If
Next
End If
End Sub

Sub BackupSharePointWebs(folder)
WScript.Echo "Backing up SharePoint with STSADM..."

BuildDirectory outputPath & "\" & folder
Set objNodeList = objXmlDoc.getElementsByTagName("IIsWebVirtualDir")
Dim url
Dim command

If objNodeList.length > 0 Then
For Each xNode In objNodeList
If InStr(xNode.getAttribute("HttpCustomHeaders"), strDetectSharePointApp) <> "0" Then
url = getSharePointURLfromAppID(getIISApplicationID(xNode.getAttribute("Location")))
command = chr(34) & sharePointInstallationPath & "\12\BIN\STSADM.EXE" & chr(34) & " -o backup"
command = command & " -url http://" & url
command = command & " -filename " & outputPath & "\" & folder & "\" & Replace(Replace(url,".", "_"),":","_") & ".dat"
command = command & " -overwrite"
WScript.Echo "Running: " & command
objShell.Run command, 0, True
End If
Next
End If
End Sub

Sub CopyFolder(LocationID, SourcePath, DestinationPath, appendString)
WScript.Echo "got here for locationid " & LocationID
'Build IIS application folder
BuildDirectory DestinationPath & "\" & getIISApplicationID(LocationID)

'Build directory to backup to
Dim backupDirectory
backupDirectory = Mid(SourcePath, 4, Len(SourcePath)) 'Removes Drive Letter
backupDirectory = Replace(backupDirectory, "\", "_")
backupDirectory = backupDirectory & appendString

'Build path to backup To
Dim BackupPath
BackupPath = DestinationPath & "\" & getIISApplicationID(LocationID) & "\" & backupDirectory

If (Right(SourcePath, "1") = "\") Then
SourcePath = Left(SourcePath, Len(SourcePath)-1)
End if
WScript.Echo "Copying " & SourcePath & " to " & BackupPath
objFSO.CopyFolder SourcePath, BackupPath, True
End Sub

Sub BuildDirectory(path)
If (objFSO.FolderExists(path) = False) Then
objFSO.CreateFolder(path)
End If
End Sub

' Finds the latest MDX file
Function getLatestIISBackupFile()
Dim m_iisMetaFolder, m_File
Set m_iisMetaFolder = objFSO.GetFolder(metaBackPath)
Dim path
Dim latestFileDate
latestFileDate = CDate("01/01/01")

For Each m_File In m_iisMetaFolder.Files
If InStr(m_File.Name, iisConfigurationBackupFile & ".MD") Then
If (m_file.DateLastModified > latestFileDate) Then
latestFileDate = m_file.DateLastModified
path = m_file.Path
End If
End If
Next
getLatestIISBackupFile = path
End Function

Function getIISApplicationID(Location)
Location = Replace(Location, "/LM/W3SVC/", "")
If InStr(Location, "/") > "0" Then
Location = Left(Location, InStr(Location, "/")-1)
End If
getIISApplicationID = Location
End Function

Function getSharePointURLfromAppID(AppID)
Set objNodeList = objXmlDoc.getElementsByTagName("IIsWebServer")
'Assuming Server Binding Format of: ::
Dim Port
Dim HostName
Dim firstColon, secondColon

If objNodeList.length > 0 Then
For Each xNode In objNodeList
If InStr(xNode.getAttribute("Location"), AppID) <> "0" Then
firstColon = InStr(xNode.getAttribute("ServerBindings"), ":")
secondColon = InStr(firstColon+1, xNode.getAttribute("ServerBindings"), ":")

If InStr(xNode.getAttribute("SecureBindings"), "443") <> "0" Then
Port = "443"
Else
Port = Mid(xNode.getAttribute("ServerBindings"), firstColon+1, secondColon-firstColon-1)
End If
HostName = Right(xNode.getAttribute("ServerBindings"), Len(xNode.getAttribute("ServerBindings"))-secondColon)
If (Len(HostName) = "0") Then
HostName = objNet.ComputerName
End if
End If
getSharePointURLfromAppID = HostName & ":" & Port
Next
End If
End Function

'Disposing objects
Set objNet = Nothing
Set objNodeList = Nothing
Set objXmlDoc = Nothing
Set objFSO = Nothing
Set objShell = Nothing
WScript.Quit

1 comment:

Anonymous said...

Pirooz,
Your backup script worked great. How do I reinstall the GAC files it put into separate folders in the backup folder?