Daves Occasional SharePoint Thoughts.....

Thinking about SharePoint....and some other dev stuff that floats me boat.

Performing a SharePoint site collection backup and restore from an unattached database using just PowerShell

Performing a SharePoint site collection backup and restore from an unattached database using just PowerShell

Rate This
  • Comments 1

Backing up and restoring a site collection using PowerShell is a pretty straight forward task and you can read the documentation for Backup-SPSite at http://technet.microsoft.com/en-us/library/ff607901(v=office.15).aspx

The unattached database restore feature which was introduced in SharePoint 2010 let's you access a copy of a database which you've restored to SQL (perhaps last night's backup) then use the Central Administration site to connect to that database and perform a backup of content (like a site collection backup for example). Articles such as http://www.mssharepointtips.com/tip.asp?id=1051&page=1 do a good job of walking you though the process.

But what if you want to complete the whole process using PowerShell?

The trick is to use Get-SPContentDatabase to get a reference to the restored unattached database and pipe it to Get-SPSite.

This example allows you to specify the name of an existing production site collection and have it restored from the unattached database back into production.

$restoredDatabase = "myRestoredDatabase"
$databaseServer = "devlab01"
$siteCollectionToRestore = "http://devlab01"
$backupLocation = "c:\temp\unattachedSite.bak"

$unattachedDB = Get-SPContentDatabase -ConnectAsUnattachedDatabase -DatabaseName $restoredDatabase -DatabaseServer $databaseServer
$productionSite = Get-SPSite $siteCollectionToRestore
$unattachedSite = $unattachedDB | Get-SPSite | ? { $_.Id –eq  $productionSite.Id }; See Notes
Backup-SPSite $unattachedSite -Path $backupLocation
Restore-SPSite $productionSite.Url -Path $backupLocation -Force -Confirm:$false


Why do we need to perform the "$unattachedSite = $unattachedDB | Get-SPSite | ? { $_.Id –eq  $productionSite.Id };" stuff for? The problem is that despite this being referred to as an "unattached" restore, the fact is the database IS attached to the central administration web app (however you can't actually browse to them). When the attaching takes place, the URL changes of course and whilst the site collection you want to restore might be at http://intranet/sites/myimportantsite by the time it's attached to central administration it's called http://myspserver:8081/sites/myimportantsite. Since the Id is a unique GUID which identifies the site collection in a content database we can find the Id of the project site collection and use this to locate the same site in the restored database.

Note also that once the site collection has been restored (on the top of the existing production one) it actually gets a new GUID. So whilst you can run the script above once and it will run correctly, running it again will fail because it can't find a match between the GUID in production and the restored database.


  • Dear Dave

    thank for your effort.

    But I am very much convinced that this script is not even working in the first place.

    Actually it generates a back up of the same site which is live on the production and then tried to restore it on the same production site. I  even tested it and it seems to be the case. Please correct me if I am wrong.

    Secondly this code wont work if the $siteCollectionToRestore has been deleted from the PROD accidently before and you want to get your site back from an Content database snapshot restored on to SQL Server.

    Backup-SPSite  does not give a possibility to get

    In addition to your script here is my script:

    $backupLocation = "E:\Backups\PMO\BackupfromOldDb\NewBackup3.bak"

    $restoredDatabase = "WSS_Content_Intranet_PMO_ProjectSites_Clone_03092015"

    $databaseServer = "SPSQLSERVER02\SPSQLDB02"

    Get-SPContentDatabase -ConnectAsUnattachedDatabase -DatabaseName $restoredDatabase -DatabaseServer $databaseServer | Get-SPSite -Limit 20 |

    ForEach-Object {

    #Be careful here!

    $SiteName = $_.Url.Substring(30)

    $Location = $backupLocation  + $SiteName + "_10092015.bak"

    #write-Output $_.url;

    Backup-SPSite  $_.Id -Path $Location


    I ran in to a trouble when one of the admins accidently deleted the much need sites from a webapplication in production. I already got an old snapshop of the content data base so I restored it to SQL server. Now I just needed to get the back up of missing site from this restored snap shot. Strangely via Central Admin everything seemed to be working fine via  "Recover data from an unattached content database" option but you cannot do the same using powershell. But you can do such a thing for 30 Site Collections.

    The info given over here is also useless


    I spent around 8 hours try to figure how to do that. I'm extremely against doing things manually but due to the time constraints I had to backup 30 site collections using central admin and then restored them using powershell.

    Please share your experience on how to handle such catastrophies.



Page 1 of 1 (1 items)
Leave a Comment
  • Please add 8 and 2 and type the answer here:
  • Post