locked
stsadm backup using SPSiteBackup.wsf RRS feed

  • Question

  • I'm using the command line backup script. Here..

    <?xml version="1.0" ?>
    <package>
     <job id="SPSiteBackup">
     <runtime>
      <description>
    ************************************************************
    SPS 2003/2007 Site Backup Tool
    ************************************************************
      </description>
        <named name="virt" helpstring="The Virtual Server you wish to backup SPS Site from." type="string" required="1" />
        <named name="path" helpstring="The UNC or file path you want to backup SPS Sites to." type="string" required="1" />
        <named name="smtpserver" helpstring="SMTP Server to send email report to." type="string" required="0" />
    	  <named name="reportto" helpstring="Email address to send email report to." type="string" required="0" />
      <example>
    Example:
    cscript SPSiteBackup.wsf /path:"\\remoteserver\sharename"
    cscript SPSiteBackup.wsf /path:"c:\sitebackups" /smtpserver:"smtpserver.mycompany.com" /report:"SPSAdmin@mycompany.com"
      </example>
     </runtime>
     <script language="VBScript">
    <![CDATA[
    On Error Resume Next
    
    '===================================================================
    ' Comments about the script
    '===================================================================
    'Microsoft Windows SharePoint Services provides administrators a command-line
    'tool to backup and restore Web sites called stsadm.exe. Using this tool you
    'can perform a site-by-site backup and restore. However, to perform a backup 
    'of all the sites hosted by Microsoft Windows SharePoint Services requires
    'administrators to run the command-line tool for each site or write a batch file
    'to back up all sites. Running the command-line tool by hand or maintaining
    'a batch file in a dynamic environment can be time consuming.
    '
    'Luckily, the SPS 2003/2007 Site Backup Tool (SPSiteBackup) has been written to
    'fully automate the job of site backups. To perform this automation SPSiteBackup
    'uses stsadm to query a list of all the sites from Microsoft Windows SharePoint
    'Services. SPSiteBackup then uses this list to then backup each site using stsadm.
    
    '===================================================================
    ' Check args
    '===================================================================
    Dim StdOut, StdIn
    Set StdOut = WScript.StdOut
    Set StdIn = WScript.StdIn
    
    If WScript.Arguments.Named.Exists("virt") = FALSE Then
     WScript.Arguments.ShowUsage()
     WScript.Quit()
    End If
    
    If WScript.Arguments.Named.Exists("path") = FALSE Then
     WScript.Arguments.ShowUsage()
     WScript.Quit()
    End If
    
    If WScript.Arguments.Named.Exists("smtpserver") = TRUE Then
      If WScript.Arguments.Named.Exists("reportto") = FALSE Then
        	StdOut.WriteLine("Please define reportto.")
    	    StdOut.WriteLine(vbNullString)
    	    StdOut.WriteLine("Click Enter to continue...")
    	    StdIn.ReadLine()
    	    WScript.Arguments.ShowUsage()
    	    WScript.Quit()
    	End If
    End If
    
    If WScript.Arguments.Named.Exists("reportto") = TRUE Then
      If WScript.Arguments.Named.Exists("smtpserver") = FALSE Then
        	StdOut.WriteLine("Please define smtpserver.")
    	    StdOut.WriteLine(vbNullString)
    	    StdOut.WriteLine("Click Enter to continue...")
    	    StdIn.ReadLine()
    	    WScript.Arguments.ShowUsage()
    	    WScript.Quit()
    	End If
    End If
    
    Const ForReading = 1 
    Const ForWriting = 2 
    Const ForAppending = 8 
    
    ReDim arrSiteRecords(0)
    Dim FSO, objShell
    Dim strVirt, strPath, strReportTo
    Dim strFileName, strTempFile, strLogFile
    Dim dtmThisMinute, dtmThisHour
    Dim dtmThisDay, dtmThisMonth, dtmThisYear
    
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set objShell = WScript.CreateObject("Wscript.Shell") 
    
    strVirt = WScript.Arguments.Named("virt")
    strPath = WScript.Arguments.Named("path")
    strSMTPServer = WScript.Arguments.Named("smtpserver")
    strReportTo = WScript.Arguments.Named("reportto")
    
    dtmThisSecond = PadDigits(Second(Now), 2)
    dtmThisMinute = PadDigits(Minute(Now), 2)
    dtmThisHour = PadDigits(Hour(Now), 2)
    dtmThisDay = PadDigits(Day(Now), 2)
    dtmThisMonth = PadDigits(Month(Now), 2)
    dtmThisYear = Year(Now)
    
    strFileName = dtmThisYear & "-" & dtmThisMonth & "-" _
    	& dtmThisDay & "-" & dtmThisHour & "-" & dtmThisMinute & "-" & dtmThisSecond
    	
    
    '--------------------
    ' Create log file and sites file
    '--------------------
    If Not FSO.FolderExists(strPath & "\logs") Then
      FSO.CreateFolder(strPath & "\logs")
    End If
    
    strTempFile = strPath & "\logs\" & strFileName & ".sites"
    strLogFile = strPath & "\logs\" & strFileName & ".log"
    
    Set objLogFile = FSO.CreateTextFile(strLogFile, ForWriting, True)
    
    '===================================================================
    ' Backup Sites
    '===================================================================
    Mess "########################################"
    Mess "#      STS Site Backup      #"
    Mess "########################################"
    Mess "Start Time: " & dtmThisYear & "-" & dtmThisMonth & "-" &_
    	              dtmThisDay & "-" & dtmThisHour & "-" & dtmThisMinute & "-" & dtmThisSecond
    Mess vbNullString
    
    'Mess strVirt
    'Mess strTempFile
    '--------------------
    ' EnumSites
    '--------------------
    StatStart "Enum Sites"
      strErrorCode = objShell.Run("cmd /c stsadm.exe -o enumsites -url " & strVirt & " > " & strTempFile, 0, True)
      
      If strErrorCode <> 0 Then
        ' Write to console
    		StdOut.WriteLine(" Critical Error: stsadm command failed")
    		' Write to logfile
    		objLogFile.WriteLine(" Critical Error: stsadm command failed")
    		
    		WScript.Quit()
      End If
    StatDone
    
    Set objTempFile = FSO.OpenTextFile(strTempFile, ForReading)
    
    count = -1
    
    Do While objTempFile.AtEndOfStream <> True 
      strText = objTempFile.ReadLine 
      
      If InStr(strText, "http") Then
        strTemp = Trim(strText)
    	  strWriteLineTextStart = Mid(strTemp,12) 
        intChrPosition = InStr(1,strWriteLineTextStart,Chr(34)) - 1
        strWriteLineTextFinal = Mid(strTemp,12,intChrPosition) 
    
        count = count + 1
        
        If count > UBound(arrSiteRecords) Then ReDim Preserve arrSiteRecords(count)
    		  arrSiteRecords(count) = strWriteLineTextFinal
      End If 
    Loop
    
    Mess vbNullString
    
    '--------------------
    ' Backup Sites
    '--------------------
    Mess "Backing Up Sites:"
    
    For Each SiteRecord In arrSiteRecords
      intTextLength = Len(SiteRecord)
      intChrPosition = InStrRev(SiteRecord,Chr(47)) + 1 
      strWriteLineTextFinal = Mid(SiteRecord,intChrPosition,intTextLength)
      
      StdOut.Write " " & strWriteLineTextFinal
    	objLogFile.Write " " & strWriteLineTextFinal
      
      strErrorCode = objShell.Run("cmd /c stsadm.exe -o backup -url " & SiteRecord & " -filename " _
                & strPath & "\" & strWriteLineTextFinal & "-" & strFileName & ".dat", 0, True)
        
      If strErrorCode <> 0 Then
        StdOut.WriteLine(";ERR: stsadm command failed")
        objLogFile.WriteLine(";ERR: stsadm command failed")
      Else
        Mess ";[DONE]"
      End If    
    Next
    
    Mess "Done Backing Up Sites"
    
    objTempFile.Close
    objLogFile.Close
    
    Set objTempFile = Nothing
    Set objLogFile = Nothing
    
    '--------------------
    ' Send Email
    '--------------------
    If strReportTo <> "" Then
    	' Define consts
    	Const cdoSendUsingMethod = "http://schemas.microsoft.com/cdo/configuration/sendusing"
    	Const cdoSendUsingPort = 2
    	Const cdoSMTPServer = "http://schemas.microsoft.com/cdo/configuration/smtpserver"
    	
    	' This is the sender of the report
    	arrEmailAddress = Split(strReportTo,"@")
    	intArraySize = UBound(arrEmailAddress)
    	strFrom = "MOSS_Services@" & arrEmailAddress(intArraySize)
    
    	' This is the subject
    	strSubject = "MOSS Site Backup Report for " & dtmThisYear & "-" & dtmThisMonth & "-" &_
    	                    dtmThisDay & "-" & dtmThisHour
    	
    	Set objMessage = CreateObject("CDO.Message")
    	Set objConfig = CreateObject("CDO.Configuration")
    	Set objFields = objConfig.Fields
    	
    	With objFields
    		.Item(cdoSendUsingMethod) = cdoSendUsingPort
    		.Item(cdoSMTPServer) = strSMTPServer
    		.Update
    	End With
    	
    	With objMessage
    		Set .Configuration = objConfig
    			.To = strReportTo
    			.From = strFrom
    			.Subject = strSubject
    			.AddAttachment strLogFile
    			.HTMLBody = "Attached is your MOSS site backup for " & strVirt
    	End With
    	
    	objMessage.Send
    
    	Set objMessage = Nothing
    	Set objConfig = Nothing
    	Set objFields = Nothing
    	strHTMLBody = vbNullString
      strFrom = vbNullString
      strSubject = vbNullString
    	arrEmailAddress = vbNullString
    	intArraySize = vbNullString
    End If
    
    '===================================================================
    ' Subs
    '===================================================================
    '--------------------
    ' General Message Sub
    '--------------------
    Sub Mess(Message)
      ' Write to console
    	StdOut.WriteLine(Message)
    	' Write to logfile
    	objLogFile.WriteLine(Message)
    End Sub
    
    '--------------------
    ' General Start Message Sub
    '--------------------
    Sub StatStart(Message)
    	' Write to console
    	StdOut.Write(Message)	
    	' Write to logfile
    	objLogFile.Write(Message)
    End Sub
    
    '--------------------
    ' General Finish Message Sub
    '--------------------
    Sub StatDone
    	' Write to console
    	StdOut.Write(vbTab & vbTab)
    	StdOut.WriteLine("[OK]")
    	' Write to logfile
    	objLogFile.Write(vbTab & vbTab)
    	objLogFile.WriteLine("[OK]")
    End Sub
    
    '===================================================================
    ' Functions
    '===================================================================
    ' This function is used to pad date variables that contain only on digit.
    Function PadDigits(n, totalDigits)
      If totalDigits > len(n) then 
        PadDigits = String(totalDigits-len(n),"0") & n 
      Else 
        PadDigits = n 
      End If 
    End Function 
    ]]>
     </script>
     </job>
    </package>

    I call the script using these parameters...

     

    cscript C:\SPSiteBackup.wsf /virt:"http://moss:43526" /path:"\\server\Mossbackups\Records" /smtpserver:"smtp.mail.com" /reportto:me@someurl.com

    This works fine when my sharepoint site is on port 80, when I specify a non-standard port number I get a zero length backup file...

     

    Any ideas....????

     

    Thanks!!!!

    Dan

     

    Friday, April 30, 2010 6:19 PM

All replies

  • its better to go to the Michael Noels blog and check to comments section . May be you will get the answer for this.

    Aniket
    Friday, June 4, 2010 6:41 AM