How can I dynamically back up all SSAS databases on a given instance? 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.
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 &lt;SQL Server InstallationDrive&gt;:\Program Files\Microsoft SQL Server\&lt;SQL Server Version&gt;\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 &gt; 0***. 
        ' --&gt; If it is 0, it could be a corrupted database which causes the backup process to stop.
        ' --&gt; 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 &gt; 0***.
        ' --&gt; The databse could be corrupt or backup could hang if there are no cubes.
        If AsDatabase.EstimatedSize &gt; 0 And AsDatabase.Cubes.Count &gt; 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= " &amp; AsDatabase.EstimatedSize &amp; "; " &amp; "Number of cubes= " &amp; 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= '" &amp; ASConn &amp; "'. Error: " &amp; ex.ToString, String.Empty, 0)
    End Try

    'Print info for output while testing/debugging.
    Dts.Events.FireInformation(0, "Establishing SSAS Connection", "Connection established. ConnectionString= '" &amp; ASConn &amp; "'.", 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 &lt;&gt; "" Then

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

        'Get timestamp for the backup file
        Dim timeStamp As String = Now.Year &amp; _
                                    Right("0" &amp; Now.Month, 2) &amp; _
                                    Right("0" &amp; Now.Day, 2) &amp; _
                                    Right("0" &amp; Now.Hour, 2) &amp; _
                                    Right("0" &amp; Now.Minute, 2) &amp; _
                                    Right("0" &amp; Now.Second, 2)

        'Get the filename and location for the backupfile
        Dim filePath As String = "\\&lt;servershare&gt;\" &amp; Dts.Variables("ServerForDatasource").Value &amp; "\" &amp; ASDatabaseName
        Dim fileName As String = ASDatabaseName &amp; "_" &amp; timeStamp &amp; ".abf"

        'Put the filepath into the SSIS variable
        Dts.Variables("BackupDir").Value = filePath

        'Print info for output while testing/debugging
        Dts.Events.FireInformation(0, "Retrieving full backup path", "BackupPath= '" &amp; filePath &amp; "\" &amp; fileName &amp; "'.", String.Empty, 0, True)

        'Build XMLA script
        Dim xmlaScript As String = ""
        xmlaScript = "&lt;Backup xmlns=""http://schemas.microsoft.com/analysisservices/2003/engine""&gt;" &amp; _
                            "&lt;Object&gt;&lt;DatabaseID&gt;" &amp; ASDatabaseId &amp; "&lt;/DatabaseID&gt;&lt;/Object&gt;" &amp; _
                            "&lt;File&gt;" &amp; filePath &amp; "\" &amp; fileName &amp; "&lt;/File&gt;" &amp; _
                        "&lt;/Backup&gt;"

        'Put the XMLA script into the SSIS variable
        Dts.Variables("XMLAScript").Value = xmlaScript

        'Print info for output while testing/debugging.
        Dts.Events.FireInformation(0, "Generate XMLAScript", "XMLAScript= '" &amp; xmlaScript &amp; "'.", String.Empty, 0, True)
    Else
        'Print info for output while testing/debugging.
        'If DatabaseId is empty, we will not perform the rest of the tasks in the Foreach container.
        Dts.Events.FireWarning(0, "Retrieving DatabaseId", "Unable to retrieve the DatabaseId for Database= '" &amp; ASDatabaseName &amp; "'.", String.Empty, 0)
    End If

    ' Return success
    Dts.TaskResult = ScriptResults.Success
End Sub



Create a File System Task (to create the Backup Directory)

It is important to set UseDirectoryIfExists to true to avoid an error if the backup directory already exists.



Edit the precedence constraint between the Execute Script Task and the File System Task

This precedence constraint will handle a missing DatabaseId. The DatabaseId would be missing if a connection to the SSAS server could not be established in the vbscript, or if the database is corrupted. You do not want to back up a corrupted database. It will bring the backup to a halt.



Example of Expression: `@[User::DatabaseId] !=""

Create an Analysis Services Execute DDL Task



Your final Control Flow should look like this:





Create the SQL Agent Job


Add a step for each SSAS instance that will be backed up.
Each step should be configured to execute the SSIS package.
For each step, click the Set values tab and set the value of InstanceForDatasource to the instance name for the step. 


Example syntax for Property Path: \Package.Variables[User::InstanceForDatasource].Properties[Value]
(More on using the Set values tab)



Notes:


To make the entire backup directory dynamic, you would just have to add another variable for that and account for it in the vbscript.
Again, if necessary, the entire connection string variable could be set by the SQL Agent job instead of just the instance name.