Clone a web application

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:

  1. perform a SQL backup of the content database
  2. restore the backup into a new database
  3. created a new web application
  4. marked the content database that this created as offline
  5. 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 …