Automated PowerShell Script to backup SharePoint Farm or Site Collection with email Notification

In today’s fast paced environment, we really like most of our things to be automated and at the same time get an alert on how things are going, so we can take necessary steps as required. Administering a SharePoint 2010 farm is no different, and as a day to day activity we have to take periodic backups of the entire farm or a particular Site Collection and at the same time be prepared for disaster recovery if required.

I have prepared a small yet powerful PowerShell script for taking a backup of the entire SharePoint 2010 farm and at the same time notifying you via email the outcome of the Script i.e. Was the backup successful or did it fail and the reason why it failed.

Let’s Start

# ==============================================================================================
# NAME: SP2010_Farm_Backup_With_Notification.ps1
# AUTHOR: Mukesh Parmar
# DATE: 07 December 2010
# COMMENT: A Powerful Script to take backup of the entire SharePoint 2010 Farm with email notification.
# Website: https://www.SharePointPolice.com
# ==============================================================================================
Add-PsSnapin Microsoft.SharePoint.Powershell –ErrorAction SilentlyContinue
try
 {
  $today = (Get-Date -Format dd-MM-yyyy)
 #Location of the Backup Folder
  [IO.Directory]::CreateDirectory("E:\Backup\DailyFarmBackUp\$today")
 # This will actually initiate the SPFarm backup.
  Backup-SPFarm -Directory E:\Backup\DailyFarmBackup\$today -BackupMethod full
 # 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)
 }

A brief on the execution of the above script

  1. First it will get the current system Date in ddMMyyyy format [07-12-2010].
  2. It will create a folder with Current Date as the name in the following location E:\Backup\DailyFarmBackup\
  3. Next step will be to start SharePoint 2010 Farm backup procedure in the newly created folder.
  4. Once the backup is complete it will send a notification email to the email address mentioned in the $emailTo field.
  5. If the backup fails for some reason, you will receive an email containing the reason why the backup failed.

You can download the above script & the another one for backing up the Site Collection from the following locations

SP2010_Farm_Backup_With_Notification

SP2010_Site_Collection_Backup_With_Notification

Rename the above files extension from .txt to .ps1

Note

  • Once you have configured the script as per your environment, create a new task in Task Scheduler for the above script and set it to run as per your requirement.
  • Make sure you periodically check the backup folder size as this script will not delete those for you.
  • Make sure you provide a valid From/To address and SMTP server IP address, validate these settings by checking with your Exchange Administrator if needed.
  • As I have mentioned before the script is very powerful, you can change the task to whatever you like to automate and get an email for failure or success for that particular task.