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
- 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.
17 thoughts on “Automated PowerShell Script to backup SharePoint Farm or Site Collection with email Notification”
Tweets that mention Automated PowerShell Script to backup SharePoint Farm or Site Collection with email Notification -- Topsy.com
December 7, 2010[…] This post was mentioned on Twitter by Jeffery Hicks, Mukesh Parmar. Mukesh Parmar said: Automated PowerShell Script to backup SharePoint Farm or Site Collection with email notification.. http://bit.ly/gH2NAc […]
cataract surgeries
December 16, 2010Nice Post…
[…]I saw this really great post today. I linked back to it from my site. Thanks![…]…
Alshah
December 30, 2010Thanks 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
SharePoint 2010 Backup scripts « amavs
January 19, 2011[…] Automated PowerShell Script to backup SharePoint Farm or Site Collection with email Notification. […]
Dilip Nikam
November 9, 2011Is 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.
Mukesh Parmar
June 3, 2012Dilip, Will start posting on SharePointPolice soon, will post an article on how to do that too 🙂
Imran Nawaz
July 18, 2012Thanks 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
Tim
July 4, 2012how would you add an if statement to the above?
Giuliano
August 7, 2012Good 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
Web Copying Program
September 7, 2012It’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.
Hung Lima
November 9, 2012Everyone 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.
Mike
May 2, 2013Hi !
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
Mukesh Parmar
November 20, 2013Mike, it depends on the level of customization done on your SharePoint farm. In most cases one recover from a dead server scenario if you have the full farm backup.
In other cases, depending on the level of customization one might need a backup of the following.
1. List of any manual entries done in Web.Config of any Web Application
2. Backup of FilesDLL’s stored in the inetpub folder.
3. Files stored in the 1415 hives folder.
4. Any other files stored on any other location other than the Content DB & the above mentioned folders.
The best people to answer the same would be the people who have developed the custom solutions.
Let me know if you need any further details or help on the same
Maxim
August 9, 2013Those links should work. The published ones come form the previous website of the poster 😉 :
http://sharepointpolice.com/wp-content/uploads/2010/12/SP2010_Site_Collection_Backup_With_Notification.txt
http://sharepointpolice.com/wp-content/uploads/2010/12/SP2010_Farm_Backup_With_Notification.txt
Best regards,
Maxim
ashotosh das
November 13, 2013Hi,
I can not download the script. When I click in the link it does nothing. Can u help pls?
Cheers,
Ash
Mukesh Parmar
November 20, 2013Sorry, This took time.
The Links are fixed now 🙂
Vijay
April 6, 2015Thank you very much for sharing this very useful script. It is working perfectly fine with email notifications!
Thanks,
Vijay
Leave a reply