Team blog of MCS @ Middle East and Africa

This blog is created by Microsoft MEA HQ near shoring team, and it aims to share knowledge with the IT community.With its infrastructure and development sides,It brings to you the proven best practices and real world experiences from Subject Matter Experts
Follow Us On Twitter! Subscribe To Our Blog! Contact Us

SharePoint 2013 Backup with PowerShell and Task Scheduler for beginners

SharePoint 2013 Backup with PowerShell and Task Scheduler for beginners

  • Comments 22
  • Likes

You can do Everything with PowerShell - yes everything - and SharePoint is no exception. In this post for beginners, I will show you how to create tasks for the Task Scheduler to automate your SharePoint backups with PowerShell.

First, to backup your farm, you can use the PowerShell command Backup-SPFarm. For more details on this cmdlet, refer to http://technet.microsoft.com/en-us/library/ff607881.aspx. You can use the command to backup the farm a full backup as in the following example.

Backup-SPFarm -BackupMethod Full -Directory \\MyServer\SPBackups

And to backup the farm a differential backup, you can use the command as in the following example

Backup-SPFarm -BackupMethod Differential -Directory \\MyServer\SPBackups

To create a PowerShell script file, open a notepad file and save it as *.ps1. Let's create two files - one for the full backup and another for the diff backup.

So create two files: SPFarmFullBackup.ps1 and SPFarmDiffBackup.ps1

In the first file for the full backup, type the following text.

Add-PSSnapin "Microsoft.SharePoint.PowerShell"

Set-ExecutionPolicy -ExecutionPolicy "Unrestricted" -Force

Backup-SPFarm -BackupMethod Full -Directory \\MyServer\SPBackups

Save the file.

In the second file for the differential backup, type the following text.

Add-PSSnapin "Microsoft.SharePoint.PowerShell"

Set-ExecutionPolicy -ExecutionPolicy "Unrestricted" -Force

Backup-SPFarm -BackupMethod Differential-Directory \\MyServer\SPBackups

Save the file.

Now, follow the steps below to create the tasks for Task Scheduler to automate the execution of backup files - let's assume we will full-backup weekly and diff-backup daily.

1 - Open Tools > Tasks Scheduler

2 - In the center pane, right-click, and click Create Basic Task

3 - Assign the task a meaningful name - such as SharePoint-BackupFullWeekly

4 - Choose "Weekly " on the When do you want the task to start screen

5 -  Specify the parameter for the weekly task schedule

6 -  Choose "Start a program" from the what action do you want the task to perform

7 -  On the start a program screen type in the command as the screenshot below. The Task scheduler is intelligent enough to recognize that you want to run PowerShell and that you supplied arguments.

Repeat the steps above to create a task for the diff backup.

Remarks:

Good Luck!

 

Comments
  • Thanks Yousef!  Just what I was looking for today.

  • Glad you liked the post Mark!

  • Thanks a lot. this helped out greatly! Exactly what I was looking for!

  • Nice article - just the job!

    Space needed between "Differential-Directory"

    Thanks

  • Thanks Gary. fixed it. :)

  • It's working, but the search function gives errors

  • What about cleanup on backupdestination?

  • Hi Soren

    Try this an d save as ps1

    # Location of spbrtoc.xml

    $spbrtoc = "C:\Backups\spbrtoc.xml"

    # Days of backup that will be remaining after backup cleanup.

    $days = 7

    # Import the Sharepoint backup report xml file

    [xml]$sp = gc $spbrtoc

    # Find the old backups in spbrtoc.xml

    $old = $sp.SPBackupRestoreHistory.SPHistoryObject |

    ? { $_.SPStartTime -lt ((get-date).adddays(-$days)) }

    if ($old -eq $Null) { write-host "No reports of backups older than $days days found in spbrtoc.xml.`nspbrtoc.xml isn't changed and no files are removed.`n" ; break}

    # Delete the old backups from the Sharepoint backup report xml file

    $old | % { $sp.SPBackupRestoreHistory.RemoveChild($_) }

    # Delete the physical folders in which the old backups were located

    $old | % { Remove-Item $_.SPBackupDirectory -Recurse }

    # Save the new Sharepoint backup report xml file

    $sp.Save($spbrtoc)

    Write-host "Backup(s) entries older than $days days are removed from spbrtoc.xml and harddisc."

  • great post

  • Thx

  • Great post. thanks

  • Thank you!

  • This is great stuff Yousef!! I am new to SP so this is coming in handy.

    Quick question, there is the SP server and the SQL server with the DB where all the SP data lives, correct?

    So with this command you backup everything I need to rebuild the environment incase I loose both the SP and the SQL server?

    If so, would you be able to provide the restore script as well? Please? :-)

    Many thanks!

  • Great post, Yousef.

  • Thanks. Very Usefull article.

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment