(post courtesy Priyo Lahiri)
Out of the box, SharePoint 2010 doesn’t have the capability of scheduling a backup. Fortunately, we have the mix of PowerShell script and Windows Task Scheduler to do the job. In reality it’s just a single line script:
Backup-SPFarm –Directory YourBackupDirectory –BackupMethod Full / Differential
How can we make this backup script more special?
The script below will send an email if an error occurrs during the backup with the error description so that every morning (or whatever your backup schedule is) you don’t have to open up Central Admin and see if the backup went fine or not (assuming you check the backup status / integrity…. I know a lot of people who don’t!) and hopefully it gives you an idea on what went wrong.
*Note: The following script is given as a sample, and should be customized and tested in a lab environment long before it is ever run in production. The domain “LZENG” is used in the examples below.
The PowerShell cmdlet that we can use to send the email is a simple one:
Send-MailMessage -From "firstname.lastname@example.org" -To "YourEmail@domain.com" -Subject "Error captured" -Body “Error Message Here” -SmtpServer YourSMTPServerFQDN
(more parameters are available for the Send-MailMessage cmdlet here: http://technet.microsoft.com/en-us/library/dd347693.aspx)
So I created the simple script below to first see the error message being displayed from PowerShell:
Unfortunately, the error message is not very helpful. It tells me that the backup was not successful but doesn’t give me any pointers as to why. No point sending this to the SharePoint admin. Let’s take a look at the spbrtoc.xml from the backup directory:
<SPFailure>Object lz_Admin failed in event OnBackup. For more information, see the spbackup.log or sprestore.log file located in the backup directory.</SPFailure>
Now that makes a little more sense, I can now see that the Admin Content Database failed during the back-up. So how about sending this to the SharePoint Admin so that he knows exactly what’s going on? To make this more useful, we will also attach the backup log from the SPBackupDirectory with the email so that the SharePoint admin knows everything he needs to about the error.
Here is how we do it… (in pseudocode)
Read the spbrtoc.xml file and pull up the First <SPHistoryObject> block and get the path of the spbackup.txt from <SPBackupDirectory> and attach it to the email.
Here is a script sample
And here is the result:
Here is the full script:
A workable script is attached with this post that you can download and use. All you need to do it specify values for the variables below and follow the instructions to create a task in Windows Task Scheduler.
Variables to populate in the script:
$BackupDir = '\\app02\SPBackup' # Your backup directory here. Recommended to use \\server\share
NOTE: DO NOT put '\' after the above path
$FromAccount = 'email@example.com' # valid domain account or an account with Send As rights
$ToAccount = 'firstname.lastname@example.org' # Recipient email address
$smtpServer = 'mail.lzeng.local' # SMTP / Exchange / SMTP Relay Agent FQDN
If you need other parameters like CC Field or if you exchange uses SSL refer here: http://technet.microsoft.com/en-us/library/dd347693.aspx
Instructions to configure the task:
- Put the ps1 file in a folder (for example C:\scripts)
- In your task scheduler, configure the task to run in highest privilege and choose the option to “Run whether user is logged on or not”
- Now on the Action tab specify this as the Program / Script
- “powershell -command C:\Scripts\BackupFarm.ps1”
- Windows will change it to this:
- In the Trigger create a schedule to run the task every night at 2 AM (or whatever suits your schedule).
You are all set.
If you want, you can create 2 copies of this script and change the second one to run Differential backup every night and Full Backup every Sunday or whatever your backup strategy is.
Ok so now you know I can read the xml file from PowerShell which means I can also remove some lines from it and may be have a cleanup script for backup retention and remove all those failed backup listed in the central admin? You are right on… but that’s for the next post.