Tuesday, November 4, 2008

Backup site collection using Powershell

Scenario:
You want to backup site collection.

Solution:
You can use Powershell script as an option

Code:

###########################################################
# Script to add function to properly backup site collections
#
# SYNTAX:
# Backup-SPSite
# -url <Site Collection URL>
# -path <path to backup file>
# -lock <"noadditions", "noaccess", "readonly"> #state for backup
# -AdditionalCommands [additional commands for Backup]
# Restore-SPSite
# -url <site collection restore url>
# -path <path to backup file>
# -force
#
# EXAMPLE:
# backup-spsite -url "http://localhost" -path "C:\zbackuptest.bak" -AdditionalCommands "-overwrite"
###########################################################

function global:Backup-SPSite($url,$path,$lock="noaccess",$additionalcommands){
$stsadm = "$env:programfiles\Common Files\Microsoft Shared\Web Server Extensions\12\BIN\STSADM.EXE"

#Check Required Vars
If($url -eq "" -or $url -eq $null ){ Write-Host -ForegroundColor "Red" "URL is required"; return; }
If(!($lock -like "none" -or $lock -like "noadditions" -or $lock -like "noaccess" -or $lock -like "readonly" )){ Write-Host -ForegroundColor "Red" "Lock must be 'none', 'readonly', 'noaccess', or 'noadditions'"; return; }
#Setup Variable Defaults

#LOCK SITE
Write-Progress -Activity "Backup SPSite" -PercentComplete 25 -Status "Locking Site"
$oldlockstate = [xml](&stsadm -o getsitelock -url $url)
$void = &stsadm -o setsitelock -url $url -lock $lock

#BACKUP
Write-Progress -Activity "Backup SPSite" -PercentComplete 50 -Status "Backing up Site ($url)"
$sTemp = &stsadm -o Backup -url $url -filename $path $AdditionalCommands
if(!($sTemp -like "*Operation completed successfully*")){ Write-Host -ForegroundColor "red" -BackgroundColor "white" "Backup of site '$url' Failed! `n $stemp"}

#RESET LOCK STATE
Write-Progress -Activity "Backup SPSite" -PercentComplete 75 -Status "Resetting Site Lock"
$void = &stsadm -o setsitelock -url $url -lock ($oldlockstate.SiteLock.Lock.ToString())
Write-Progress -Activity "Backup SPSite" -PercentComplete 100 -Status "Complete"
}
Article:

0 comments: