Friday 2 November 2012

Backup of SharePoint site with PowerShell

Hi All,

Today I will blog about important but simple topic of SharePoint project execution, i.e. Backup of SharePoint sites.

Environment: SharePoint 2010

Though there are many options and fine level of granularity in SharePoint 2010 central admin for taking the backup of sites, clearly one thing is missing i.e. recurring backups or scheduling backups which is crucial in SharePoint project execution.

Today I will provide step by step process to schedule backups with powerful powershell scripting.

1. Create a script that takes the backup of the site and stores in to a shared location.
2. After the backup is taken and stored in the shared location, it is better to delete the old backups as we can't afford infinite memory.
3. Create a batch file and call this powershell script and with any parameters.
4. Use windows scheduler to schedule this batch job.

And now the interesting code part ...


1. Typically the powershell script can be like this



function BackupNCopybak($urlsource,$bkpsrc,$bakname)
{ 
 #creating a folder at the backup location with the name of today in yyyymmdd format so that it is easy to sort the folders
 $bkpsrctoday=$bkpsrc+"\"+$today
 
 # if path exists then use it, else create a folder
 if(!(Test-Path $bkpsrctoday))
 {
     #write-host "creating directory with today's name"
     New-Item $bkpsrctoday -type directory -force
 }
 
 # appending the path and the backup name which is required while takeing the backup of the site collection
    $backupfilepathsrc=$bkpsrctoday+"\"+$bakname
 
    write-host "adding sharepoint snapin"
    add-pssnapin microsoft.sharepoint.powershell -erroraction "SilentlyContinue"
 
    write-host "back up started"
    backup-spsite -identity $urlsource -path $backupfilepathsrc -force
 
 #to delete the old backups, first step is to find the folder name that has the old backup.
 # this script stores the backup for past 8 days and delete the old ones, if you want more backups increase the number
 $last8thday=[System.DateTime]::Today.AddDays(-8).ToString("yyyyMMdd")
 $last8thdaypath=$bkpsrc+"\"+$last8thday
 
 # check if the old backup exists, if exists delete else just move on
 if((Test-Path $last8thdaypath))
 {
  Remove-Item -Path $last8thdaypath -Recurse -Force
 }
}
$today=[System.DateTime]::Today.ToString("yyyyMMdd")
$urlsource=$args[0]
$bkpsrc=$args[1]
$bakname=$args[2]
$bkpdestshare=$args[3]
BackupNCopybak $urlsource $bkpsrc $bakname $bkpdestshare
#BackupNCopybak "http://server:portnumber" "E:\Backups\AutomatedBackups" "SiteCollectionTitle.bak"


2. And the batch file will be 





powershell -command "<location of your powershell script.ps1> <url> <backuplocaton> <backupname.bak>" 


Example  powershell -command "E:\Kesava\Scripts\BackupScripts\SiteBackup.ps1 http://xxxx:2000 E:\Backups\AutomatedBackups xxx.bak"

3. Open Task shceduler in win server 
       3.1 Create a new task
       3.2 Give the name and description
3.3 Select the option "Run whether user selected or not"
3.4 Select the option "Run with highest privileges"
3.5 In triggers tab, click new and create a schedule as per your needs
3.6 In Actions tab, choose the batch (.bat) file location, so that task scheduler will run this once the trigger (schedule) is triggered
3.7 Change any settings in settings tab to your needs and click ok

if's

While doing this, if you see that the task scheduler is not opening then you can follow these troubleshooting tips
1. Check for the service Task Scheduler, if it is stopped then start it.
2. If it is greyed out, then check for the dependency services like windows log service, rpc service and others.
3. Even they are started, then you have to change the registry key HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\Schedule start dword from 4 to 2