Friday 7 November 2014

Backup and Restore SharePoint Site Collections using PowerShell scripts

The requirement is to take all site collection backups from the respective web application and  restoring that into different web application in another farm.

BackupSites.ps1

Param($WebAppUrl,$FolderPath,$ReadOnly);
Add-PSSnapin Microsoft.SharePoint.PowerShell

function Main($WebAppUrl,$FolderPath,$ReadOnly)
{
        #Log in Excel file
$xmlPath = "$((pwd).path)/BackupSites.xml";
$resultInXml = new-object xml
$decl = $resultInXml.CreateXmlDeclaration("1.0", $null, $null)
$rootNode = $resultInXml.CreateElement("SiteBackupReport");
$resultInXml.InsertBefore($decl, $resultInXml.DocumentElement)
$resultInXml.AppendChild($rootNode);

Write-Host -Fore Green "Starting..."

write-host "Web Application URL: " $WebAppUrl
write-host "Backup Folder Path: " $FolderPath
write-host "Read Only: " $ReadOnly

$SPWebApp = Get-SPWebApplication $WebAppUrl
foreach ($SPSite in $SPWebApp.Sites)
{
if ($SPSite -ne $null)
{
write-host -Fore Green ------------------------------------------------------------
Write-Host -Fore Green "Web Url - " $SPSite.RootWeb.Url

#Set Read Only LockState for each site
Write-Host -Fore Green "Setting Site LockState.."
$SPSite.ReadOnly = [bool]::Parse($ReadOnly)
Write-Host -Fore Green "Changed ReadOnly LockState to "  $SPSite.ReadOnly

$Filename = $FolderPath+"\" + $SPWebApp.Name.Replace(" ","") + $SPSite.ServerRelativeUrl.Replace("/","-")+ ".bak"
Write-Host -Fore Green "$Filename"

# backup the site
#Overwrite the existing backup file (-Force parameter)
backup-spsite -identity $SPSite.URL -path $FileName -Force

$SiteUrl = $resultInXml.CreateElement("SiteUrl")
$SiteUrl.SetAttribute("SiteURL",$SPSite.URL)
$SiteUrl.SetAttribute("BackupFileName",$FileName)
$rootNode.AppendChild($SiteUrl);
}
}
   
    Write-Host -Fore Green "End..."

$resultInXml.Save($xmlPath)
}

Start-Transcript

Main $WebAppUrl $FolderPath $ReadOnly


Stop-Transcript


BackupSites.bat

echo off
cd %SystemRoot%\system32\WindowsPowerShell\v1.0
powershell {Set-ExecutionPolicy Unrestricted}
cd /d %~dp0

SET WebAppUrl="http://servername:1234/"
SET FolderPath="D:\Backup"
SET ReadOnly= false

powershell.exe -command .\BackupSites.ps1 '%WebAppUrl%' '%FolderPath%' '%ReadOnly%'
powershell {Set-ExecutionPolicy Restricted}
pause

RestoreSites.ps1

Param($WebAppUrl,$BackupFolderPath,$ReadOnly);
Add-PSSnapin Microsoft.SharePoint.PowerShell

function Main($WebAppUrl,$BackupFolderPath,$ReadOnly)
{
Write-Host -Fore Green "Starting..."

#Log in Excel file
$xmlPath = "$((pwd).path)/RestoreSites.xml";
$resultInXml = new-object xml
$decl = $resultInXml.CreateXmlDeclaration("1.0", $null, $null)
$rootNode = $resultInXml.CreateElement("SiteRestoreReport");
$resultInXml.InsertBefore($decl, $resultInXml.DocumentElement)
$resultInXml.AppendChild($rootNode);

write-host "Web Application URL: " $WebAppUrl
write-host "Backup Folder Path: " $BackupFolderPath
write-host "Read Only: " $ReadOnly

#Get WebApplication
$SPWebApp = Get-SPWebApplication $WebAppUrl

$files = ([System.IO.DirectoryInfo] (Get-Item $BackupFolderPath)).GetFiles()
ForEach($file in $files)
{
  write-host -Fore Green ------------------------------------------------------------
  write-host -Fore Green "Backup file name : " $file.Name
  write-host -Fore Green "Backup file Path : " $file.FullName
  $BackupFilePath = $file.FullName

  $lists = $file.Name.Substring(0, $file.Name.LastIndexOf('.')).split("-");
             
if($lists.Count -eq 3)
{  
#Restore Site Url
write-host -Fore Green "Managed Path :" $lists[1]
$RestoreSiteUrl = $WebAppUrl+"/"+$lists[1]+"/"+$lists[2]

#check ManagedPath exists in the web application
#If not create a new Managed Path
Create-SPManagedPath $WebAppUrl  $lists[1]

write-host -Fore Green "Restore Site Url: " $RestoreSiteUrl
Restore-SPSite -Identity $RestoreSiteUrl -Path $BackupFilePath -Confirm:$false
write-host -Fore Green "Site :" RestoreSiteUrl "restored successfully"

#Set Read Only LockState for each site
Write-Host -Fore Green "Setting Site LockState.."
$SPSite = Get-SPSite $RestoreSiteUrl
$SPSite.ReadOnly = [bool]::Parse($ReadOnly)
Write-Host -Fore Green "Changed ReadOnly LockState to "  $SPSite.ReadOnly

$SiteUrl = $resultInXml.CreateElement("SiteUrl")
    $SiteUrl.SetAttribute("SiteURL",$SPSite.URL)
    $SiteUrl.SetAttribute("ReadOnly",$ReadOnly)
    $rootNode.AppendChild($SiteUrl);
}
elseif($lists.Count -eq 2)
{  
    #Restore Root Site Url
write-host -Fore Green "Root Site"
$RestoreSiteUrl = $WebAppUrl+"/"
write-host -Fore Green "Restore Site Url: " $RestoreSiteUrl
#Restore-SPSite -Identity $RestoreSiteUrl -Path $BackupFilePath -Confirm:$false
write-host -Fore Green "Site :" RestoreSiteUrl "restored successfully"

#Set Read Only LockState for each site
Write-Host -Fore Green "Setting Site LockState.."
$SPSite = Get-SPSite $RestoreSiteUrl
$SPSite.ReadOnly = [bool]::Parse($ReadOnly)
Write-Host -Fore Green "Changed ReadOnly LockState to "  $SPSite.ReadOnly
}
else
{
 write-host -Fore Green "Unable to restore the Backup file : " $file.FullName
}
write-host -Fore Green ------------------------------------------------------------
}
   
    Write-Host -Fore Green "End..."

$resultInXml.Save($xmlPath)
}

function Create-SPManagedPath
{
    param ($webApplicationUrl, $managedPathName)
    $managedPath = Get-SPManagedPath -WebApplication $webApplicationUrl -Identity $managedPathName -ErrorAction SilentlyContinue
    if ($managedPath -ne $null)
    {
        Write-Host "Managed path $managedPathName already exists."
        return
    }

    Write-Host "Creating managed path $managedPathName ..."
    New-SPManagedPath –RelativeURL $managedPathName -WebApplication $webApplicationUrl
    Write-Host "Managed path $managedPathName created sucessfully" -foregroundcolor Green
}


Start-Transcript

Main $WebAppUrl $BackupFolderPath $ReadOnly


Stop-Transcript

RestoreSites.bat

echo off

cd %SystemRoot%\system32\WindowsPowerShell\v1.0
powershell {Set-ExecutionPolicy Unrestricted}
cd /d %~dp0

SET WebAppUrl="http://servername:5007"
SET BackupFolderPath="D:\Backup"
SET ReadOnly=false

powershell.exe -command .\RestoreSites.ps1 '%WebAppUrl%' '%BackupFolderPath%' '%ReadOnly%'

powershell {Set-ExecutionPolicy Restricted}

pause



No comments:

Post a Comment