Automating Site Collection Backups with PowerShell

A script for automating site collection backups via STSADM commands was included in the previous version of this book. This script was one of the most asked-for scripts from the book because it enabled administrators to back up individual site collections to flat files on a nightly basis. Although useful, this script was a four-page Visual Basic script and had some complex logic in it that would enumerate site collections and back them up individually.

SharePoint 2010 now allows for the same functionality, from a simplified interface. You can completely automate the backup procedure with built-in SharePoint PowerShell cmdlets. The following sample script demonstrates some additional things than can be done with a combination of a couple of built-in scripts.

The script enables you to do the following:

• Back up all site collections in your server farm.

• The backup filename will be a combination of the site collection name and date when the backup was created.

• The user running this script can specify the number of previous backups they want to retain.

• When the backup finishes, a notification will be sent to a specified email address.

Here is the script listing:

image

image

image

You can execute this script by using the following code snippets:

image

The preceding three samples show you how you can execute your script in various scenarios. A backup script has six parameters:

SPSiteID— GUID that uniquely identifies a site collection. In the previous samples, we pass the ID value from the ForEach-Object loop.

BackupFolder— Backup folder on a local drive. Leave the trailing backslash . In case this folder does not exist, it will be automatically created.

RootWeb— Value of site collection RootWeb property. The value being used forms the backup filename—for example, RootWeb-BackupTime.dat.

BackupFilesLimit— The number of previous backup files to retain at BackupFolder location. In case this number is five, only five last backups will be left in the backup folder, and all previous files will be deleted.

Email— The email value used as a To and From email address. This value is optional. If value is not provided, a notification email will not be sent.

SmtpServer— The address of the SMTP server to send the notification email to. If this value is not provided, an email will not be sent.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset