Sharepoint 2010 backup with Powershell

Always one for trying to get things automated then I wanted to get backups of my new Sharepoint 2010 website automated. Previously I have not had a great experience of this, as I never tried a restore of the backup set before it was time…still we live and learn. In previous versions of Sharepoint backups were taken care of through the use of STSADM however this is not the case in the latest version. You can, as always, run a manual backup through the central administration console on the server but this is not what I needed. So I started searching around and found a couple of articles on the web which described the use of the Powershell to perform backups, so here is what I have come up with through the combination of the two sources:


Add-PsSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue
try
{
  $today = (Get-Date -Format dd-MM-yyyy)
# Created the a new directory with todays date and store in a variable
  [IO.Directory]::CreateDirectory("F:sp_backup"+$today)
  $backuppath = "F:sp_backup"+$today

# This will iterate through all of your sharepoint sites and create a backup with the site URL as the name 
  Get-SPWebApplication | Get-SPSite | ForEach-Object{$filepath = $backuppath + "" + $_.Url.Replace("http://","").Replace("/","-").Replace(":","-") + ".bak" ; Backup-SPSite -Identity $_.Url -Path $filepath}

# Edit this address to suit your own needs 
  $emailFrom = “Sharepoint@server.local

# This is the address that the notification email should be sent to 
  $emailTo = “administrator@server.local

# Subject for the notification email. The + “$today” part will add the date in the subject.
  $subject = "The SharePoint sites Backup was Successful for "+"$today"

# Body or the notification email. The + “$today” part will add the date in the subject.
  $body = "The SharePoint sites Backup was Successful for "+"$today"

  # IP address of your SMTP server. Make sure relay Is enabled for the SharePoint server on your SMTP server
  $smtpServer = "1.1.1.1"
  $smtp = new-object Net.Mail.SmtpClient($smtpServer)
  $smtp.Send($emailFrom, $emailTo, $subject, $body)
}
Catch
{
  $ErrorMessage = $_.Exception.Message
  # Configure the below parameters as per the above.
  $emailFrom = “Sharepoint@server.local
  $emailTo = “administrator@server.local
  $subject = "The SharePoint sites Backup Job failed on "+"$today"
  $body = "The SharePoint sites Backup Job failed on "+"$today and the reason for failure was $ErrorMessage."
  $smtpServer = "1.1.1.1"
  $smtp = new-object Net.Mail.SmtpClient($smtpServer)
  $smtp.Send($emailFrom, $emailTo, $subject, $body)
}

 

This script can simply be copied into notepad and saved onto your server as a “.ps1” file. This can then be run straight from the servers “run” command by entering “powershell {scriptname}.ps1” and it will create a backup file, this in turn can be used to created a scheduled task on your server to be run each day/week etc.

Credit where due though, this script is a combination of the following 2 sources:

http://thecommunicator.co.cc/2010/12/07/automated-powershell-script-to-backup-sharepoint-farm-or-site-collection-with-email-notification/

http://www.toddklindt.com/blog/Lists/Posts/Post.aspx?List=56f96349-3bb6-4087-94f4-7f95ff4ca81f&ID=191&Web=48e6fdd1-17db-4543-b2f9-6fc7185484fc

Hopefully this will help others out there Smile

– Rob

Rob Written by:

Be First to Comment

Leave a Reply