I needed to set up a new web application that was a copy of an existing web application. The constraint was that there was only one farm available to work with (I did have other farms but they were running different versions of SQL server).
Approach 1
I’ll first point out that this didn’t work, skip ahead if you’re not interested.
I went through the following steps:
- perform a SQL backup of the content database
- restore the backup into a new database
- created a new web application
- marked the content database that this created as offline
- attached the cloned database to the web application with the powershell command Mount-SPContentDatabase – I was sure to pass in the -AssignNewDatabaseId parameter
The result was the database attached but it had no site collections in it.
It turns out that this approach could work but only if you attach it to a different farm. You can then backup that database and restore/attach it back on the source farm.
As I stated at the beginning – I didn’t have the luxury of a second farm.
Approach 2
The only option left that I could think of was to backup and restore each site collection manually.
I knocked up the following powershell script to automate this and it worked …
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
$sourceApp = "http://source.example.com" $targetApp = "http://target.example.com" $app = Get-SPWebApplication -Identity $sourceApp foreach ($site in $app.Sites) { Write-Host performing backup of $site.URL Backup-SPSite -Identity $site.URL -Path "c:\backup\migration.bak" -force $newurl = $site.URL -replace $sourceApp, $targetApp Write-Host restoring into $newurl Restore-SPSite -Identity $newurl -Path "c:\backup\migration.bak" -force -confirm:$false } |