Powershell script to backup farm configuration and service applications

Requirement

We use Data Protection Manager 2010 in our environment to backup our content databases. Unfortunately, DPM cannot properly back up farm configuration and service applications in SharePoint 2010.

See the following links for more information:

http://technet.microsoft.com/en-us/library/ee428318.aspx – Backing up Service Applications

http://technet.microsoft.com/en-us/library/ee428320 – Backing up Farm Configuration

I decided to with a PowerShell script approach to backing up our environment.

Solution

Our backup solution involves running a daily backup of the farm configuration and service application database to a UNC path. DPM is then configured to backup that location to disk / tape according to the company’s backup retention policy. The next time the script runs, it deletes the previous day’s backup and the process repeats.

In summary, this script will:

  1. Delete all files in the existing backup directory.
  2. Backup the farm configuration database.
  3. Catch any errors that occur and send an e-mail to the administrator.
  4. Backup ALL service applications in the farm (full).
  5. Catch any errors that occur and send an e-mail to the administrator.
  6. Write all actions / error messages to a timestamped log file.

Update the variables in the script to suit your requirements.

#Variables
$logfile = "SPFarm-Backup-" + $(Get-Date -Format dd-MM-yy) + ".log"
$ConfigDB = "SP_Config"
$DBServer = "server"
$BackupConfigFolder = "\\server\SharePoint\Backup\Config"
$BackupSAFolder = "\\server\SharePoint\Backup\ServiceApp"
$AdminEmail = <a href="mailto:admin@sharepoint.com">admin@sharepoint.com</a>
$MailServer = "mail.sharepoint.com"
$FromAddress = <a href="mailto:sharepoint.notifications@sharepoint.com">sharepoint.notifications@sharepoint.com</a>
 
Write-Host "Script starting.."
 
# Backup Farm Configuration
try
{
	ac $logfile "$(Get-Date)`t Clearing old configuration backups.."
	Get-ChildItem $BackupConfigFolder | foreach ($_) {Remove-Item $_.fullname}
	ac $logfile "$(Get-Date)`t Done."
	ac $logfile "$(Get-Date)`t Backing up SharePoint Farm Configuration.. "
	Backup-SPConfigurationDatabase -Directory $BackupConfigFolder -DatabaseServer $DBServer -DatabaseName $ConfigDB -Verbose
	ac $logfile "$(Get-Date)`t Done."
}
catch [system.exception] 
{
		 ac $logfile "$(Get-Date)`t An error occured while backing up the farm configuration database: $_."
 
		 		$messageParameters = @{
				Subject = "Backup Failed: Farm Configuration Database"
				Body = "ERROR $_."
				From = $FromAddress
				To = $AdminEmail
				SmtpServer = $MailServer
			}
 
			Send-MailMessage @messageParameters
}
 
# Backup Service Applications
try
{
	ac $logfile "$(Get-Date)`t Clearing old service application backups.."
	Get-ChildItem $BackupSAFolder | foreach ($_) {Remove-Item $_.fullname}
	ac $logfile "$(Get-Date)`t Done."
	ac $logfile "$(Get-Date)`t Backing up SharePoint Farm Configuration.. "
	Backup-SPFarm -Directory $BackupSAFolder -BackupMethod Full -Item "Farm\Shared Services" -Verbose
	ac $logfile "$(Get-Date)`t Done."
}
catch [system.exception] 
{
		 ac $logfile "$(Get-Date)`t An error occured while backing up the service application database: $_."
 
		 		$messageParameters = @{
				Subject = "Backup Failed: Service Application Database"
				Body = "ERROR $_."
				From = $FromAddress
				To = $AdminEmail
				SmtpServer = $MailServer
			}
 
			Send-MailMessage @messageParameters
}
 
Write-Host "Done."

Tags: ,

2 Responses to “Powershell script to backup farm configuration and service applications”

  1. proresults January 19, 2015 at 10:35 am #

    Thanks for sharing the backup script, Andrew; Any scripts/tips to restore?

Trackbacks/Pingbacks

  1. Another Sharepoint Blog - December 13, 2015

    […] http://www.mysharepointadventures.com/2012/05/powershell-script-to-backup-farm-configuration-and-ser&#8230; […]

Leave a Reply

%d bloggers like this: