SharePoint 2010 speichert die Daten in der SQL Datenbank, die Datenbank wird vom DBA gesichert. Das ist die eine Seite… Dennoch kommt es vor, dass eine Anforderung aus dem Backup Recovery Konzept die Notwendigkeit eines regelmässiges Site Collection Backups erfordert.
In der SharePoint Central Administration Seite kann man das nicht per scheduling durchführen. So habe ich als einfache Lösung den Ansatz gewählt, mittels Powershell Script den oder die notwendigen Site Collections zu sichern. Damit das aber kompakt bleibt, habe ich ein PS Script erstellt, welches die Site Collection Backups aufgrund einer Konfigurations Datei (XML) ausführt.
Ziel: In der XML Datei kann ich die Parameter für den Backup Job Angeben. In der XML Datei kann ich definieren, welche Site Collections mitgesichert werden.
BackupConfig.XML
Das Powershell Script für die Datensicherung sieht dann so aus:
1: # ===============================================================================================
2: # Script Name: TVD_SCBackup.ps1
3: #
4: # AUTHOR: Stephan Jola, Trivadis AG
5: # DATE : 29.05.2012
6: #
7: # ==============================================================================================
8: param
9: (
10: [string]$InputFile = $(throw '- Need parameter input file (e.g. "c:\SP2010\Scripted\SetInputs.xml")')
11: )
12:
13: Add-PsSnapin Microsoft.SharePoint.PowerShell
14: ## Take care of the disposable objects to prevent memory leak.
15: Start-SPAssignment -Global
16:
17:
18: ## Go throughthe Site Collection configuration parameter in the input xml file
19: Write-Host -ForegroundColor Green "- Reading Input File $InputFile..."
20: $xmlinput = [xml] (get-content $InputFile)
21: $item = $xmlinput.SiteCollections
22:
23:
24: ## Read the Backup parameters
25: $BackupDir = $item.BackupDir
26: $BackupRetentionDays = $item.BackupRetentionDays
27: $eMailFrom = $item.eMailFrom
28: $eMailTo = $item.eMailTo
29: $SMTPServer = $item.SMTPServer
30: $MailSubject = $item.MailSubject
31:
32: ## Other Settings
33: $CrLf = "`r`n"
34: $outstr = "SharePoint Site Collection Summary:`r`n"
35:
36: # Remove all that backup folders created > BackupRetenationDays
37: get-childitem $BackupDir | where {$_.Lastwritetime -lt (date).adddays(-$BackupRetentionDays)} | remove-item -recurse -Confirm:$false
38:
39: # invalid characters such as "/" and ":"
40: $today=Get-Date -format "MM-dd-yyyy HH.mm.ss"
41:
42: # Create a folder in the backup location with todays date (sortable)
43: $todayFolder = $BackupDir + '\' + $((get-date).toString('MM-dd-yyyy'))
44: $logFile="$todayFolder\BackupLog.log"
45: md $todayFolder
46:
47: [xml]$SiteCollectionList = Get-Content $InputFile
48:
49:
50: foreach ( $SC in $SiteCollectionList.SiteCollections.SiteCollection)
51: {
52: write-Host Start backing up $SC.siteName to $todayFolder
53: try {
54: # Create a new backup file and name it based on current date.
55: # If you want to create only 1 backup file and overwrite it
56: # each time the backup is run, you can replace "$today.bak"
57: # with your desired file name.
58: $pathName = ($todayFolder + '\' + $SC.siteName + '.bak')
59:
60: # This farm does not have Enterprise SQL Server, so snapshots
61: # cannot be used. This requires that the sites belocked for
62: # update during the backup, so this should be run after hours
63: Backup-SPSite -Identity $SC.siteURL -Path $pathName
64: $outstr = $outstr + $today +': Backup of Site Collection ' + $SC.siteURL + ' succeeded.' +$CrLf
65: # Write success message to the log file
66: write $today $SC.siteURL " successfully backed up.">> $logFile
67: }
68:
69: catch {
70: $outstr = $outstr + $today +': Backup of Site Collection ' + $SC.siteURL + ' failed.' +$CrLf
71: # Write error message to the log file
72: $e = $Err[0].ToString()
73: write "$today $site Error: $e" >> $logFile
74:
75: }
76: }
77:
78: $body = $outstr
79: $smtp = new-object Net.Mail.SmtpClient($SMTPServer)
80: $smtp.Send($eMailFrom, $eMailTo, $MailSubject, $body)
81:
82: Stop-SPAssignment -Global
83: Remove-PsSnapin Microsoft.SharePoint.PowerShell
Das ganze dann noch mit dem Task Scheduler ausführen lassen und fertig ist das Site Collection Backup.