Monday 4 July 2016

Site Collection Backup and Restore

Backup Site Collection 


######################### Add SharePoint PowerShell Snapin ###############################################
 
 if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )
 {
 
 Add-PSSnapin Microsoft.SharePoint.Powershell
 
 }
 


 ########################### End of Add SharePoint PowerShell Snapin ##################################

  ######################## Set Execution Path ################################################
 
 $scriptBase = split-path $SCRIPT:MyInvocation.MyCommand.Path -parent
 
 Set-Location $scriptBase
 
 ################################# End of Set Execution Path #################################



############################ Functions Starts Here #########################################

function SiteBackUp([string]$SiteURL, [string]$DestinationPath)
{
 try
  {

    Backup-SPSite -identity $SiteURL -path $DestinationPath -nositelock -Force

    Write-Host $SiteURL "Backup Successfully Completed and Placed in the" $DestinationPath

  }
  catch
  {
  }
}

######################### Function Ends Here ################################################



Write-Host "Site BackUp Process Started!!!"

$CSVFileName=Read-Host -Prompt "Please enter Your CSV file Name"


######################## Data fetching from CSV #############################################

 $SiteBackupCSV = $scriptBase + "\" + $CSVFileName
 
 import-csv $SiteBackupCSV | where {

 
    SiteBackUp $_.SiteURL $_.DestinationPath
 

 }




Sample CSV file:







SiteURL DestinationPath
http://sp2013devvm/sites/Pub2016  D:\Backupfolder\first.bak

Restore Site Collection


######################### Add SharePoint PowerShell Snapin ###############################################
 
 if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )
 {
 
 Add-PSSnapin Microsoft.SharePoint.Powershell
 
 }
 


 ########################### End of Add SharePoint PowerShell Snapin ##################################

  ######################## Set Execution Path ################################################
 
 $scriptBase = split-path $SCRIPT:MyInvocation.MyCommand.Path -parent
 
 Set-Location $scriptBase
 
 ################################# End of Set Execution Path #################################



############################ Functions Starts Here #########################################

function SiteRestore([string]$SiteURL, [string]$SourcePath, [string]$DatabaseName)
{
 try
  {

    Restore-SPSite -identity $SiteURL -path $SourcePath -databasename $DatabaseName -force

    Write-Host $SiteURL "Restore Successfully"

  }
  catch
  {
  }
}

######################### Function Ends Here ################################################



Write-Host "Site Restore Process Started!!!"

$CSVFileName=Read-Host -Prompt "Please enter Your CSV file Name"


######################## Data fetching from CSV #############################################

 $SiteBackupCSV = $scriptBase + "\" + $CSVFileName
 
 import-csv $SiteBackupCSV | where {

 
    SiteRestore $_.SiteURL $_.SourcePath $_.DatabaseName
 

 }


Sample CSV file:



SiteURL SourcePath DatabaseName
http://sp2013devvm/sites/Pub2016  D:\Backupfolder\first.bak Wss_content

























No comments:

Post a Comment