Monday, September 30, 2013

Powershell Daily Automated backup of SP 2013 Site Collections

Need to backup your SharePoint 2013 Site collections daily into named.bak files?

Well the script is not elegant or pretty and does not scale well if you have thousands of site collections (but you can slightly modify this script and simply get the sp web top level farm root then iterate in a for loop aka: "for each site in sites"... - just google how to do that in sharepoint powershell if needed and you are too lazy to add in each site collection url by hand - I have done 4 below and each time I need a new one backed up I just add it manually to the script file - 3 lines)

Login to SharePoint Web Server node as SP farm admin.

Step 1: Copy the following to text file save as c:\projects\transfer\backupdaily.ps1 (rename the backup dir, site collection names, and site collection urls according your site collection needs):


Add-PSSnapin "Microsoft.SharePoint.PowerShell"
Set-ExecutionPolicy -ExecutionPolicy "Unrestricted" -Force

$backupdir = "C:\projects\transfer\"

$mm = (Get-Date).Month
$dd = (Get-Date).Day
$yyyy = (Get-Date).Year
$hh = (Get-Date).Hour
$min = (Get-Date).Minute
$ss = (Get-Date).Second
$backupdatetime = "$mm" + "$dd" + "$yyyy" + "_" + "$hh" + "$min" + "$ss"
$backupdatetime

$backupfile = "$backupdir" + "root_" + "$backupdatetime" + ".bak"
$backupfile
Backup-SPSite -Identity "http://sdspweb01/" -Path "$backupfile" -Force -Verbose

$backupfile = "$backupdir" + "change_" + "$backupdatetime" + ".bak"
$backupfile
Backup-SPSite -Identity "http://sdspweb01/sites/change" -Path "$backupfile" -Force -Verbose

$backupfile = "$backupdir" + "assets_" + "$backupdatetime" + ".bak"
$backupfile
Backup-SPSite -Identity "http://sdspweb01/sites/assets" -Path "$backupfile" -Force -Verbose

$backupfile = "$backupdir" + "standards_" + "$backupdatetime" + ".bak"
$backupfile
Backup-SPSite -Identity "http://sdspweb01/sites/standards" -Path "$backupfile" -Force -Verbose



Step 2: You can use Windows Task Scheduler to run this powershell task on a scheduled basis: Open Administrative Tools on the SharePoint web server (any SharePoint web server node) Open Task Scheduler Create new basic task Set it daily at a certain time always (for example) For Action select Run a program For program type in: powershell -file c:\projects\transfer\backupdaily.ps1 (change to your filename and path where the powershell script is saved) If prompted a question about arguments, just click yes. Click run with highest privilege in the checkbox. Save it.

What it will do:
At the certain time each day it will create 4 (however many site collections you have) new files in the backup directory you selected.
Files will be named like "root_9302013_15451.bak" and "change_9302013_15451.bak" next day could be like: "root_10012013_15452.bak" and "change_10012013_15451.bak" (assuming I set them to fire at 3:45pm each day)

No comments: