A.K.A The Communicator
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: http://thecommunicator.co.cc
# ==============================================================================================
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
- First it will get the current system Date in dd-MM-yyyy format [07-12-2010].
- It will create a folder with Current Date as the name in the following location E:\Backup\DailyFarmBackup\
- Next step will be to start SharePoint 2010 Farm backup procedure in the newly created folder.
- Once the backup is complete it will send a notification email to the email address mentioned in the $emailTo field.
- 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.
| Print article | This entry was posted by Mukesh Parmar on December 7, 2010 at 7:10 PM, and is filed under PowerShell, SharePoint 2010. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |

about 2 years ago
Thanks for the Script, was really helpful.
While testing the script, it sends the success email if command fails, so how about using if condition.
Thanks
alshah
about 1 year ago
Is this possible o download the installed wsp from powersript.
I want create the sript which will first take backup of the current wsp and then retract & install new wsp.
about 11 months ago
Dilip, Will start posting on SharePointPolice soon, will post an article on how to do that too
about 10 months ago
Thanks I use this script its working fine with email notification. please tell me how I restore. if we have 10 web app and multiple site collection. can we take backup 1 web app under multiple site collection
about 10 months ago
how would you add an if statement to the above?
about 9 months ago
Good Morning,
I’m trying the script to backup a site collection with notification.
I have notice that, if a file name in the destination path already exist, the script send an email that all is ok, but in reality the backup wasn’t done, and receive the following error on powershell windows:
Backup-SPSite : File C:BCKSITE7-08-2012test.bak already exists. Use the parameter -Force to overwrite an existing b
ackup.
At C:BCK Site.ps1:17 char:16
+ Backup-SPSite <<<< -Identity $Site -Path C:BCKSITE$todaytest.bak
+ CategoryInfo : InvalidData: (Microsoft.Share…mdletBackupSite:SPCmdletBackupSite) [Backup-SPSite], SPE
xception
+ FullyQualifiedErrorId : Microsoft.SharePoint.PowerShell.SPCmdletBackupSite
Sorry for my english.
thanks to all
Giuliano
about 8 months ago
It’s really a nice and useful piece of info. I am glad that you shared this useful information with us. Please stay us up to date like this. Thank you for sharing.
about 6 months ago
Everyone loves what you guys are up too. This kind of clever work and reporting!
Keep up the superb works guys I’ve included you guys to my own blogroll.
about 3 weeks ago
Hi !
The link for the script are down
I also would like to know if it will backup IIS config?
Will i be able to restore everything from this backup if a “server dead” crash occur?
Best regards,
Mike