There are few, and I do mean a few, examples of backing up your SSAS databases in an automated fashion. For a couple of years I have used a method of looping through a list in SSIS and executing an XMLA script. In this post I will demonstrate create a console application using Analysis Management Objects (AMO).
Step 1: Create a console application
Create a console application in Visual Studio and add Microsoft.AnalysisServices to your references.
Step 2: Add a new class Argument Parser class.
The code in this class is used almost verbatim from a handy post at .Net Scraps.
Step 3: Add a new class Processing.
This class will have all the code doing the backup. The method BackupSsasDatabases connects to the server identified in arguments passed in. Once connected to the server, we then loop through the SSAS databases enumerated in the DatabaseCollection. There is a helper method called GetBackupInfo that returns an instance of the BackupInfoClass and is used as an argument for the Backup method.
The GetFilePath method uses an argument passed into the application indicating the destination for the backup file, and builds the file name from the database name and the date and time of the backup.
Step 4: Arguments
There are four arguments the application uses. The first two are required and the second two are optional:
- ServerName : The name of the server you’re connecting to
- Path: The destination directory for the backup file
- AllowOverwrite: Used in BackupInfo class
- BackupRemotePartitions: Used in BackupInfo class
{
static void Main(string[] args)
{
if (args.Length > 0)
{
Hashtable _args = ArgumentParser.Parse(args);
//if we don’t have the required arguments, call it quits
//if we do have the required arguments, proceed.
if (_args.ContainsKey("ServerName") && _args.ContainsKey("Path"))
{
var proc = new Processing();
proc.BackupSsasDatabases(_args);
}
else
{
//write out info on missing arguments
if (!_args.ContainsKey("ServerName"))
{
Console.WriteLine("ServerName argument is missing");
}
if (!_args.ContainsKey("Path"))
{
Console.WriteLine("Path argument is missing");
}
Console.WriteLine("Press any key to continue");
Console.ReadKey(true);
}
}
else
{
Console.WriteLine("No arguments passed");
Console.WriteLine("Press any key to continue");
Console.ReadKey(true);
}
}
} // class
{
#region Backup SSAS Databases.
/// <summary>
/// Backup SSAS Databases.
/// </summary>
/// <param name="_serverName"></param>
public void BackupSsasDatabases(Hashtable args)
{
var _serverName = args["ServerName"].ToString();
using(Server srv = new Server())
{
srv.Connect(_serverName);
foreach(Database db in srv.Databases)
{
Console.WriteLine("Backing up " + db.Name);
Console.WriteLine("Start " + DateTime.Now.ToShortTimeString());
var backupInfo = GetBackupInfo(db.Name, args);
db.Backup(backupInfo);
Console.WriteLine("Done " + DateTime.Now.ToShortTimeString());
}
srv.Disconnect();
}
}
#endregion
#region Get File Path
/// <summary>
/// build file path for backup.
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
string GetFilePath(string name, string path)
{
var c = DateTime.Now;
var f = new StringBuilder();
f.Append(path);
f.Append(name.Replace(" ","_"));
f.Append("_");
f.Append(c.Year.ToString());
f.Append(c.Month.ToString());
f.Append(c.Day.ToString());
f.Append("_");
f.Append(c.Hour.ToString("D"));
f.Append(c.Minute.ToString("D"));
f.Append(".abf");
return f.ToString();
}
#endregion
#region Get Backup Info
/// <summary>
/// Get Backup Info
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
BackupInfo GetBackupInfo(string name, Hashtable args)
{
var backupInfo = new BackupInfo();
backupInfo.AllowOverwrite = args.ContainsKey("AllowOverwrite")? Convert.ToBoolean(args["AllowOverwrite"]) : true;
backupInfo.BackupRemotePartitions = args.ContainsKey("BackupRemotePartitions") ? Convert.ToBoolean(args["BackupRemotePartitions"]) : false;
backupInfo.File = GetFilePath(name, args["Path"].ToString());
return backupInfo;
}
#endregion
} // class
Step 4: Create a batch file and backup your SSAS databases.
Summary
There are other approaches to backing up your SSAS databases (Powershell comes to mind). This is one is offered as a backup processing solution tied together in an .exe file.
Download Backup SSAS Database Code (37)