SharePoint 2013 Backup with PowerShell and Task Scheduler for beginners


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 (41)

  1. Anonymous says:

    Thank you!!!

  2. Anonymous says:

    Greetings all;
    Someone posted a script above which I just found out has a bug in it. Here is the correct script:

    # Location of spbrtoc.xml
    $spbrtoc = “\znj1pdbapp01E$SQLDumpsSharepointbackupspbrtoc.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 |
    ? { [DateTime]::Parse($_.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.”

    The original script was taking the system date – Days to determine if a backup was older than the $days old. However I discovered that it was doing a string compare so when the calendar crossed over to a new year the current backup was being deleted along with the oldest. By converting the SPStartTime to DateTime this bug is resolved.

    HNY;
    Kurt Zimmerman
    Sr. DBA
    Lefrak Organization

  3. Anonymous says:

    Glad you liked the post Mark!

  4. tnielsen@os.dk says:

    Thank you for the tip. I will for sure try that on my new Sharepoint Server.

  5. Anonymous says:

    Great post, Yousef.

  6. Anonymous says:

    Thanks Gary. fixed it. 🙂

  7. Anonymous says:

    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!

  8. Mike G - Seattle, WA, USA says:

    Thanks Yousef!  Just what I was looking for today.

  9. Larry H - Saint Louis, MO, USA says:

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

  10. Gary says:

    Nice article – just the job!

    Space needed between "Differential-Directory"

    Thanks

  11. ITer says:

    It's working, but the search function gives errors

  12. Soren says:

    What about cleanup on backupdestination?

  13. Sean says:

    Hi Soren

    Try this an d save as ps1

    # Location of spbrtoc.xml

    $spbrtoc = "C:Backupsspbrtoc.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."

  14. narasimha says:

    great post

  15. thomas says:

    Great post. thanks

  16. Eren says:

    Thanks. Very Usefull article.

  17. Jan Hrachovsky says:

    nice. thanks

  18. SuzeB says:

    Great post. Only thing I have to add is that I needed to change the properties on the task to run whether or not the farmadmin account was logged in, and to run under highest privileges (had to supply password). Other than that, this works great! Thanks!

  19. Rikki Steele says:

    Awesome post thanks for your time

  20. Chris624 says:

    Great script – thanks. Our SharePoint databases are in full recovery mode and the transaction logs have been growing over the months (understandably!). Is there a best practice for doing a transaction log backup in conjunction with this script or should
    I just do this within SQL?

  21. Lila says:

    Hi Sean,

    Thanks, cleanup on backup destination script works perfectly, but it keeps the old folders spbrxxx . each old spbrxxx folder contains same txt file named spbackup. do you have an idea on how to delete all those old folders.

  22. Qasim AlQari says:

    Great Post ..
    Thanks aloot ..
    You save my time ..

  23. Anonymous says:

    In general , you need to backup the whole farm through the SharePoint UI and/or the SQL databases .

  24. Deepa says:

    thanks, I found it very useful and precise.

  25. mike says:

    Thank you gentlemen, you have saved me much time, may you be blessed. Please keep on providing this kind of directly useful information.

  26. Ida says:

    Great post! Since I’m new to SP this was perfect.

  27. Marceli says:

    3 Lines Backup-Script: that’s great.
    The Backup seems to be uncompressed: Compression of a full backup (1.49 GB) took only 219 MB with 7zip.
    Compression should be built-in (but it’s still PoorShell instead of PowerShell – compared to #!/bin/sh)

  28. Virat kamboj says:

    Thank you Yousef

  29. backupoffice365 says:

    Cloud to cloud backup for leading online SaaS application. To get the command over the cloud productivity to your business of any size, and also want to save money and time then only office 365 provides all these features. For further information visit:
    http://www.cloudally.com/office-365-backup/?refId=ESA

  30. backupoffice says:

    In this blog, the easiest way of creation of tasks explains with the help of task scheduler.Cloud backup is for leading online SaaS application. For further information here’s the link
    http://www.cloudally.com/salesforce-backup/

  31. Gastón says:

    What happen if someone cancell, in any way, the script while it’s still running Backup-SPFarm?

  32. Taxiaros says:

    Hello,

    In case we want to keep multiple backups is there any way to put some variables in order the backupfile to take the current date, so will not replace the previous files?

    Thanks.

  33. Marty says:

    Great post – thanks! I managed to get this working perfectly. However, if I want to continue managing my SQL Server backups with a SQL Server Agent job for consistency, how do I know which databases I would back up there, and which I would back up with
    a PowerShell command? How do I specify what needs to be backed up via PowerShell if certain things are already backed up with SQL Server Agent?

  34. bhashwar bhattarai says:

    Hello Yousef,
    Great article. Does this overwrite the existing backup or keep adding the new ones? If it add new ones, how do you make it to overwrite it?

  35. FT2000 says:

    Thanks Bud! Awesome post. Greatly appreciated. Blessings
    FT

  36. Jacky says:

    it is really helpful.
    Thanks

  37. Sean says:

    Sweet works like a dream , thanks.