Monday 6 August 2012

Site Collections Backup Script

If we want to take backup of a single site collection through Powershell we use the following command:

Backup-SPSite "http://sitecollectionurl" -Path "PhysicalPath\backupname.bak"

This will take the backup of a single site collection. But if we have to take the backup of many site collections then running the above command every time will not be feasible & is a waste of time.
Now the way out in this situation is to prepare a Powershell script & run the script once & it will take the backup of all the site collections we want. This Powershell script reads the site URL & the backup name from a csv file. We will enter the url's & their respective backup file names in the csv file type & give the path of the csv file in the script. This script now reads the values one by one from each row & takes the backup.
When the rows are finished from the csv file then it stops the script. In this way we can take the backup of large number of site collections by running a single command.

The Powershell script is as follows:

$backupfolder= "Folder Path where the backup is to be stored"
$filePath = "Path of the .csv file"
$csv_info = Import-Csv $filePath
$count =$csv_info.count
#iterate Csv File
foreach ($line in $csv_info)
{
$siteurl = $line.SiteUrl.trim()
$backuppath = $backupfolder + "\" + $line.BackupPath.trim()
#cls
Backup-SPSite -Identity $siteurl -Path $backuppath -Force
#Write-Host $siteurl
Write-Host $backuppath
Write-Host "Sucess"
}  


Save the file with the .ps1 extension. e.g.- "backupscript.ps1".

Create a csv file & insert the entries as follows:

SiteUrl,BackupPath
http://siteurl,siteurl.bak
http://webapp/sites/siteurl2,siteurl2.bak
http://webapp/sites/siteurl3,siteurl3.bak
http://webapp/sites/siteurl4,siteurl4.bak


When we have finished creating both the files, now open the Powershell Management tool & run a single command which contains only the path of the powershell script. e.g.- Backup-Path\backupscript.ps1.

The script will stop after all the site collection backup is finished.


No comments:

Post a Comment