janikvonrotz
10/11/2013 - 3:38 PM

PowerShell: Backup SharePoint Web Application #PowerShell #SharePoint

PowerShell: Backup SharePoint Web Application #PowerShell #SharePoint

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Date>2013-03-20T14:18:21.6393172</Date>
    <Author>Janik von Rotz (www.janikvonrotz.ch)</Author>
	<Description>SharePoint and SQL Backup</Description>
  </RegistrationInfo>
  <Triggers>
    <CalendarTrigger>
      <StartBoundary>2013-01-01T02:30:00</StartBoundary>
      <Enabled>true</Enabled>
      <ScheduleByDay>
        <DaysInterval>1</DaysInterval>
      </ScheduleByDay>
    </CalendarTrigger>
  </Triggers>
  <Settings>
    <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
    <DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
    <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
    <AllowHardTerminate>true</AllowHardTerminate>
    <StartWhenAvailable>false</StartWhenAvailable>
    <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
    <IdleSettings>
      <StopOnIdleEnd>true</StopOnIdleEnd>
      <RestartOnIdle>false</RestartOnIdle>
    </IdleSettings>
    <AllowStartOnDemand>true</AllowStartOnDemand>
    <Enabled>true</Enabled>
    <Hidden>false</Hidden>
    <RunOnlyIfIdle>false</RunOnlyIfIdle>
    <WakeToRun>false</WakeToRun>
    <ExecutionTimeLimit>P3D</ExecutionTimeLimit>
    <Priority>7</Priority>
  </Settings>
  <Actions Context="Author">
    <Exec>
      <Command>$PSapps.PowerShell</Command>
      <Arguments>$(Get-ChildItem -Path $PSscripts.Path -Filter "Backup-SharePointStandalone.ps1" -Recurse).Fullname</Arguments>
      <WorkingDirectory>$PSProfile.Path</WorkingDirectory>
    </Exec>
  </Actions>
</Task>
try{
  
    #--------------------------------------------------#
    # settings
    #--------------------------------------------------#
    $SQLBackupFolder = "E:\SQLExpress\MSSQL10_50.SQLEXPRESS\MSSQL\Backup"
    $SharePointBackupFolder = "E:\SharePoint\Backup"
    
    #--------------------------------------------------#
    # sql backup
    #--------------------------------------------------#
    Backup-AllSQLDBs -Path $SQLBackupFolder -Instance SQLExpress
    
    # delete old backups
    gci $SQLBackupFolder -Recurse | where{$_.PsIsContainer} | %{
        gci $_.FullName | where{-not $_.PsIsContainer} | sort CreationTime -Descending | select -Skip 1 | Remove-Item -Force
    }
    
    Write-PPEventLog -Message "Finished SQL Backup" -WriteMessage -Source "SharePoint and SQL Backup" 
    
    #--------------------------------------------------#
    # sharepoint backup
    #--------------------------------------------------#
    # backup lists
    Backup-AllSPLists -Path $SharePointBackupFolder
    
    # backup sites
    Backup-AllSPWebs -Path $SharePointBackupFolder
    
    # backup site collections
    Backup-AllSPSites -Path (Join-Path -Path $SharePointBackupFolder -ChildPath "Full")
    
    # delete old backups
    gci $SharePointBackupFolder -Recurse | where{$_.PsIsContainer} | %{
        gci $_.FullName | where{-not $_.PsIsContainer} | sort CreationTime -Descending | select -Skip 1 | Remove-Item -Force
    }
    
}catch{
  
    Write-PPEventLog -Message "Finished SharePoint Backup" -WriteMessage -Source "SharePoint and SQL Backup" 
    Write-PPErrorEventLog -Source "SharePoint and SQL Backup" -ClearErrorVariable
}