Backup and Restore in SharePoint 2010 using PowerShell
It is essential to have proper backup/restore strategy in place for your SharePoint farms. SharePoint 2010 provides three types of tools for backup/restore for your server farm. The tools are
Central Administration
Windows PowerShell
Stsadm.exe
You need to carefully decide what you are going to backing up and where you are storing these backups. Be noted that SharePoint 2010 doesn’t provide a method to backup directly to tape. SharePoint 2010 backs up to a UNC file share. Technically UNC file share can be anywhere, but it is recommended that you take your backup to a UNC file share in your LAN.
Let me explore each tool available for backup.
Central Administration: You cannot schedule a backup via Central Administration. The primary use of Central Administration backups is to verify the configuration of the target backup location and to back up server farms before major operations. It is recommended to take backup using Central Administration, once you create all web applications and site collections required by your server farm. Also take one backup using Central Administration before any major upgrade of the farm.
Windows PowerShell: The preferred way to take backups in SharePoint Server 2010 is by using windows PowerShell. This method gives you the most options and will be fully supported in the future. You can also schedule the backup using Windows PowerShell.
Stsadm.exe: Stsadm was the command line administration tool available for SharePoint 2007. SharePoint 2010 also provides Stsadm, but it is recommended to use PowerShell as Stsadm is there to offer backward compatibility. Many of the Stsadm commands implemented in SharePoint Server 2010 existed in SharePoint Server 2007. Configuring command-line backups using Stsadm is easy, but it is limited to basic farm backup functionality
In this article I am going to demonstrate the PowerShell commandlets to take various types of backup from SharePoint 2010.
First you need to decide, where you are going to store the backups. Make a UNC share for that location. In order to back up to file share, the following accounts need full permissions to the backup location.
- SQL Server service account
- SharePoint 2010 Timer Log On account
- User account, who is executing the command.
Also you have to ensure that you have enough disk space available for the backup. In the remaining section of this article details the various backup options available to you.
Farm Backup
To back up a server farm using Windows PowerShell, you can use Backup-SPFarm command. The syntax of the command is as follows.
Backup-SPFarm -directory <UNC File Share> -BackupMethod <Full | Differential>
The Backup-SPFarm supports the following options
- Directory - Defines the backup location previously prepared.
- BackupThreads - The default thread count is 3. Increasing the thread count will result in faster backup, but it will affect the server performance during backup. Also increasing the threads will make it difficult to check backup logs.
- ShowTree - Displays the objects in the farm those are available to be backed up. If you use this setting in conjunction with Item, it is scoped to the object underneath Item.
- ConfigurationOnly – This option specifies to backup only the farm configuration data. No content will be backed up when you specify ConfigurationOnly in the command.
- Item – You can back up a farm component—such as a Web application, content database, or service application—using the Item parameter. You cannot back up site collections, sites, list, libraries, or list items using the Item parameter.
- Percentage - You can specify the progress increments on the screen. If scripting, you do not need to use the Percentage option.
- Force - Using force will bypass the default behavior of halting the backup if the estimator calculates there is insufficient disk space.
- Verbose – This option will display the progress of the backup in the command line. If this is not presented, you won’t be presented with current progress information.
- WhatIf - This is a common PowerShell feature, if you specify this option, the command will display the output of running the backup without actually performing the backup.
Restore a Server Farm
The backups taken by PowerShell can be restored by Central Administration. Also it is possible to restore the farm using the PowerShell Script. To restore a farm from backup you can use Restore-SPFarm commandlet.
The UNC share where you configured your backup, may contains more than one backup. So the PowerShell command for restore requires you to specify the backup ID for the backup to restore. No problem, there is a command available to list all the backups available under a UNC share. The command is as follows.
Get-SPBackupHistory -Directory <Backup folder> -ShowBackup [-Verbose]
See the output of the command.
There is another way to find the backup ID, in the root folder of the UNC share used for backup; you can find an xml file named spbrtoc.xml (SharePoint Backup Restore Table Of Contents). To find the ID, open the UNC share in windows explorer, open the spbrtoc.xml in any text editor (e.g. notepad) and you can find all backups with IDs]
The syntax for Restore-SPFarm command is as follows
Restore-SPFarm -Directory UNCShare -BackupId <id of backup>
The Restore-SPFarm supports the following options.
- Directory - specifies the path to the SharePoint Server 2010 backup location you want to restore from. There must be a valid spbrtoc.xml file in the root of the backup directory.
- RestoreMethod – Either New or Overwrite. The New option restores the selected backup components and configuration using either new database names or a new SQL Server instance, or both. The Overwrite option restores the content and configuration to the same database instances.
- BackupId - The unique backup ID you want to restore.
- ConfigurationOnly - Restores only the farm configuration data, it will not restore any content.
- Confirm - Requires you to confirm the backup by typing Y, it is just a precautionary option.
- FarmCredentials - You must supply the farm credentials when restoring content and configuration data.
- Force – if you want to restore to the existing farm, use this option.
- Item - Indicates the part of the backup you want to restore. For example, if you have performed a full farm backup, you might choose to only restore a Web application or content database.
- NewDatabaseServer - If you want to restore to an alternate database server, you can specify that during the restore process.
- Percentage - You can specify the progress increments on the screen. Percentage complete is seen only when using the Verbose option.
- RestoreThreads - The default number of process threads is 3. This can be increased to 10 or decreased to 1.
- WhatIf - Displays the effect of a command without running an actual restore process.
- Verbose - Displays the output of a command.
Backup web applications using PowerShell
You can use the –item option to the Backup-SPFarm command to back up a particular component in a farm. By specifying the item option with the Backup-SPFarm command, you can backup a web application, a service application or a content database. Also using the Restore-SPFarm command, you can restore any of these.
You can also restore individual items from a full back up without restoring the entire farm backup. So if you are taking full farm backup, it is not necessary to take item wise backups separately.
In order to backup an item, you need to specify the item name you need to backup. To retrieve the item names for backing up, you can use the following command.
Backup-SPFarm –showtree
This command will list the hierarchical items available in the farm. You can retrieve the item name by following the command output.
See the output of the command
To backup a particular item, you can
Backup-spfarm –directory <uncpath> -item “Hierarchical item name” –backupmethod FULL |Differential
e.g.:
Backup-spfarm –directory \\ServerName\UNCShare -item “Farm\ Microsoft SharePoint Foundation Web Application\SharePoint - 2038” –backupmethod FULL
Similarly you can Restore a particular item from a farm backup. See the sample command
Restore-SPFarm –directory <uncpath> -backupid <backup id> -item <item to be backed up>
Backup Site Collection
Sharepoint 2010 supports granular backup and restore. You can backup/restore site collections, individual sites and lists. In this section I am going to cover only the site collection backup.
Windows PowerShell will automatically lock the site collection during backup to reduce the risk of SQL blocking and locking errors. Also it is possible to backup site collection using a SQL snapshot to back up the data. To backup site collections you can use the command Backup-SPSite. The following are the options available for Backup-SPSite.
- Identity - specifies the URL or GUID of the site collection to back up
- Path -The full backup path, including the file name. Here you can use the file path, it is not necessary to specify the UNC path here.
- Confirm - Requires you to confirm the backup by typing Y.
- Force - If you want to overwrite an existing back up file, use the Force option.
- NoSiteLock - If you want to back up the site collection without making it read-only, use the NoSiteLock option. If you are taking large site collection backups, without a site lock, the back up may fail.
- UseSqlSnapshot - If you need to back up site collections during the day and require users to continue full read/write operations, you’ll need to create a SQL snapshot to back up from. Using this option creates a temporary SQL Server database snapshot. After the snapshot is created, the backup will be from the snapshot, not the live database. This is the best method to get a full fidelity site collection backup.
- WhatIf Shows the effects of a command without performing the actual backup
E.G.: Backup-spsite –identity http://win-hgdsnnuakhv/sites/testsite -path c:\sitebkup\mysite.bak
Schedule backups
Once the server farm is up and running, you need to take regular backups. You need to automate the server backup procedure. You need to decide your backup plan. This depends on your farm. You need to carefully decide the backup plan. Also test your back up by restoring it to a test environment, for making sure you are ready for disaster recovery.
Providing a recommendation for backup farm is depends on the implementation. Recently I worked with a farm, where I have 3 site collections in 3 different web applications. I have decided the following backup sets.
- Monthly full backup for the entire server farm
- Weekly differential backup for the entire server farm
- Weekly full backup for each site collection
- Daily differential backup for each site collection
Make sure you are taking the decision by carefully considering your back up plans.
Next you need to schedule the scripts. You need to do the following tasks for scheduling the back up.
- Create a .ps1 file with your PowerShell commands
- Create a batch file that calls the ps1 file
- Configure the batch file to execute in certain interval.
The following steps demonstrate scheduling a full farm backup using PowerShell. First create a Windows PowerShell script file with the following contents.
Add-PSSnapin Microsoft.SharePoint.PowerShell
Backup-SPFarm -directory \\win-hgdsnnuakhv\farmbkup -backupmethod FULL
Save this file with extension .ps1. I have named the file as farmfullbkup.ps1.
Now create a batch file, which calls this PowerShell Script. The batch file content is as follows.
PowerShell -command C:\BackupScheduleScripts\FarmFullBkup.ps1
Save this file with extension .bat. In my case, I named the file as FarmFullBkup.bat
Now you can schedule this batch file as you required using Windows task scheduler. In my case I configured this to execute once in a month.
Conclusion
SharePoint provides lot of options for backup/ restore, from a full farm level to list level. Planning for backup / restore in very important in any server farm and you need to carefully decide what backup / restore strategy will help you.