Wednesday, March 20, 2013

SharePoint Site Collection Backups Out of the Box

In this article I am going to show you a very simple way that you can configure your SharePoint Site Collection Backups using Powershell. These Powershell scripts were written for 2013, but should work for 2010 as well.

   There are a couple of different types of backups that you can accomplish by using Powershell; Farm Backups - Full, Farm Backup - Differential, Site Collection Backups, and Configuration Database Backups.
   You can technically accomplish a granular backup of all content, but this isn't really a backup as much as a site copy, and I won't be going into that at this time.

Although I personally run all of these backups, my experience is that the most utilized backup is the Site Collection Backup. So lets go ahead and get started.

The first step of any Powershell script is ensuring that the proper scriptlets are loaded, SharePoint is no exception.

1: To load the SharePoint Scriptlet into your SharePoint Script add the following lines

#Add the SharePoint Snap-in, in case PowerShell wasn't started with the Management Shell
Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue


2: The code in this piece of the script will enumerate all site collections within a web-app and then return them to the variable $sites

#Enumerate Sites
$sites=Get-SPWebApplication "Web App URL or Name" |
    Get-SPSite -limit ALL


3: Now that you have your variable of Site Collections, you want to loop through them and back them up. It will also name all of the backup files based on the site collection Title.


#Back-Up Sites
foreach($site in $sites)
{
Backup-SPSite -Identity $site.Url -Path "$backuppath$((Get-SPWeb -Identity $site.Url).Title).bak" -Force -Verbose
Write-Host "Successfully backed up $($site.Url) to $backuppath$((Get-SPWeb -Identity $site.Url).Title).bak"
}

4: The final step of this process it to schedule the backups. This must be done from one of your Web Front Ends (WFE), simpley just launch your task schedule and add your .ps1 file, ensure that the account you are using has full privileges to the site collections.


Here is the script in one piece.
#Add the SharePoint Snap-in, in case PowerShell wasn't started with the Management Shell
Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue

#Enumerate Sites
$sites=Get-SPWebApplication "Web App URL or Name" |
    Get-SPSite -limit ALL

#Back-Up Sites
foreach($site in $sites)
{
Backup-SPSite -Identity $site.Url -Path "$backuppath$((Get-SPWeb -Identity $site.Url).Title).bak" -Force -Verbose
Write-Host "Successfully backed up $($site.Url) to $backuppath$((Get-SPWeb -Identity $site.Url).Title).bak"
}

1 comment: