I have been meaning to write a script in PowerShell to do SharePoint Site Collection backups for a while but not got round to it. I did do a quick and functional one at the tail end of last year, but it wasn't particularly elegant. This script I am much happier with: it will backup all Site Collections at the specified Web Application and place them at the given location in a folder constructed from the day's date. It also writes to a log file and can clean up folders containing backups that are older than a predetermined number of days.
Simply copy the script below and save it to a .ps1 file of your choice. Please retain my comments at the top and, as always, use at your own risk! The deletion portion is quite destructive
. You may also have to set your Execution Policy to a less strict setting, such as RemoteSigned, e.g.:
Set-ExecutionPolicy RemoteSigned
I have tested the script with Microsoft Office SharePoint Server 2007 SP2 and Windows SharePoint Services 3.0 SP2 running on Windows Server 2003 R2 SP2 (PowerShell 1.0). It should work fine for MOSS and WSS running on different Windows Server operating systems.
Here is the script:
# SharePoint Site Collection Backup Script
# Author: Michael Cox
# Version: 1.0
# Date: March 2010
# Contact: http://www.betteritsolutions.co.uk/
# Use at own risk, please retain these comments
# Create alias for STSADM
Set-Alias -Name exeStsadm -Value ($Env:CommonProgramFiles + "\Microsoft Shared\Web Server Extensions\12\BIN\stsadm.exe") -Option Constant
# Specify maximum age in days for backup files
New-Variable -Name maxFileAge -Value 14 -Option Constant
# Get todays date, formatting as yyyyMMdd e.g. 20100224
New-Variable -Name todaysDate -Value (Get-Date -Format yyyyMMdd) -Option Constant
# Specify backup location and set log file location, folder must exist and be writable by account running script
New-Variable -Name backupLoc -Value "S:\Backups" -Option Constant
New-Variable -Name logFileLoc -Value "$backupLoc\site_collection_backups.log" -Option Constant
# Specify Web Application
New-Variable -Name webApplication -Value "http://mossintranet/" -Option Constant
# Change to backup location and create folder, suppressing notification
Set-Location $backupLoc
New-Item $todaysDate -type directory | Out-Null
Set-Location $todaysDate
# Record backup start
Out-File -FilePath $logFileLoc -Append -InputObject $("Backup started at: " + (Get-Date -Format HH:mm` dd/MM/yyyy) + "`n")
# Enumerate Site Collections within chosen Web Application and store in a variable
[xml]$sitesXml = exeStsadm -o enumsites -url $webApplication
# Enumerates each site collection and performes an stsadm sitecollection backup against each site
$sitesXml.Sites.Site | ForEach-Object -Process {$backupFile = $_.Url.Replace('http://','').Replace('/','_') + ".bak"; exeStsadm -o backup -url $_.Url -filename $backupFile; Write-Host "Finished writing: $backupFile"; Out-File -FilePath $logFileLoc -Append -InputObject "Finished writing: $backupFile"}
# Remove folders that are older than the maxFileAge value
Set-Location $backupLoc
$tooOld = (Get-Date).addDays(-$maxFileAge)
Get-ChildItem | Where-Object {$_.PSIsContainer -and ($_.lastWriteTime -le $tooOld)} | ForEach-Object -Process {Remove-Item $_ -force -recurse; Write-Host "Folder $_ was deleted."; Out-File -FilePath $logFileLoc -Append -InputObject "Folder $_ was deleted."}
# Record backup end
Out-File -FilePath $logFileLoc -Append -InputObject $("`n" + "Backup ended at: " + (Get-Date -Format HH:mm` dd/MM/yyyy) + "`n")
Here is a screenshot of files created following a backup of a Web Application with two Site Collections and also the contents of the log file:

Screenshot after running script
Please let me know if this is useful and also feel free to make suggestions for improvements or bug fixes, if you find any.






