Quantcast
Channel: My SharePoint of View
Viewing all articles
Browse latest Browse all 10

A word about moving site collections

$
0
0

When the size of your site collections is increasing you sometimes have to move site collections around to put them in new content databases. There are a couple of different ways to perform a site collection move and I will try to cover some of them and end with a quite simple script that can make the work even easier.

So the first one out is the simple backup/restore stsadm operations. The problem with this is that when doing the restore it’s not possible to specify what database to restore it to. So to be able to do this you need to lock the other content databases except the one you want to add it to. This might not be possible and the you could use a maintenance farm.

A much better option is the mergecontentdbs stsadm operation that was introduced in SP1 of Microsoft Office SharePoint Server 2007 and allows 3 different options. The first option only analyse the situation and calculate the amount of site collections in the content database and their size. The second option is to move all sites in the specified source content database and move them to the specified destination content database. Our last option is to use an xml file containing the sites that we want to move as an input file to the command and the operation will only move the sites specified in that xml-file

The best thing with the last option is that we can use the enumsites operation to get an xml file of all sites for our a specific web application.

Example: stsadm -o enumsites -url [webAppUrl] -showlocks > "D:tempsites.xml"

The only problem with the above is that if you have a large amount of site collection and a number of content databases you will need to modify the list and remove the site collections that should not be moved to another content database. Later in this post I will show you how this problem can easily be addressed using PowerShell

Before using the mergecontentdbs operation you should consider two things. The first one is that since the mergecontentdbs operation is using the Content Deployment API it’s not suitable for sites larger then 10 GB. The other is that if content is changed during the operation you will not only risk losing the content but it could also cause database corruption as described in this KB: http://support.microsoft.com/kb/969242. So before moving the sites it’s a good recommendation to lock the site collections before the move.

To solve this I created a PowerShell script that does the following:

  1. Exports all the site collection for specific web application to an xml-file
  2. Remove all sites in the file that is not located in the database I have specified
  3. Sets the site collections to no access to prevent data loss
  4. Move the sites from the source database to the destination database using the xml file
  5. Offers you to make an IISreset on all WFEs in the farm (Recommended after the stsadm operation)
  6. Set the site collection lock back to it’s original
$env:PATH = $env:PATH +
";C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions12BIN" 

$webAppUrl = "http://myWebAppUrl"
$sourceContentDB = "SourceContentDB"
$destinationContentDB = "DestinationContentDB"
$xmlfile = "d:tempsites.xml" 

# enumerate through all site collections and save them to the XML-file
Write-Host "Retreives all site collections located at"
$webAppUrl "and save it to " $xmlfile
stsadm -o enumsites -url $webAppUrl -showlocks > $xmlfile | out-null 

# Import the XML file content $sites = [xml](Get-Content $xmlfile)
# Remove only the site collections located in the source database
Write-Host "Cleaning up the" $xmlfile "file and remove sites that should not be affected"
$sites.Sites.Site | Where-Object {$_.ContentDatabase -ne $sourceContentdb} |
ForEach-Object { $_.ParentNode.RemoveChild($_) | out-null } 

# Enumerate through all the sites in the xml file and make necessary preparations 

foreach($site in $sites.sites.site) {
# Lock the sites to prevent errors when migration and data loss
Write-Host "Locking the site" $site.url "to prevent data loss"
stsadm -o setsitelock -url $site.URL -lock Noaccess
} 

# Moving sites
stsadm -o mergecontentdbs -url $webAppUrl -sourcedatabasename $sourceContentDB
  -destinationdatabasename $destinationContentDB -operation 3 -filename $xmlfile 

# Get the farm object to find get all the WFEs in the farm
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$farm = [Microsoft.SharePoint.Administration.SPFarm]::Local
Write-Host "To complete the move you need to make an IISreset on all WFEs in the farm"
Write-Host "You will now get the option to make an IISreset on all your WFEs"
Write-Host "" 

# Enumerate through all servers in the farm 

foreach ($svr in $farm.Servers) {
  foreach ($svc in $svr.ServiceInstances) {
    # If the server has the Windows SharePoint Services Web Application service it
    # is most likely it's a WFE.
    if($svc.TypeName -eq "Windows SharePoint Services Web Application"){
      # Ask if we should make an IISReset on the server
      $IISReset = Read-Host "Do you want to make an IIS-reset on"
      $svr.DisplayName "server (yes/No)?"
      if($IISReset.ToLower() -eq "yes") {
        iisreset $svr.DisplayName
      }
    }
  }
}
# Enumerate through all the sites in the xml file and finalize the move
foreach($site in $sites.sites.site) {
  # Set the original lockstate for the site collection
  Write-Host "Setting the original lock state for site" $site.url "to prevent data loss"
  stsadm -o setsitelock -url $site.URL -lock $site.Lock
}

You can download the script [Download not found]. Just make sure you run it with an account that has sufficient permissions as described in the enumcontentdbs TechNet article and to modify the first 4 paramaters.

Another option to move site collections is to use the Batch Site Manager that is included in the SharePoint Administration Toolkit. This tool is packaged as a solution that you deploy to your farm. When deployed (and activated), it will then have a new option in Central Administration under Application Management.

The tool allows you to very nicely see all site collection for each content database, select the ones you want to move and create a timer job for the move. The Batch Site Manager is using the stsadm operations backup and restore in the background. It usually takes a bit longer then the mergecontentdbs but you get the option to specify when the timer job should be run, where to store the temporary file and you can get a email notification telling you the status of the move. As you can see in the screenshot above the tool also offers you to batch locking and deletion of sites.

The Batch Site Manager is recommended to use with sites up to 15 GB. The mentioned recommendations for the both tools should be seen as recommendation and it’s possible to test your way forward and what works in your environment.

So what about really large site collections?
When moving really large sites none of the mentioned tools might work and you will then have to take on a different approach. In short this is what you need to do.

  1. Set the site collection that needs to be moved (farm 1) to read only and make a SQL backup of the content database (ContentDB A).
  2. Restore the content database with a new name (ContentDB B) and attach it to your Maintanence farm (Farm B) or if you don’t have one to a different web application. Make sure you use the –assignnewdatabaseid since we will later on restore the database back to it’s original location
  3. When it’s attached to farm B, use the stsadm -deletesite operation using the -gradualdelete parameter (introduced in the April CU) to remove the sites from ContentDB B that should not be moved.
  4. Backup the ContentDB B and restore it (but don’t attach it yet) to your Farm A
  5. Delete the large site from ContentDB A using stsadm and -gradualdelete.
  6. When the site is deleted you can attach ContentDB A to Farm A
  7. Unlock the site collection when done.

Viewing all articles
Browse latest Browse all 10

Latest Images

Trending Articles





Latest Images