You can use the following code to take full backup of SharePoint 2010 farm. I found couple of different scripts on the Internet and combined all of them to the following,so thanks to the authors! This script also send an e-mail about backup success or failure.
1: Add-PsSnapin Microsoft.SharePoint.Powershell
2: Clear-Host
3: $Error.Clear()
4:
5: $FromAccount = '<specify from e-mail address to be used in e-mail message>'
6: $ToAccount = '<specify to e-mail address to be used in e-mail message>'
7: $smtpServer = '<specify SMTP server>'
8: $today = (Get-Date -Format yyyy-MM-dd)
9:
10: #if you want to always backup to one directory, remove $today from the following line
11: $BackupDir = '<specify backup path>\$today'
12:
13: Write-Host -f green "Creating backup folder..."
14:
15: #Comment the following line if you are always backing up to the same directory
16: [IO.Directory]::CreateDirectory($BackupDir)
17:
18: Write-Host -f green "Initiating backup..."
19: Backup-SPFarm -Directory $BackupDir -BackupMethod Full -BackupThreads 3 -Force -ErrorAction SilentlyContinue
20:
21: IF($Error[0] -ne $null)
22: {
23: Write-Host -f green "Backup encountered error(s)..."
24: $xmldata = [xml](Get-Content ($BackupDir +'\spbrtoc.xml'))
25: $Node = $xmldata.SPBackupRestoreHistory.SPHistoryObject | Where-Object {$_.SPErrorCount -gt '0' -and $_.SPStartTime -gt (Get-Date -DisplayHint Date)}
26: $FailureMsg = $Node[0] | % {$_.SPFailure}
27: $Att = ($Node[0] | % {$_.SPBackupDirectory}) + 'spbackup.log'
28: $msgBody = 'An Error occurred while trying to backup your SharePoint Farm. Details : ' + $Failuremsg + 'Attached is the Error Log for additional reference.'
29:
30: $SMTPClient = New-Object Net.Mail.SmtpClient($smtpServer)
31: $SMTPClient.Send($FromAccount, $ToAccount, 'Error Occured in SharePoint Backup', $msgBody)
32: Write-Host -f green "Failure e-mail sent"
33: }
34: Else
35: {
36: $msgBody = "Backup completed successfully on "+"$today"
37: $SMTPClient = New-Object Net.Mail.SmtpClient($smtpServer)
38:
39: $SMTPClient.Send($FromAccount, $ToAccount, 'SharePoint Farm Backup was Successful', $msgBody)
40: Write-Host -f green "Success e-mail sent"
41: }
42: Write-Host -f Green "Operation Complete"
Much thanks for sharing this. Of all the scripts I've found, this is the only one that properly sends notification upon failure. All the others ones I've tried only send e-mails saying it was successful, even if the job fails.
ReplyDeleteJoseph:
DeleteThanks for the reply. Glad I was able to help.
Thanks!