Restore from unattached content database


This is a great way to restore data in SharePoint. Using powershell you can connect to an unattached content database that you restored from a database backup. Then export the site, web or list you want to restore from to the same site collection or web you want to place the items in.

This gives the posibility of doing granular restores in SharePoint which I found difficult or impossible in other tools.

 

I put the following script in a ps1 file and run it from the SharePoint Management Shell:
 
param (
   [string]$dbName = $(Read-Host -prompt “Name of content database”),
   [string]$dbServer = $(Read-Host -prompt “Database server name”),
   [string]$siteUrl = $(Read-Host -prompt “URL of site to restore (i.e. /sites/mytestsite)”),
   [string]$webUrl = $(Read-Host -prompt “URL of web to restore (i.e. Mysubweb/level2. Leave empty to restore site collection)”),
   [string]$exportPath = $(Read-Host -prompt “Path and filename for export file (use .cmp as extension)”),
   [string]$importUrl = $(Read-Host -prompt “URL to import site or web to. Note: this web must be created using the same template as the exported web”)
)
 
$db = Get-SPContentDatabase -ConnectAsUnattachedDatabase -DatabaseName $dbName -DatabaseServer $dbServer
$site = $db.Sites[$siteUrl]
$web = $site.RootWeb
 
if($webUrl -ne “”)
{
   $web = $site.AllWebs[$webUrl]
}
 
Export-SPWeb $web -Path $exportPath -IncludeUserSecurity -IncludeVersions All -NoFileCompression
Import-SPWeb $importUrl -Path $exportPath -IncludeUserSecurity -NoFileCompression