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

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

How to backup a site collection in SharePoint 2016 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. Whereas PowerShell provides a quick and easy way to backup site collections. We can use scripted operations and avoid manual repetition of the 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 a 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 a 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.  When using this method, you don’t need to specify the -NoSiteLock parameter.

Backup Site Collection in SharePoint 2013 – PowerShell example:

For example: To backup the “IT” site collection at “https://intranet.crescent.com/sites/IT” to the “C:\Backups\” location, you would use the following PowerShell command:

 Backup-SPSite https://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 site collection backup 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. E.g., 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 also be extended to all site collections across all web applications on the farm!

PowerShell script to backup All SharePoint site collections

To backup a site in SharePoint Server, use the Backup-SPSite cmdlet:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Configuration parameters
$BackupLocation="D:\backups\"
$WebAppURL="https://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("https://","").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 reduce the impact on server performance.

For example, to restore the “IT.bak” file, use the following command:

Restore-SPSite https://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.

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

One thought on “How to Backup-Restore a Site Collection in SharePoint using PowerShell?

  • HI, I get an error while trying to get backup.Application Server job failed for service instance Microsoft.Office.Server.Search.Administration.SearchDataAccessServiceInstance (e7d19668-678b-4c4a-ae50-feedb4bac7aa).

    Reason: The trust relationship between the primary domain and the trusted domain failed.

    Technical Support Details:
    System.SystemException: The trust relationship between the primary domain and the trusted domain failed.

    at System.Security.Principal.SecurityIdentifier.TranslateToNTAccounts(IdentityReferenceCollection sourceSids, Boolean& someFailed)
    at System.Security.Principal.SecurityIdentifier.Translate(IdentityReferenceCollection sourceSids, Type targetType, Boolean forceSuccess)
    at System.Security.Principal.SecurityIdentifier.Translate(Type targetType)
    at Microsoft.SharePoint.Administration.SPAce`1.get_PrincipalName()
    at Microsoft.SharePoint.Administration.SPAcl`1.Add(String principalName, String displayName, SPIdentifierType identifierType, Byte[] identifier, T grantRightsMask, T denyRightsMask)

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *