HiveBrain v1.2.0
Get Started
← Back to all entries
snippetModerate

How can I dynamically back up all SSAS databases on a given instance?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
candynamicallydatabasesallbackinstancehowgivenssas

Problem

I want to dynamically back up all the databases on a given SSAS instance using a SQL Agent job (which would most likely involve executing an SSIS package). It is imperative that this is a dynamic process - if users add databases or cubes, I want to set up a job one time that can automatically detect all existing SSAS metadata.

Unfortunately, I don't see anything out there that tells me how I can automatically and dynamically back up all of the databases on an SSAS instance in a clean way. By "clean", I mean:

  • Using the DatabaseId in the backup command, not the DatabaseName. Sometimes there can be a difference between a DatabaseName and a DatabaseId, and if there is a difference, the backup will fail if the DatabaseName is used in place of the DatabaseId. Just querying the Catalogs schema alone won't give me the DatabaseId.



  • Avoiding the need to create a linked server every time I come across a new instance to back up.

Solution

This can in fact be done. There are probably a few ways to do it, and here is a fairly straightforward example. For this solution, you will use a combination of:

  • A SQL Agent job with a step for each instance that needs backed up (i.e. A step for the development server, the qa server, and for production).



  • One dynamic SSIS package that is called in each step of the job.



  • An Execute Script Task that uses the Analysis Management Objects (AMO).



Create the SSIS Package

Variables

VariableName|Scope|Type

-
BackupDir | Package level or Foreach level | String

-
DatabaseId | Package level or Foreach level | String

-
DatabaseName | Package level or Foreach level | String

-
InstanceForDatasource | Package level | String

  • Intended for use in the Set Values tab of the SQL Agent job if more than one instance will be backed up. This variable will be used to build the ConnectionString.



-
ConnectionString | Package level | String

-
Set the EvaluateAsExpression property to true.

-
Set the Expression property as follows: Data Source="+ @[User::InstanceForDatasource] +";Provider=MSOLAP.4;Integrated Security=SSPI;

-
Please note that this approach can be expanded upon to have the entire ConnectionString be dynamic and dictated by the calling SQL Agent job step if necessary.

-
XMLAScript | Package or Foreach level | String

Connection Managers

Use a real connection in design time so that the metadata plays nice.

Creating the connection managers now isn't required, but it makes it easier for later. For each task in the process, you will have the appropriate connection manager available in the drop-down without the need to create any on-the-fly.

ADO.NET

  • Create a new ADO.NET connection manager that uses the Microsoft OLE DB Provider for Analysis Service



  • Using the Property Expressions Editor, set the ConnectionString property as follows: [User::ConnectionString]



OLEDB

  • Create a new OLEDB connection manager that uses the Microsoft OLE DB Provider for Analysis Service



  • Using the Property Expressions Editor, set the ConnectionString property as follows: [User::ConnectionString] + "Format=Tabular;"



Analysis Services

  • Create a new Analysis Services connection



  • Using the Property Expressions Editor, set the ConnectionString property as follows: [User::ConnectionString] = "Impersonation Level=Impersonate;"



Create the Foreach Container

Here, you will create a Foreach based on the Catalogs schema rowset. This will get us the DatabaseName for each database in the instance and the DatabaseName will be put into its corresponding variable.

Create an Execute Script Task

Set the script to use Visual Basic.

Set the ReadOnlyVariables and ReadWriteVariables as follows:

  • ReadOnlyVariables: User::ConnectionString,User::DatabaseName,User::InstanceForDatasource



  • ReadWriteVariables: User::BackupDir,User::DatabaseId,User::XMLAScript



Edit the Script

-
Add a reference to the Analysis Management Objects (AMO) assembly.

-
Right-click on the project name (should be the top-most item in the Project Explorer in the Visual Studio window that opened when you clicked Edit Script) and select Add Reference.

-
Add a reference to the Analysis Services Objects component. The dll for AMO should be located in :\Program Files\Microsoft SQL Server\\SDK\Assemblies.

-
In the script header, add an Imports statement to use AMO: Imports Microsoft.AnalysisServices

-
Replace the contents of Public Sub Main() with the following script:

```
Public Sub Main()
'
Dim ASServer As New Microsoft.AnalysisServices.Server()
Dim AsDatabase As New Microsoft.AnalysisServices.Database
Dim ASConn As String = ""
Dim ASDatabaseName As String = ""
Dim ASDatabaseId As String = ""

'Create a variable that uses the dynamic ConnectionString variable
ASConn = Dts.Variables("ConnectionString").Value

'What database are we working with in this iteration of the Foreach?
ASDatabaseName = Dts.Variables("DatabaseName").Value

'Use the Analysis Services AMO to get the DatabaseId for this DatabaseName.
' It is necessary to get the DatabaseId because the XMLA backup command requires the DatabaseId,
' and if at any point the database is renamed, the DatabaseId will differ from the DatabaseName.
Try
'Establish the connection to SSAS.
ASServer.Connect(ASConn)

'Get the database.
AsDatabase = ASServer.Databases.FindByName(ASDatabaseName)

'ONLY IF the EstimatedSize > 0.
' --> If it is 0, it could be a corrupted database which causes the backup process to stop.
' --> In that case, we will leave the DatabaseId variable blank to be a visiblie indicator that the db is corrupt.
'ONLY IF the number of cubes > 0.
' --> The databse could be corrupt or backup could hang if there are no cubes.
If AsDatabase.EstimatedSize > 0 And AsDatabase.Cubes.Count > 0 Then
'Get the Databa

Code Snippets

Public Sub Main()
    '
    Dim ASServer As New Microsoft.AnalysisServices.Server()
    Dim AsDatabase As New Microsoft.AnalysisServices.Database
    Dim ASConn As String = ""
    Dim ASDatabaseName As String = ""
    Dim ASDatabaseId As String = ""

    'Create a variable that uses the dynamic ConnectionString variable
    ASConn = Dts.Variables("ConnectionString").Value

    'What database are we working with in this iteration of the Foreach?
    ASDatabaseName = Dts.Variables("DatabaseName").Value

    'Use the Analysis Services AMO to get the DatabaseId for this DatabaseName.
    '   It is necessary to get the DatabaseId because the XMLA backup command requires the DatabaseId,
    '   and if at any point the database is renamed, the DatabaseId will differ from the DatabaseName.
    Try
        'Establish the connection to SSAS.
        ASServer.Connect(ASConn)

        'Get the database.
        AsDatabase = ASServer.Databases.FindByName(ASDatabaseName)

        '***ONLY IF the EstimatedSize > 0***. 
        ' --> If it is 0, it could be a corrupted database which causes the backup process to stop.
        ' --> In that case, we will leave the DatabaseId variable blank to be a visiblie indicator that the db is corrupt.
        '***ONLY IF the number of cubes > 0***.
        ' --> The databse could be corrupt or backup could hang if there are no cubes.
        If AsDatabase.EstimatedSize > 0 And AsDatabase.Cubes.Count > 0 Then
            'Get the DatabaseId
            'Retrieve the DatabaseId from the Databases collection.
            ASDatabaseId = ASServer.Databases.FindByName(ASDatabaseName).ID
        Else
            Dts.Events.FireWarning(0, "Verifying database", "The estimated size and/or number of cubes in the database does not meet the requirements. Estimated Size= " & AsDatabase.EstimatedSize & "; " & "Number of cubes= " & AsDatabase.Cubes.Count, String.Empty, 0)
        End If
    Catch ex As Exception
        'Couldn't connect. Do not error out because maybe the next iteration will succeed.
        Dts.Events.FireWarning(0, "Establishing SSAS Connection", "Unable to connect to the SSAS Server with ConnectionString= '" & ASConn & "'. Error: " & ex.ToString, String.Empty, 0)
    End Try

    'Print info for output while testing/debugging.
    Dts.Events.FireInformation(0, "Establishing SSAS Connection", "Connection established. ConnectionString= '" & ASConn & "'.", String.Empty, 0, True)

    'Put the DatabaseId into the SSIS variable. 
    Dts.Variables("DatabaseId").Value = ASDatabaseId

    'If we were able to retrieve the DatabaseId, then generate the backup directory and filename and the XMLA script.
    If ASDatabaseId <> "" Then

        'Print info for output while testing/debugging.
        Dts.Events.FireInformation(0, "Retrieving DatabaseId", "DatabaseId retrieved for " & ASDatabaseName & ". DatabaseId= '" & ASDatabaseId & "'.", String.Empty, 0, True)

        'Get timestamp for the backup file
        Dim timeStamp As S

Context

StackExchange Database Administrators Q#43493, answer score: 10

Revisions (0)

No revisions yet.