SharePoint: Automate Farm Backup and Cleanup

Backing up the SharePoint content is extremely important. There are two steps to the backup process that should be performed on a regular basis to ensure that SharePoint is properly backed up and at top performance. In addition, it is also recommended to perform maintenance tasks routinely to help with performance. These can be found in my SharePoint: Database Maintenance post.

A) SharePoint Farm Backup Task
B) Backup Cleanup Task

SharePoint Farm Backup Task

This is a simple Powershell command that should be scheduled on the Web Front-End server hosting Central Administration.

1) Create a file called C:\Scripts\SP_FullBackup.ps1
2) Enter the following code into the file:


Add-PSSnapin Microsoft.SharePoint.PowerShell

Backup-SPFarm -directory \\Server\BackupShare -backupmethod Full

3) In the Task Scheduler, create a new task that calls the following program and argument:


Powershell.exe C:\Scripts\SP_FullBackup.ps1

a) Ensure that “Run whether user is logged on or not” is selected
b) Ensure that “Run with highest privileges” is selected
c) This task must be run by an account with the following permissions, as suggested by Todd Klindt:

A) Your Central Admin app pool account must have read/write access to the location of the backups.
B) Your SQL Service account must have read/write access to the location of the backups.
C) If you’re running a farm backup from STSADM or Windows PowerShell, the account you’re running it as must have read/write access to the location of the backups
D) The location must be accessible from the SharePoint machine the backup is running on.
E) The location must be accessible from the SQL instance that SharePoint is trying to back up
F) The location must be a UNC Paths (\\server\share), and not local paths (C:\backups)

Backup Cleanup Task

Next, you will want to create a task to cleanup old backups as they will just be taking up unnecessary space. I found a handy script here to automate this.

On the Web Front-End server hosting Central Administration:

1) Create a file called C:\Scripts\SP_CleanBackups.ps1
2) Enter the following code into the file:


# Location of spbrtoc.xml
$spbrtoc = "\\server\BackupShare\spbrtoc.xml"

# Days of backup that will be remaining after backup cleanup.
$days = 7

# Import the Sharepoint backup report xml file
[xml]$sp = gc $spbrtoc

# Find the old backups in spbrtoc.xml
$old = $sp.SPBackupRestoreHistory.SPHistoryObject |
? { $_.SPStartTime -lt ((get-date).adddays(-$days)) }
if ($old -eq $Null) { write-host "No reports of backups older than $days days found in spbrtoc.xml.`nspbrtoc.xml isn't changed and no files are removed.`n" ; break}

# Delete the old backups from the Sharepoint backup report xml file
$old | % { $sp.SPBackupRestoreHistory.RemoveChild($_) }

# Delete the physical folders in which the old backups were located
$old | % { Remove-Item $_.SPBackupDirectory -Recurse }

# Save the new Sharepoint backup report xml file
$sp.Save($spbrtoc)
Write-host "Backup(s) entries older than $days days are removed from spbrtoc.xml and harddisc."

3) In the Task Scheduler, create a new task that calls the following program and argument:


Powershell.exe C:\Scripts\SP_CleanBackups.ps1

a) Ensure that “Run whether user is logged on or not” is selected
b) Ensure that “Run with highest privileges” is selected
c) This task can be run with the same account as the backup script.

Advertisements
This entry was posted in SharePoint 2010 and tagged , . Bookmark the permalink.

One Response to SharePoint: Automate Farm Backup and Cleanup

  1. Pingback: SharePoint: Database Maintenance | My Windows System Administrator

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