Home > SharePoint 2010 > PowerShell Script to Backup Site Collections

PowerShell Script to Backup Site Collections

This script will create a backup for every site collection. The directory name  that contains the site collection backup is the GUID for site collection (the log file contains the mapping for GUID to URL). Make sure the backup location is shared so that the process can write.

This code shows how to remove particular site collections from the backup. It also prunes the number of backups that kept by deleting all backup folders older than 7 days.

# Backup all site collections in the farm, placing them in 
# a directory on a shared location
# Note - Does not backup the /train site collection
Add-PsSnapin Microsoft.SharePoint.PowerShell

# Take care of the disposable objects to prevent memory leak.
Start-SPAssignment -Global

# This is the backup path - must be shared

# Remove all that backup folders created > 7 days ago
get-childitem $backupLocation | 
     where {$_.Lastwritetime -lt (date).adddays(-7)} | 
     remove-item -recurse -Confirm:$false

# Get current date for use in log file and format it to avoid 
# invalid characters such as "/" and ":"
$today=Get-Date -format "MM-dd-yyyy HH.mm.ss"

# Create a folder in the backup location with todays date (sortable)
$todayFolder = $backupLocation + '\' + $((get-date).toString('MM-dd-yyyy'))
md $todayFolder

# Does not backup the /train site collection
# or the temporary web ap site collection on port 15702
foreach ($site in get-spsite -limit all | 
         where-object -FilterScript {$_.url -notlike "*/train*"} | 
         where-object -FilterScript {$_.url -notlike "*15702"}) {
    write-Host Start backing up $site to $todayFolder
    try {
         # Create a new backup file and name it based on current date.
         # If you want to create only 1 backup file and overwrite it
         # each time the backup is run, you can replace "$today.bak"
         # with your desired file name.
         $pathName = ($todayFolder + '\' + $($site.ID) + '.bak')
         write-Host 'Backing up ' $site ' GUID = ' $($site.ID)

         # This farm does not have Enterprise SQL Server, so snapshots 
         # cannot be used. This requires that the sites belocked for 
         # update during the backup, so this should be run after hours
         Backup-SPSite -Identity $site -Path $pathName -EV Err 
            -EA "SilentlyContinue"
         write-Host Backup succeeded.

         # Write success message to the log file
         write "$today $site GUID = 
           $($site.ID) successfully backed up.">>logFile
    } catch {
        write-Host Backup failed. See $logFile for more information.
        # Write error message to the log file
        $e = $Err[0].ToString()
        write "$today $site Error: $e">>logFile
Stop-SPAssignment -Global
Remove-PsSnapin Microsoft.SharePoint.PowerShell
write-Host "Finished script."
Categories: SharePoint 2010
  1. Hartz
    May 21, 2012 at 4:59 am

    Nice script, thank you.

  2. March 27, 2014 at 6:39 am

    Nice script man, Thanks a lot….

  1. December 12, 2015 at 11:57 pm

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 )

Google photo

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

Connecting to %s