Monday 5 March 2012

SharePoint 2010 Farm Backups via PowerShell

There are a few ways to do this but this method gave good results.

Overview :
Setup a share on a Server you will use to store the farm backups. This can be any one of your SP servers in the Farm. We normally backup to a share on the SQL box.
Setup scheduled tasks to trigger a batch file which runs the PS scripts to backup the farm, or cleanup the backups. This seems like an extra step but worked for me on Win 2008 R2 where nothing else would after a long day.
A good place to run this task would be an application server that most folk don't have access to as i set the script execution policy to unrestricted.



Pre-reqs :

Script execution status :
Check the server that you will be running the PS scripts on.
Ensure the execution policy for PS scripts are set to unrestricted.
(run PS as admin) PS commands in blue
To check : Get-ExecutionPolicy
To Set : Set-ExecutionPolicy Unrestricted


Setup your File share to store you backups :
Share the folder with the following settings
Everyone full access
NTFS permissions : SQL account & SP Admin account full access. (you need both accounts listed here)
Share name : \\Servername\FarmBackups



PS Scripts setup : 

Place you backup scripts in a relevant folder
\\Servername\FarmBackups\backupscripts


1. Setup a .bat file to be triggered by the scheduled task
File name : 1schedfarmbakup.bat
Insert the following line in the file and save. Batch file command in red
powershell -command "&{D:\FarmBackups\backupscripts\2backupSPfarm.ps1}"




2. Setup the powershell script file to backup the Farm.
File name : 2backupSPfarm.ps1
Insert the following lines and save
Add-PsSnapin Microsoft.SharePoint.Powershell
Backup-SPFarm -Directory \\Servername\FarmBackups -BackupMethod full -BackupThreads 10 -percentage 10 -verbose –force

Note : backup farm to specified directory using full backup with 10 threads, display the results every 10 percent verbosely and force the script to run even if there is not enough space on the drive. so make sure you have monitoring on you drive space!


3. Setup the bat file to clean out old Farm backups from the server.
File name : 3clean.bat
Insert line below and save
powershell -command "&{D:\FarmBackups\backupscripts\4cleanbackups.ps1 }"


4. Setup the powershell cleanup script

Filename  : 4cleanbackups.ps1
Insert the following, with correct path, in italic below, and your required number of backups and save

#///Start of ps script
# Location of spbrtoc.xml
$spbrtoc = "\\servername\FarmBackups\spbrtoc.xml"

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

# 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."

#///end of ps script



Setup Scheduled tasks.

Login to the Server running the backups as SP setup account


Setup new task (Backup the farm)

General:
Name - Backup SharePoint Farm
Run whether user is logged in or not
Run  with highest privileges
Configure for : windows 2008 R2

Trigger :
Schedule
Daily at 18:00
Stop if runs longer than 5 hours (enter appropriate time)
Enabled

Actions :
start a program
Browse to the 1schefarmbackup.bat
Fill in appropriate start in dir

Conditions :
Start task only if computer is on AC

Settings :
Allow run on demand
Stop if runs longer than 3 days
If task does not end, force to stop.

Click OK to create task and enter PWD for SP Setup account.



Setup new task  (cleanup old backups)

General:
Name – Cleanup SP Farm Backups
Run whether user is logged in or not
Run  with highest privileges
Configure for : windows 2008 R2

Trigger :
Schedule
Daily at 04:00
Stop if runs longer than 2 hours (enter appropriate time)
Enabled

Actions :
start a program
Browse to the 3clean.bat
Fill in appropriate start in dir

Conditions :
Start task only if computer is on AC

Settings :
Allow run on demand
Stop if runs longer than 4 hours
If task does not end, force to stop.

Click OK to create task and enter PWD for SP Admin account.



Lastly, test your scheduled tasks and ensure the backup folders and .xml files are created on your share. Then check your cleanup scripts run successfully and cleanup the relevant folders a day or two later.



This came from somewhere but never kept the reference, apologies if came from you.

PowerShell script not running as Scheduled Task Server 2008
http://social.technet.microsoft.com/Forums/en-US/ITCG/thread/0ea10ab8-8dd3-4c4c-977c-bbfd0bb17a1d/

No comments:

Post a Comment