Skip to content

SQL Agent Powershell Job to Check SharePoint Farm Backups complete properly with Thrown Error and Email Notification

by on October 29, 2014

SQL Agent BackupFilesCheck Job

$ErrorActionPreference = “Stop”
if ((ls “M:\spbackup\farm\”) -eq $null)
{
$smtp = New-Object Net.Mail.SmtpClient(“smtp.mydomain.com”)
$objMailMessage = New-Object System.Net.Mail.MailMessage
$objMailMessage.From = “sqlsp@mydomain.com”
$objMailMessage.To.Add(“spalerts@mydomain.com”)
$objMailMessage.Subject = “SQLSP SP Farm Backup Failed”
$objMailMessage.Body = “SP Farm Backup Failed – Failure, the backup location M:\spbackup\farm\ is empty. Please check SP Farm backup job in Central Admin and space on server.”
$smtp.send($objMailMessage)
throw “Failure, the backup location is empty. Please check SP Farm backup job in Central Admin and space on server.”
}else{
$smtp = New-Object Net.Mail.SmtpClient(“s-ex6.mydomain.com”)
$objMailMessage = New-Object System.Net.Mail.MailMessage
$objMailMessage.From = “sqlsp@mydomain.com”
$objMailMessage.To.Add(“ebruce@mydomain.com”)
$objMailMessage.Subject = “SQLSP SP Farm Backup Exists”
$objMailMessage.Body = “SP Farm Backup Exists”
$smtp.send($objMailMessage)
}

NOTE: You could use sql job setting for email, but this gives a bit more control IMHO

Task runs Backupmanagement.bat

This along with a scheduled task job that let you keep two copies:

[adjust script as needed]
@echo off
echo ==================================================
echo Backup the farm
echo ==================================================
@SET stsadm=”C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\BIN\stsadm”
rmdir /S /Q “\\sqlsp.mydomain.com\M$\spbackup\farmold”
rename “\\sqlsp.mydomain.com\M$\spbackup\farm” “farmold”
mkdir “\\sqlsp.mydomain.com\M$\spbackup\farm”
%stsadm% -o backup -directory “\\sqlsp.mydomain.com\M$\spbackup\farm” -backupmethod full
echo complete

Advertisements
Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: