Copying Site Collections in SharePoint 2013 is trivial, except if you are trying to copy a Host Named Site Collection with multiple SPSiteUrls.
Copying Site Collections in SharePoint 2013
There are many different reasons for copying Site Collections that vary from having a head start for building a new Site Collection to having a separate place to test things without disturbing operation. Probably the easiest way to copy a Site Collection in SharePoint 2013 is by using the Copy-SPSite PowerShell cmdlet.
The Copy-SPSite cmdlet is flexible and allows you to specify the new URL for your Site Collection, to which Content Database it should be copied and whether it should be a Path-based or a Host Named Site Collection.
Host Named Site Collections
One of the new capabilities of SharePoint 2013 are Host Named Site Collections. As the name says, Host Named Site Collections allow you to associate a host name with a Site Collection. Comparably, in the past you would need a separate Web Application to achieve the same. As you can imagine this offers significant benefits from the scalability perspective.
One great property of Host Named Site Collections are SPSiteUrls. When working with Web Applications you can define multiple Zones: each with its own authentication scheme and URL. With SPSiteUrls you can leverage all of that configuration in Host Named Site Collections by defining additional URLs and associating them with corresponding Web Application Zones.
Although SPSiteUrls are without a doubt a great addition to Host Named Site Collections using them comes with a price.
Inconvenient copying Host Named Site Collections with multiple SPSiteUrls
If you try to copy a Host Named Site Collection, you will get the following exception:
Another site already exists at [URL]. Delete this site before attempting to create a new site with the same URL, choose a new URL, or create a new inclusion at the path you originally specified.
Please note that you will get this exception no matter how many SPSiteUrls the Host Named Site Collection has defined.
If you try an alternative approach with the Backup- and Restore-SPSite cmdlets, you will get another exception:
Violation of PRIMARY KEY constraint ‘PK_SiteUrlMap’. Cannot insert duplicate key in object ‘dbo.SiteUrlMap’. The duplicate key value is (0x, [URL], 2).
This time the exception is clear about the reason of the failure: the specified SPSiteUrl is already defined and you are not allowed to add it to the database once again. So what are the alternatives?
Option #1: remove all SPSiteUrls
The first option that you have is to remove all SPSiteUrls before performing the backup of the Site Collection. After having restored the site you would add the SPSiteUrls back and optionally would add some new SPSiteUrls to the copied Site Collection.
Although this approach is pretty straight-forward it has one serious disadvantage: by removing the additional SPSiteUrls from the original Site Collection you’re interrupting its operability and prevent some users from accessing it. And although this might be okay in some cases, there might be scenarios where this is simply not an option.
Option #2: remove all SPSiteUrls after the backup
An alternative approach would be to take a backup of the Site Collection, restore it in another Farm of exactly the same patch level, remove all SPSiteUrls there and then restore it back to the original Farm.
In this case no service interruption occurs in the original Site Collection. The only downside is the need for a separate Farm with exactly the same patch level as the original Farm where you can restore the Site Collection from the backup, remove all SPSiteUrls and create a new backup to restore to the original Farm.
Copying Site Collections in SharePoint 2013 can be easily done using the Copy-SPSite cmdlet. Unfortunately this cmdlet works only with Path-based Site Collections. Host Named Site Collections can be copied using the Backup- and Restore-SPSite cmdlets. Some additional work is required if the Host Named Site Collection has multiple SPSiteUrls associated.