Thursday, February 17, 2011

List Level backups for multiple sub sites -Poweshell

In SharePoint projects, there are scenarios, where a number of sub sites are created using a same template (now WSP).

We come across requirements where these templates require updation in due course of time.   And hence the sub sites already created may require an update. But it is not feasible to go to each and every sub site created and make the changes.

So best way could be take backup of variable components (   Lists) and then recreate all the sub sites and then restore back the lists to the newly   created sub sites.

The below mentioned   PowerShell script can be used to take backup export the content  from a specific named list in a shared folder structure, where each sub site will have a specific folder for backup in format cmp :

 

$mainBackuppath="\\abc2010\exportimport"

$File="export.txt"

Get-SPSite -Identity $spsite | Get-SPWeb -limit all| ForEach-Object {

 

 

 

if(($_.URL -eq $spsite+"/xyz") -or ($_.URL -eq $spsite+"") -or ($_.URL -eq $spsite+"/lmn") )

{

write-host -f Green  "Skipping " $_.URL

"Skipping " + $_.URL | Out-File $File -append

 

}

else

{

 

write-host -f Green "processing for "  $_.URL

"processing for " + $_.URL | Out-File $File -append

$currentSubSiteTitlewithoutspace= ""

$currentSubSiteTitlewithoutspace=$_.Title.Replace(" ","")

write-host -f Blue "Creating Directory: "$mainBackuppath"\"$currentSubSiteTitlewithoutspace

"Creating Directory: "+$mainBackuppath+"\"+$currentSubSiteTitlewithoutspace | Out-File $File -append

$error.clear()

 

[IO.Directory]::CreateDirectory($mainBackuppath+"\"+$currentSubSiteTitlewithoutspace)

if($error.count -gt 0){$error | out-file $File -append}

 

 

 

$filesystemPath=""

$filesystemPath = $mainBackuppath+"\"+$currentSubSiteTitlewithoutspace

 

if(Test-Path $filesystemPath)

{

$filesystemPath =$mainBackuppath+"\"+$currentSubSiteTitlewithoutspace+"\"+$currentSubSiteTitlewithoutspace+".cmp"

$DocLibPath=""

$DocLibPath= "/"+$_.Title+"/Shared Documents"

write-host  -f Yellow "Taking Backup for "$DocLibPath " at location " $filesystemPath "...."

"Taking Backup for "+$DocLibPath +" at location "+ $filesystemPath +"...."| Out-File $File -append

 

$error.clear()

export-spweb -identity $_.URL   -path  $filesystemPath   -ItemUrl $DocLibPath -Force -IncludeUserSecurity -IncludeVersions All | Out-File $File -append

if($error.count -gt 0){$error | out-file $File -append}

 

write-host  -f Yellow "Backup for "$DocLibPath " at location " $filesystemPath "Completed."

"Backup for "+$DocLibPath +" at location "+ $filesystemPath +"Completed."| Out-File $File -append

 

}

 

else

{

write-host  -f Red $filesystemPath  " does not exist."

$filesystemPath  +" does not exist." | Out-File $File -append

}

 

write-host -f Green "End of "  $_.URL"`n`n`n`n"

"End of "+  $_.URL+"`n`n`n`n"| Out-File $File -append

 

 

}

 

}

 

 

The below mentioned   PowerShell script can be used to restore backup /exported content  shared folder structure, where each sub site will have a specific folder for backup in format cmp, to the subsites created using new template :

 

 

$spsite= "http://abc:1104"

$mainBackuppath="\\abc2010\exportimport"

$File="export.txt"

Get-SPSite -Identity $spsite | Get-SPWeb -limit all| ForEach-Object {

 

 

 

if(($_.URL -eq $spsite+"/xyz") -or ($_.URL -eq $spsite+"") -or ($_.URL -eq $spsite+"/lmn") )

{

write-host -f Green  "Skipping " $_.URL

"Skipping " + $_.URL | Out-File $File -append

 

}

else

{

 

write-host -f Green "processing for "  $_.URL

"processing for " + $_.URL | Out-File $File -append

 

$currentSubSiteTitlewithoutspace= ""

$currentSubSiteTitlewithoutspace=$_.Title.Replace(" ","")

write-host -f Blue "Checking if directory exist for current web"

"Checking if directory exist for current web" | Out-File $File -append

$filesystemPath=""

$filesystemPath =$mainBackuppath+"\"+$currentSubSiteTitlewithoutspace+"\"+$currentSubSiteTitlewithoutspace+".cmp"

 

if(Test-Path $filesystemPath)

{

 

$DocLibPath=""

$DocLibPath= "/"+$_.Title+"/Shared Documents"

write-host  -f Yellow "Restoring  Backup at  "$DocLibPath " from  " $filesystemPath "...."

"Restoring  Backup at  "+$DocLibPath +" from  " +$filesystemPath +"...." | Out-File $File -append

 

$error.clear()

Import-SPWeb -Identity $_.URL -Path $filesystemPath  -IncludeUserSecurity  -Force -UpdateVersions Overwrite | Out-File $File -append

if($error.count -gt 0){$error | out-file $File -append}

 

write-host  -f Yellow "Backup for "$DocLibPath " at location " $filesystemPath "Completed."

"Backup for "+$DocLibPath +" at location " +$filesystemPath +"Completed." | Out-File $File -append

 

}

else

 

{

write-host  -f Red $filesystemPath  " does not exist."

$filesystemPath  +" does not exist." | Out-File $File -append

 

}

 

 

 

write-host -f Green "End of "  $_.URL"`n`n`n`n"

"End of "  +$_.URL+"`n`n`n`n" | Out-File $File -append

 

 

}

 

}

 

___________________________________________________________________________________________________________________

description of commands being used:

 

 

1.       Get-SPSite  : creates object for the spsite as given by identity parameter.

2.       Get-SPWeb -limit all  gets all the  spweb in the site collection

 

3.       Out-File $File –append   :  put entry in a log file.

 

4.       write-host -f Green "processing for "  $_.URL :  to write a text on screen

 

5.       $error.clear() : clears the current error

6.       [IO.Directory]::CreateDirectory()  : used to create a folder on the shared path

 

 

7.       if($error.count -gt 0){$error | out-file $File –append :  if the error count is greater than 0, that is logged in the log file.

 

 

8.       Test-Path $filesystemPath  : verifies if the folder has been created

 

9.       export-spweb -identity $_.URL   -path  $filesystemPath   -ItemUrl $DocLibPath -Force -IncludeUserSecurity -IncludeVersions All | Out-File $File –append

: the heart of all commands, which is actually creating the cmp file

 

10.   Import-SPWeb -Identity $_.URL -Path $filesystemPath  -IncludeUserSecurity  -Force -UpdateVersions Overwrite | Out-File $File –append

: the heart of all commands, which is actually restoring   the cmp file.

No comments:

Post a Comment