Sign In
 
 
Go Search
 
Zach Rosenfield's SharePoint Blog > Posts > Locking Sites on Backup
Locking Sites on Backup

I recently noticed Michael Blumenthal's blog post on the need to backup a site collection before backing it up. This is absolutely the recommended practice—and everyone should do it! Since there is a need, I thought I'd throw together a PowerShell script to do the complete operation in a single command. Here's the outline of my design for the "Backup-SPSite" function script:

First, I want to be able to call this as a function, so I create the global function declaration with my parameters and checks:

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;

}

 

Next, I store the site's current lock state and then lock the site collection:

$oldlockstate = [xml](&stsadm -o getsitelock -url $url)

$void = &stsadm -o setsitelock -url $url -lock $lock

Then I backup the Site Collection, using any additional parameters the user wants with the "AdditionalCommands" Variable:

$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"}

Finally, I reset the lock state and end the function block

$void = &stsadm -o setsitelock -url $url -lock ($oldlockstate.SiteLock.Lock.ToString()) }

 

That's it! I added this script to my profile 'bootstrap' and then I can backup site collections by simply typing a command like: backup-spsite -url "http://localhost" -path "C:\zbackuptest.bak" -AdditionalCommands "-overwrite". The full script with additional progress information is available here, and as always, use at your own risk!

Comments

Nice Script!

That looks handy! I've posted the permalink for your post to the comments on Keith Richie's original post on the matter: http://blog.krichie.com/2007/02/11/perform-non-intrusive-site-collection-level-backups-with-splsbackup/
at 3/11/2008 7:35 PM

Nice

Nice script.  I am looking at using powershell to automate restoration of web application backups to a backup farm.  I cannot figure out how to pass the app pool id, site url, database server name, database name and database file path that I get prompted for during the restore.  How can I automate this?
at 5/28/2008 8:49 AM

Add Comment

Items on this list require content approval. Your submission will not appear in public views until approved by someone with proper rights. More information on content approval.

Title


Comments *


Name (required) *


Attachments
 
 
   
Real Time Web Analytics