kwizcom banner advertisement

How to Backup-Restore a Site Collection in SharePoint using PowerShell

Requirement: Backup and restore a site collection in SharePoint 2013 using PowerShell.

How to backup a site collection in SharePoint 2013 using PowerShell?
Although you can backup site collections from SharePoint Central Administration, as in  How to Backup SharePoint Site Collection using Central Admin, the Central admin way is kind of clunky and takes a while to perform the backup. PowerShell provides a quick and easy way to backup site collections, We can use in scripted operations and avoid manual repetition of same tasks such as performing backups. Use SharePoint 2016 Management Shell to run these cmdlets.

SharePoint site collection backups may be initiated from SharePoint WFEs and you need to be member of a local administrator on the server and should be a farm admin.

Backup SharePoint Site Collection using PowerShell
Here is the PowerShell syntax to take backup of a site collection in SharePoint 2016. It takes two mandatory parameters to identify the site collection to backup and the path to save the backup file:

Backup-SPSite -Identity <Site-Collection-URL> -Path <Backup-File-Path> [-Force] [-NoSiteLock] [-UseSqlSnapshot]
  • Force -  overwrites backup file with the same name.
  • NoSiteLock - prevents the site from going to Read Only mode while the backup is being taken.  However, if someone updates the site while backup is being created, it might lead to data corruption.
  • UseSQLSnapshot - A database snapshot will be taken before the backup begins and the backup will be done off the snapshot. The advantage is that changes can be made to the site while the backup process is running without fear of corruption. The snapshot will be deleted automatically when the backup is completed.  You don't need to specify the -NoSiteLock parameter when using this method
Backup Site Collection in SharePoint 2013 - PowerShell example:
For example: To backup the "IT" site collection at "http://intranet.crescent.com/sites/IT" to "C:\Backups\" location, you would use the following PowerShell command:
 Backup-SPSite http://intranet.crescent.com/sites/IT -path d:\backup\it.bak
backup a site collection in sharepoint 2016 using powershell
The Backup-SPSite cmdlet backs up everything in the site collection including documents, pages, alerts, workflows, etc. and produces a backup of a site collection with .bak extension.
restore site collection sharepoint 2013 powershell example

Backup All Sites in a Web Application:
PowerShell provides us the ability to automate repeated tasks with less administrative effort. Say for for example, if you need to back up all site collections from a web application, it would be cumbersome to run the same command so many times. Instead, you can use the following PowerShell to loop through each site collection in the web application and back each one up. This can be extended to all site collections across all web applications in the farm as well!

PowerShell script to backup All SharePoint site collections
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Configuration parameters
$BackupLocation="D:\backups\"
$WebAppURL="http:\\company.sharepoint.com"

#Get All site collections under the given web app
$SitesColl= Get-SPWebApplication $WebAppURL | Get-SPSite -limit ALL

#Iterate through each site collection
Foreach($Site in $SitesColl)
{
   #Backup the site collection 
   $FilePath = $BackupLocation + $Site.Url.Replace("http://","").Replace("/","-") + ".bak"
   Backup-SPSite -Identity $Site.Url -Path $FilePath -Force 
   Write-Host "Backup completed Successfully for $($Site.Url)"
}


Restore site collection backup in SharePoint 2016 using PowerShell:
How to restore site collection in SharePoint 2013 using PowerShell? The restoration procedure is similar to the backup process. The Restore-SPSite command requires the site collection Identity and backup file path parameters. Here is the basic syntax for restoring site collections in SharePoint:

Restore-SPSite -Identity <Site-Collection-URL> -Path <Backup-File-Path> [-DatabaseServer] [-DatabaseName] [-Force] [-GradualDelete]
  • GradualDelete - When you overwrite an existing site collection by restore, GradualDelete parameter deletes the site collection gradually over time to reduces the impact on server performance.
For example, to restore the it.bak file, use the following command:
Restore-SPSite http://portal.crescent.com/sites/IT -path c:\backup\it.bak

This will restore the provided BAK file into a new site collection. The site collection will be created automatically.

In order to overwrite an existing site collection from the backup file, use -Force parameter.

Please note, To restore the site collection there are some limitations:
  • A content database can contain only one copy of a given site collection. So, If you want to restore your backup to the same web application from which it was backed up, you need to either delete the original site collection or create a new content database into which the backup can be stored.
  • The site collection backup can be restored to either the same farm or a different farm, as long as the destination farm is the same build or later as the source farm.
How to Backup-Restore a Site Collection in SharePoint using PowerShell How to Backup-Restore a Site Collection in SharePoint using PowerShell Reviewed by Salaudeen Rajack on May 15, 2017 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.