PowerShell and Scheduled Backup Runs

One of my good friends had asked me about this, when she got very frustrated trying to run a scheduled backup job on a site collection using PowerShell scripts. I thought I would add a quick note here on this.

PowerShell is a different beast from the normal command line utilities that used to ship with MOSS 2007 and the earlier versions. It is a beast not because it is very complicated. It is a beast because it is extremely powerful. Microsoft has done a good job of providing an extensible PowerShell module for SharePoint 2010 which will not only allow us to execute the out-of-the-box snapins and accomplish complex administration tasks but also build our own snap-ins. However, with great power comes greater frustrations (in the computer administrator’s world that is).

So here is a, hopefully, helpful post for not only my dear friend, but also to the wider SharePoint administration community.

If you are interested in an already written script, download the attached PS1 script file and start using it. If you wish to know what is happening behind the scenes, read on.

The first thing that the script does is to ensure that the Microsoft.SharePoint.PowerShell is loaded as a PS snap-in.  Once loaded, the script then goes on to execute the Backup-SPSite command-let to backup the site to the provided location.

The script accepts 2 parameters:

-Site – The site which needs to be backed-up

-Path – The full path to the backup file – which will be overwritten each time this script is run.

To schedule this script to execute as a daily job, setup a schedule in Task Scheduler remembering to do the following:

  • The account used to run the task must have farm and local administration privileges.
  • The account must run in “Highest privilege” mode
  • The task should be set to run “whether the user is logged on or not”

When setting up the “Actions”:

  • Ensure that for each action the program or script to run should be set to Powershell.exe. You may browse to the location of powershell.exe and provide the full path to it if required.
  • In “Add Arguments” use the following format: <<path_to_the_script_file> –Site <<url_to_the_site>> –Path <<Full_Path_To_The_Backup_File>>
  • Please note that the script uses the –Force parameter with Backup-SPSite – which means the file will be overwritten each time the task is run. You may take it out if you so require.

That is it. To my dear friend out there, Good luck! and let me know how it goes.

For security reasons I cannot attach the PS1 script file to the post, however here is the script itself: