Saturday, June 30, 2012

Site Collection Restore Using powershell from Farm Back UP


Hi,

I am posting power shell script where site collection is getting restored with new configuration from the Farm Back up.

Generally if we write below script to restore site collection, it restores site collection into existing database which is old configuration.

Restore-SPSite -identity $site -path $restorefrom -force -Confirm:$false

Whereas i have put Database, DatabaseServerName, WebApplicationName, SiteUrl in xml file

From xml file power shell script is accessing the values as below.

// RestoreSiteColl.ps1
$snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}
if ($snapin -eq $null)
{
Write-Host "Loading SharePoint Powershell Snap-in"
Add-PSSnapin "Microsoft.SharePoint.Powershell"
}
[xml]$xmldata=Get-Content D:\Naga\RestoreSiteCollScript\Test.xml

##[Environment]::CurrentDirectory=(Get-Location -PSProvider FileSystem).ProviderPath
$site = $xmldata.RestoreSiteCollectionInputs.SiteUrl
$restorefrom = $xmldata.RestoreSiteCollectionInputs.Backuppath
$webappurl= $xmldata.RestoreSiteCollectionInputs.WebApplicationName

# get database server and name - prefer to explicitly restore over the existing database
$database = Get-SPContentDatabase -Site $site
if ($database -ne $null)
{
                $dbname = $xmldata.RestoreSiteCollectionInputs.Database
                $dbserver = $xmldata.RestoreSiteCollectionInputs.DatabaseServerName
  New-SPContentDatabase -Name  $dbname -WebApplication $webappurl
                # restore rwebase backup into this new site collection  
                 Restore-SPSite -identity $site -path $restorefrom -force -databasename $dbname -databaseserver $dbserver -Confirm:$false
                write-host "Site Collection Successfully restored" -ForegroundColor Green
}
else
{
                Write-Host "Error: Site '($($site))' must not exist, content database not found. Please ensure the site collection for RWE Home exists and passed as the parameter to this script" -ForegroundColor Red
}

//Test.xml
<?xml version="1.0" encoding="utf-8" ?>
<RestoreSiteCollectionInputs>
  <Database>restoredb43</Database>
  <DatabaseServerName>databaseservername</DatabaseServerName>
  <WebApplicationName>SharePoint - portno</WebApplicationName>
  <SiteUrl>http://servername:portno/sites/samplesite</SiteUrl>
  <Backuppath>D:\SiteCollBackup\backupsite.bak</Backuppath>
</RestoreSiteCollectionInputs>

No comments:

Post a Comment