Friday, 8 April 2011

How to back up SharePoint with Backup-SpFarm PowerShell command

The first you need to know is what components you can back up. You can list all SharePoint components available for the backup in the hierarchical tree format by running the following PowerShell command:

Backup-SPFarm -ShowTree

The result should be something like this:



OK, let's say I want to backup my web application on port 80 which was named SharePoint Intranet - 80 together with all components under this node:

Backup-SPFarm -BackupMethod Full -Item "Farm\Microsoft SharePoint Foundation Web Application\SharePoint Intranet - 80" -Directory "\\MySP2010Server\BackupDirectory"

For full description of all parameters see the Microsoft technet: http://technet.microsoft.com/en-us/library/ff607881.aspx

After backup is completed, go to Directory folder. There should be a new directory with many backup files and one spbackup.log file. If everything went well, the file should contain the following messages at the very end of the file:

[6/04/2011 10:25:12 PM] Finished with 0 warnings.
[6/04/2011 10:25:12 PM] Finished with 0 errors.
[6/04/2011 10:25:12 PM] Backup completed successfully.
[6/04/2011 10:25:13 PM] -------------------------------------------------

Now when we have PowerShell script which is working, next step is to create a scheduled task so you can run a back-up on regular basis. To do this, create a text file with PS1 extension and copy & paste the following line.

Add-PSSnapin Microsoft.SharePoint.PowerShell

The second line should the actual PowerShell back up script, in my case:

Backup-SPFarm -BackupMethod Full -Item "Farm\Microsoft SharePoint Foundation Web Application\SharePoint Intranet - 80" -Directory "\\MySP2010Server\BackupDirectory"

Now you're ready to go to Task Scheduler and create a backup scheduled task using the script file you just created.

The most common issues you're probably going to face are:
  • Operating system error code 3 (The system cannot find the path specified)
You'll run into this error if you specify the local path in Directory parameter (e.g. c:\Backup). The reason is that SharePoint delegates the content database backup to SQL Server which is probably located on different server and the back up directory doesn't exist on that server. That's why I use UNC path together with a share folder.
  • Operating system error code 5 (Access denied)
Actually this error is related to the previous error and this time the problem is permission related. As I said before a content database backup is delegated to SQL Server and it means that SQL Server (e.i. account running SQL Server service) needs to have the write/modify permission on the folder specified in Directory parameter. So check the folder permission and update it if needed.
  • Database permission denied error
The account you're using for the back up has to have backup rights . If it doesn't, the backup will fail. You can use either SharePoint farm account or you can create specific account just for this purpose.

3 comments:

  1. David,

    Truely excellent article. I was all hung up attempting to run a batch file from task scheduler that in turn ran the .ps1 powershell script, and it was failing due to unknown reasons.

    Tried your method of running .ps1 directly from scheduler and it is the trick! Thanks much!

    /Robin

    ReplyDelete
  2. Hi, David

    Thanks for the nice post. I have had problem with "Operating system error code 3". your sugguestion fixed the problem!


    Joy

    ReplyDelete
  3. Ditto. I have had the same issue with "Operating system error code 3".
    Your suggestion helped resolve it!
    Thanks

    ReplyDelete