Shahar Gvirtz's Weblog

PowerShell Script to backup SharePoint Site Collection

I wrote a small script that backup selected Windows SharePoint Services Site Collection and I publish it here. This is PowerShell script, that can be run manually or from scheduled task.

This is the script (you can download it, in the end of this post, if you can't see the full line):

   1:  #Backup Site Collection in Windows SharePoint Services Site Collection to file
   2:  #Author: Shahar Gvirtz
   3:  #Weblog: http://weblogs.asp.net/shahar
   4:   
   5:  param(
   6:  [string]$SiteCollectionUrl = $(throw "Please enter the URL of the Site Collection you want to backup"),
   7:  [string]$Path =$(throw "Please enter the folder you want to backup to")
   8:  )
   9:  if(test-path $path)
  10:  {
  11:      $guid = "\" + [Guid]::NewGuid().ToString()
  12:      & "$env:programfiles\Common Files\Microsoft Shared\web server extensions\12\BIN\stsadm.exe" `
  13:   -o backup -url $SiteCollectionUrl -filename $Path$guid.backup -overwrite > $null
  14:      [DateTime]::Now.ToString() +  ": Backup Done! File name is $path$guid.backup" >> "$path\log.txt"
  15:  }
  16:  else
  17:  {
  18:      write-error "The Path doesn't exists"
  19:  }
  20:   

This script get two parameters: the first one is the Site Collection url, and the second onw is the path to the folder where you want to save the backups.
If everything works fine, this script backup your site collection using stsadm.exe command line tool (line 13 is the continue of 12. there is a Back Tick operator in the end of line 12 which means that you only break the line).
After creating the backup, the script add new line to log file, with the current time and the name of the backup file.

Here is the syntax for using this script:

c:\backup.ps1 -SiteCollectionUrl "http://web:2020" -Path c:\backups

You must give an existing folder for the path parameter!
If you use it from the task scheduler, use this syntax (change the path and the site collection url):

powershell.exe c:\script\backup.ps1 -SiteCollectionUrl "http://web:2020" -Path c:\backups

You can download the full script, as txt file from here (change the file extension from txt to ps1 before using).

Shahar Gvirtz

Comments

Jess said:

Shahar, the txt download file come back with a 404 error.

# February 27, 2008 1:09 PM

Shahar Gvirtz said:

Here is the full script, Jess:

#Backup Site Collection in Windows SharePoint Services Site Collection to file

#Author: Shahar Gvirtz

#Weblog: http://weblogs.asp.net/shahar

param(  

[string]$SiteCollectionUrl = $(throw "Please enter the URL of the Site Collection you want to backup"),  

[string]$Path =$(throw "Please enter the folder you want to backup to")  

)  

if(test-path $path)  

{  

  $guid = "\" + [Guid]::NewGuid().ToString()  

  & "$env:programfiles\Common Files\Microsoft Shared\web server extensions\12\BIN\stsadm.exe" `  

  -o backup -url $SiteCollectionUrl -filename $Path$guid.backup -overwrite > $null  

  [DateTime]::Now.ToString() +  ": Backup Done! File name is $path$guid.backup" >> "$path\log.txt"  

}  

else  

{  

  write-error "The Path doesn't exists"  

}  

# February 27, 2008 11:01 PM

Rodolfo said:

Dear Shahar

I hope you are well I tried to run your script but I am getting parameter errors. Could you kindly send it to me via email rsvacchi@gmail.com

Many thnaks

Rodolfo

# April 27, 2010 2:46 PM

westerdaled2 said:

Out of interest why aren't you using the SharePoint 2010 cmdlets? I am in the processes of editing my profile to load these automatically.

Daniel

# January 25, 2011 10:15 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)