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
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.
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
#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.
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.