Thursday, April 15, 2010

Daily and Weekly SharePoint 2010 Site Collection backup using Powershell

Backup instructions for Site Collection, read TechNet http://technet.microsoft.com/en-us/library/ee748617(office.14).aspx
I want to backup the site collection daily and keep a backup of every week of the year. I created a Powershell script and scheduled the script.

The Powershell script does the following:

  • checks if SharePoint Powershell Snapin is loaded;
  • checks if backup location exists;
  • backups the site collection;
  • copies the daily backup to the weekly if necassary;

The ‘Backup Site Collection.ps1’ code:

"-------------------------------------"
"Yellow & Red SharePoint Backup script"
"-------------------------------------"
"- One daily backup"
"- And weekly backup"
#Backup location
$backupLocation = "\\XXX\d$\_backups\XXX\"
#Site collection location
$sitecollection = "http://XXX"
#daily filename
$dailybackupfile = Join-Path -Path $backupLocation -ChildPath "backup_yarintranet_daily.bak"
#weekly filename (using uformat as format doesn't have week option)
$weeklybackupfile = Join-Path -Path $backupLocation -ChildPath ("backup_yarintranet_" + (Get-Date -UFormat %Y%W) + ".bak")
# check to ensure Microsoft.SharePoint.PowerShell is loaded
$snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}
if ($snapin -eq $null) {
    Write-Host "Loading SharePoint Powershell Snapin"
    Add-PSSnapin "Microsoft.SharePoint.Powershell"
}
if(!(test-path $backupLocation -PathType Container))
{
    "Backup location (" + $backupLocation + ") not found."
    break
}
"Starting backup process"
Backup-SPSite -Identity $sitecollection -Path $dailybackupfile -Force
"Finshed backup process"
"Copy daily backup to weekly if necessary"
if((test-path $weeklybackupfile -PathType Leaf))
{
    "Weekly backup file found, so no backup will be made"
    break
}
copy $dailybackupfile $weeklybackupfile -Force
"Finished"

 

Schedule the a task using the Windows 2008 scheduler.

image

Make sure that:

  • you set the correct Identity that has access to the backup location;
  • task runs even when user is not logged in;
  • Set interval of script execution in ‘Trigger’ tab;

image 

In ‘Action’ Tab set the execution of the Powershell script:

  • Program/script: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
  • Arguments: -file "<full path location to your Site Collection backup Powershell script>" (exclude the <, include the ")

And as always, make sure you test your backup. Otherwise you’ll have a very bad day when disaster strikes.

2 comments:

  1. Hi there.

    The script is good, but the task didn't work. The Task Scheduler says it run but no backup is done. If I try to run manually the task, Task Scheduler service goes down.

    Any clues?

    Thx.

    ReplyDelete
  2. I've found it has something to do with "Run whether user is logged on or not" option. When it's selected, the task doesn't work.

    Still looking for a solution. Any clues will be appreciated :-)

    ReplyDelete

Note: Only a member of this blog may post a comment.