SharePoint 2010 Backup scripts

Easy backup procedure can be to daily backup the farm and clean the old backups.

Backup script:

Add-PsSnapin Microsoft.SharePoint.Powershell
Backup-SPFarm -Directory \\server_name\backup_folder_name -BackupMethod full

Clean script deletes all backups that are older than 3 days

# Location of spbrtoc.xml
 $spbrtoc = "\\server_name\backup_folder_name\spbrtoc.xml"

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

# 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
 Write-host "Backup(s) entries older than $days days are removed from spbrtoc.xml and harddisc."

In order to run the script invoke the following command in powershell (you can put this script into BAT file if you want to run it from the command prompt) :

powershell -command C:\BackupScripts\Script\CleanOldBackups.ps1

We have to run Backup and Clean scripts daily. We can do this by using “Task Scheduler”. Put the call of both scripts into separate BAT files.

Scripts are written in PS1 files.

We call above scripts from BAT files.

Then take the following steps in order to setup daily farm backup.

1. Run the Task scheduler

2. In Task Scheduler create new task (Action / Create Task)

3. Setup the task (here are some screen shots):

See the following links as well:

Enhanced by Zemanta

About Andrej Dobrovoljc

Andrej Dobrovoljc

Posted on 19. January, 2011, in SharePoint and tagged , , , . Bookmark the permalink. 4 Comments.

  1. This is a gr8 script that I was looking for.

  2. The script is failing. It’s doing some string-based comparison instead of date-based comparison. Please see:

  3. Does not work for systems running European Date formats…

  4. Hi,

    Can I just ask a couple of questions please? Can you just call the scripts directly from within the Task Scheduler? i.e. PowerShell.exe (for Start a Program) and “C:\BackupScripts\Script\CleanOldBackups.ps1” (for Add Arguments)? Without the need of creating a *.bat file?

    Also, for some reason, on one of my environments I can run PowerShell scripts using Task Scheduler without any problems. However, in another environment, I cannot use Task Scheduler at all to run any scripts. Only if I use PowerShell from within a console/command window will the scripts work. But i cannot get the script to work from Task Scheduler. Any ideas?

    Thank you in advance, Ash

Leave a Reply

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

You are commenting using your 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: