Simple scripts that you can automate with the Task Scheduler to backup/cleanup your Sharepoint 2010 farm with email notifications.
Backup Script
try
{
$today = (Get-Date -Format dd-MM-yyyy)
#Location of the Backup Folder
[IO.Directory]::CreateDirectory("E:BackupDailyFarmBackUp$today")
# This will actually initiate the SPFarm backup.
Backup-SPFarm -Directory E:BackupFullFarmBackup$today -BackupMethod full
# Backup-SPFarm -Directory E:BackupDailyFarmBackup$today -BackupMethod differential
# Edit the From Address as per your environment.
$emailFrom = "SPADMIN@Sharepoint.com"
# Edit the mail address to which the Notification should be sent.
$emailTo = "Admin@SharePoint.Com"
# Subject for the notification email. The + “$today” part will add the date in the subject.
$subject = "The SharePoint Farm Backup was Successful for "+"$today"
# Body or the notification email. The + “$today” part will add the date in the subject.
$body = "The SharePoint Farm Backup was Successful for "+"$today"
# IP address of your SMTP server. Make sure relay Is enabled for the SharePoint server on your SMTP server
$smtpServer = "192.168.0.0"
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($emailFrom, $emailTo, $subject, $body)
}
Catch
{
$ErrorMessage = $_.Exception.Message
# Configure the below parameters as per the above.
$emailFrom = "SPADMIN@Sharepoint.com"
$emailTo = "Admin@SharePoint.Com"
$subject = "The SharePoint Farm Backup Job failed on "+"$today"
$body = "The SharePoint Farm Backup Job failed on "+"$today and the reason for failure was $ErrorMessage."
$smtpServer = "192.168.0.0"
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($emailFrom, $emailTo, $subject, $body)
}
Cleanup Script
# Location of spbrtoc.xml $spbrtoc = "\YOURPATHspbrtoc.xml"
# Days of backup that will be remaining after backup cleanup. $days = 14
# Load Backup File [xml]$sp = gc $spbrtoc
# Find the old backups in spbrtoc.xml
$old = $sp.SPBackupRestoreHistory.SPHistoryObject |? { [datetime]$_.SPStartTime -lt ((get-date).adddays(-$days)) }
if ($old -eq $Null) { write-host "$old and $date - No reports of backups older than $days days found in spbrtoc.xml.`nspbrtoc.xml isn't changed and no files are removed.`n" ; break}
# Delete the old backups from the Sharepoint backup report xml file
$old | % { $sp.SPBackupRestoreHistory.RemoveChild($_) }
# Save the new Sharepoint backup report xml file
$sp.Save($spbrtoc)
Write-host "Backup(s) entries older than $days days are removed from spbrtoc.xml and harddisc."
#Check if any error has occured and send an alert email if it did.
if ($error[0])
{
Write-Output "An error has occurred" | Out-File -FilePath C:tempbackupsharepointfarm.log -Append;
for ($i=0; $i -le $error.count; $i++) {Write-Output $error[$i] | Out-File -FilePath C:tempbackupsharepointfarm.log -Append}
###First, the administrator must change the mail message values in this section $FromAddress = "backupjob@yourserver.com" $ToAddress = "you@host.com" $MessageSubject = "Sharepoint Cleanup Failed." $MessageBody = "The Sharepoint Server Cleanup Failed. Please check." $SendingServer = "192.168.0.0"
###Create the mail message $SMTPMessage = New-Object System.Net.Mail.MailMessage $FromAddress, $ToAddress, $MessageSubject, $MessageBody
###Send the message $SMTPClient = New-Object System.Net.Mail.SMTPClient $SendingServer $SMTPClient.Send($SMTPMessage)
}
