Free SharePoint Backups (1 of 4)

SharePoint
This entry is part 1 of 4 in the series Free SharePoint Backups

I’ll be writing a series of 4 articles on how to backup SharePoint for free. In future articles we’ll discuss backing up different components you may need to perform a disaster recovery or a simple backup. These are “okay” backup solutions and definitely can’t compare to 3rd party backup solutions which I’ll touch on in the last article of this series.

First up: Backup all Site Collections

Backing up all Site Collections independently has saved me a lot of time. If a Site Owner or even a contributor deletes something important, I can go back to this copy. Its come in handy for those situations where the site just won’t load (web part problem or something else). With SharePoint 2007’s recycle bins, this has become less of an issue but at least for me, hasn’t done away completely for the need of individual Site backups.

This solution has 3 steps: A Windows Script, a batch file, and the task scheduler.

Step 1:

The Windows Script is created by Michael Noel and is part of his book, SharePoint 2007 Unleased. Todd Klindt has kindly hosted the script, it can be found here. I copied the code here for ease, just press down to expand.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
 
<?xml version="1.0" ?>
<package>
 <job id="SPSiteBackup">
  <runtime>
    <description>
************************************************************
SharePoint 2003/2007 Site Backup Tool
************************************************************
    </description>
        <named name="virt" helpstring="The Virtual Server you wish to backup WSS Sites from." type="string" required="1" />
        <named name="path" helpstring="The UNC or file path you want to backup WSS 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.companyabc.com" /report:"SPAdmin@companyabc.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.
'
'To automate this process, the SP 2003/2007 Site Backup Tool (SPSiteBackup) was 
'written for the SAMS Publishing book SharePoint 2007 Unleashed. Contributing 
'Author to the book Tyson Kopczynski was principally involved in the creation
'of the original script.  The script was originally written for the SharePoint 
'2003 Unleashed book but works in SPS 2003, MOSS 2007, WSS 2.0, and WSS 3.0.
'
'This script fully automates the job of site backups.  To perform this automation 
'SPSiteBackup uses the local copy of stsadm on a SharePoint 2003/2007 front-end 
'server to query a list of all the site collections in the specific web application
'referenced by the script. SPSiteBackup then uses this list to then backup each 
'site collection using stsadm. Copy the script to the same file location where
'the stsadm file is located.  This varies depending on the version of WSS in use.
'
'C:"\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN" is
'the location of the file in WSS 3.0.
'C:"\Program Files\Common Files\Microsoft Shared\web server extensions\60\BIN" is
'the location of the file in WSS 2.0.  These paths assume that the SharePoint
'binaries are installed on the C:\ drive.
'
'For optimal effect, this script should be invoked via a batch file, which can 
'then be setup to run as a Scheduled task on the Web front-end server.  This allows
'the script to be run with specific credentials, since full admin rights to the
'SharePoint site collections is required to perform the backup.  Sample syntax to
'include in the batch file would be as follows:
'
'cscript C:"\Program Files\Common Files\Microsoft Shared\web server extensions
'\12\BIN\SPSiteBackup.wsf" /virt:"https://sp.companyabc.com" 
'/path:"\\backupserver\backupshare\SPBAckups" /smtpserver:"mail.companyabc.com" 
'/reportto:"spadmin@companyabc.com"
'
'In this case, it is assumed that the STSADM file is located on a WSS 3.0 server.
'If using WSS 2.0, change the path from \12\BIN to \60\BIN.
'
'For more information on using this script, please reference the SAMS Publishing
'book SharePoint 2007 Unleashed, written by Michael Noel and Colin Spence.
'ISBN-10: 0-672-32947-6; ISBN-13: 978-0-672-32947-0; Published: Apr 12, 2007
'
'===================================================================
' 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
 
'--------------------
' 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 = "STSSiteBackup@" & arrEmailAddress(intArraySize)
 
	' This is the subject
	strSubject = "STS 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 SPS 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>

Step 2:
Create a folder called “BackupBatch” on your data drive (D:\ in the example). Also create a folder on another drive (G:\ in the example) called “Backups”; create a batch file by copying and pasting the example below into notepad. Here’s a sample one that I have created and use. Remember to change your drive letters and file paths.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@ECHO OFF
ECHO ================================================
ECHO Backup Script For Office SharePoint Server 2007
ECHO        Written By: Wahid Saleemi
ECHO ================================================
REM ## Check for STSADM.EXE
D:
CD D:\BackupBatch
IF EXIST D:\BackupBatch\STSADM.EXE GOTO START
COPY "C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN\STSADM.EXE" "D:\BackupBatch"
:START
REM ## Archive yesterday's backups; Delete 2 days prior
DEL /Q G:\Backups\Daily\Archive\*.DAT
MOVE /Y G:\Backups\Daily\*.DAT G:\Backups\Daily\Archive
REM ## Run the Backup for all WSS Sites
ECHO "Backup Started:" %date% %time% >> dailybackup.log
cscript SPSiteBackup.wsf /virt:"http://sharepoint.domain.com" /path:"G:\Backups\Daily" /smtpserver:"mailserver" /reportto:"PortalAdmin@domain.com"
ECHO "Backup Complete:" %date% %time% >> dailybackup.log

Step 3:
Finally, create a scheduled task that calls the batch file. I set mine to run using the farm service account at 1am everyday.

This post shows you how to create backup files for each Site Collection and store them on the server or to a file share. That file share can be backed up to tape or disk for archiving if you needed. Next up, I’ll show you how I use some of these same techniques to create Farm-level backups.

email
Series NavigationFree SharePoint Backups (2 of 4)
1 comment… add one

Leave a Comment

%d bloggers like this: