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
   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:  }

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 have been disabled for this content.