Backup-SSAS


posted by Richard Giles
10-06-2008

Downloads: 953
File size: 5.8kB
Views: 6,375

Embed
Backup-SSAS
  1. ## ===================================================================== 
  2. ## Title       : Backup-SSAS 
  3. ## Description : Backup all Analysis Server databases 
  4. ## Author      : Idera 
  5. ## Date        : 6/27/2008 
  6. ## Input       : -serverInstance <server\inst> 
  7. ##               -backupDestination <drive:\x\y | \\unc\path> 
  8. ##               -retentionDays <n> 
  9. ##               -logDir <drive:\x\y | \\unc\path> 
  10. ##               -verbose  
  11. ##               -debug     
  12. ## Output      : write backup files (*.abf) 
  13. ##                   create log file of activity 
  14. ## Usage            : PS> . Backup-SSAS -serverInstance MyServer -Destination C:\SSASbackup  
  15. ##                                 -RetentionDays 2 -LogDir C:\SSASLog -v -d 
  16. ## Notes            : Original script attributed to Ron Klimaszewski 
  17. ## Tag            : Microsoft Analysis Server, SSAS, backup 
  18. ## Change Log  : 
  19. ## ===================================================================== 
  20.  
  21. param  
  22. (  
  23.     [string]$ServerInstance = "(local)",  
  24.     [string]$BackupDestination,  
  25.     [int]$RententionDays = 2,  
  26.     [string]$LogDir,  
  27.     [switch]$verbose
  28.     [switch]$debug 
  29.  
  30. function main() 
  31.     if ($verbose) {$VerbosePreference = "Continue"
  32.     if ($debug) {$DebugPreference = "Continue"
  33.     Backup-SSAS $serverInstance $backupDestination $retentionDays $logDir 
  34.  
  35. function Backup-SSAS($serverInstance, $backupDestination, $retentionDays, $logDir
  36.     trap [Exception]  
  37.     
  38.         write-error $("TRAPPED: " + $_.Exception.Message); 
  39.         continue
  40.     
  41.      
  42.     # Force a minimum of two days of retention  
  43.     # TIP: using PS "less than" operator 
  44.     if ($RetentionDays -lt 2 )  
  45.     
  46.         $RetentionDays = 2  
  47.     }  
  48.      
  49.     # Load Microsoft Analysis Services assembly, output error messages to null 
  50.     [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.AnalysisServices") | Out-Null 
  51.      
  52.     # Declare SSAS objects with strongly typed variables 
  53.     [Microsoft.AnalysisServices.Server]$SSASserver = New-Object ([Microsoft.AnalysisServices.Server])  
  54.     [Microsoft.AnalysisServices.BackupInfo]$serverBackup = New-Object ([Microsoft.AnalysisServices.BackupInfo])  
  55.      
  56.     # Connect to Analysis Server with specified instance 
  57.     $SSASserver.Connect($ServerInstance)  
  58.      
  59.     # Set Backup destination to Analysis Server default if not supplied 
  60.     # TIP: using PowerShell "equal" operator 
  61.     if ($backupDestination -eq "")  
  62.     
  63.         Write-Debug "Setting the Destination parameter to the BackupDir parameter"  
  64.         $BackupDestination = $SSASserver.ServerProperties.Item("BackupDir").Value  
  65.     }  
  66.      
  67.     # Test for existence of Backup Destination path 
  68.     # TIP: using PowerShell ! operator is equivalent to "-not" operator, see below 
  69.     if (!(test-path $backupDestination))  
  70.     
  71.         Write-Host Destination path `"$backupDestination`" does not exists.  Exiting script.  
  72.         exit 1  
  73.     }  
  74.     else  
  75.     
  76.         Write-Host Backup files will be written to `"$backupDestination`"  
  77.     }  
  78.      
  79.     # Set Log directory to Analysis Server default if not applied 
  80.     if ($logDir -eq "")  
  81.     
  82.         Write-Debug "Setting the Log directory parameter to the LogDir parameter"  
  83.         $logDir = $SSASserver.ServerProperties.Item("LogDir").Value  
  84.     }  
  85.      
  86.     # Test for existence of Log directory path 
  87.     if (!(test-path $logDir))  
  88.     
  89.         Write-Host Log directory `"$logDir`" does not exists.  Exiting script.  
  90.         exit 1  
  91.     }  
  92.     else  
  93.     
  94.         Write-host Logs will be written to $logDir  
  95.     }  
  96.      
  97.     # Test if Log directory and Backup destination paths end on "\" and add if missing 
  98.     # TIP: using PowerShell "+=" operator to do a quick string append operation 
  99.     if (-not $logDir.EndsWith("\"))  
  100.     
  101.         $logDir += "\" 
  102.     }  
  103.      
  104.     if (-not $backupDestination.EndsWith("\"))  
  105.     
  106.         $backupDestination += "\" 
  107.     }  
  108.      
  109.     # Create Log file name using Server instance 
  110.     [string]$logFile = $logDir + "SSASBackup." + $serverInstance.Replace("\","_") + ".log"  
  111.     Write-Debug "Log file name is $logFile" 
  112.      
  113.     Write-Debug "Creating database object and set options..." 
  114.     $dbs = $SSASserver.Databases  
  115.     $serverBackup.AllowOverwrite = 1  
  116.     $serverBackup.ApplyCompression = 1  
  117.     $serverBackup.BackupRemotePartitions = 1  
  118.      
  119.     # Create backup timestamp 
  120.     # TIP: using PowerShell Get-Date to format a datetime string 
  121.     [string]$backupTS = Get-Date -Format "yyyy-MM-ddTHHmm"  
  122.      
  123.     # Add message to backup Log file 
  124.     # TIP: using PowerShell to output strings to a file 
  125.     Write-Debug "Backing up files on $serverInstance at $backupTS" 
  126.     "Backing up files on $ServerInstance at $backupTS" | Out-File -filepath $LogFile -encoding oem -append  
  127.      
  128.     # Back up the SSAS databases 
  129.     # TIP: using PowerShell foreach loop to enumerate a parent-child object 
  130.     foreach ($db in $dbs)  
  131.     
  132.         $serverBackup.file = $backupDestination + $db.name + "." + $backupTS + ".abf"  
  133.      
  134.         # TIP: using mixed string literals and variable in a Write-Host command 
  135.         Write-Host Backing up $db.Name to $serverBackup.File  
  136.         $db.Backup($serverBackup)  
  137.          
  138.         if ($?) {"Successfully backed up " + $db.Name + " to " + $serverBackup.File | Out-File -filepath $logFile -encoding oem -append}  
  139.         else {"Failed to back up " + $db.Name + " to " + $serverBackup.File | Out-File -filepath $logFile -encoding oem -append}  
  140.     }  
  141.      
  142.     # Disconnect from Analysis Server 
  143.     $SSASserver.Disconnect()  
  144.      
  145.     # Clear out the old files and files backed up to the Log file 
  146.     Write-Host Clearing out old files from $BackupDestination  
  147.     [int]$retentionHours = $retentionDays * 24 * - 1  
  148.     "Deleting old backup files" | Out-File -filepath $logFile -encoding oem -append  
  149.      
  150.     # TIP: using PowerShell get-childitem (get child items for matching location) and pipe to 
  151.     #        where-object (selecting certain ones based on a condition)  
  152.     get-childitem ($backupDestination + "*.abf") | where-object {$_.LastWriteTime -le [System.DateTime]::Now.AddHours($RetentionHours)} | Out-File -filepath $logFile -encoding oem -append  
  153.     get-childitem ($backupDestination + "*.abf") | where-object {$_.LastWriteTime -le [System.DateTime]::Now.AddHours($RetentionHours)} | remove-item  
  154.  
  155. main 
Filed under: ,

Back up all Microsoft Analysis Server databases

Copyright 2012 PowerShell.com. All rights reserved.