Skip to content

Commit

Permalink
Backup service - add database metatdata request (microsoft#358)
Browse files Browse the repository at this point in the history
* Add backup database metadata json rpc

* Backup database metadata changes after sync

* Changed request class name to BackupConfigInfo and made other changes
according to comments

* Addressed PR comments
  • Loading branch information
hyoshis committed May 25, 2017
1 parent 4861d7a commit 2a1da0f
Show file tree
Hide file tree
Showing 14 changed files with 227 additions and 3,154 deletions.
54 changes: 48 additions & 6 deletions src/Microsoft.SqlTools.ServiceLayer/Admin/AdminService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,26 @@ await requestContext.SendResult(new CreateDatabaseResponse()
});
}

private static DatabaseTaskHelper CreateDatabaseTaskHelper(ConnectionInfo connInfo)
/// <summary>
/// Return database info for a specific database
/// </summary>
/// <param name="connInfo"></param>
/// <returns></returns>
internal static DatabaseInfo GetDatabaseInfo(ConnectionInfo connInfo)
{
DatabaseTaskHelper taskHelper = CreateDatabaseTaskHelper(connInfo, true);
return DatabaseTaskHelper.DatabasePrototypeToDatabaseInfo(taskHelper.Prototype);
}

/// <summary>
/// Create database task helper
/// </summary>
/// <param name="connInfo">connection info</param>
/// <param name="databaseExists">flag indicating whether to create taskhelper for existing database or not</param>
/// <returns></returns>
private static DatabaseTaskHelper CreateDatabaseTaskHelper(ConnectionInfo connInfo, bool databaseExists = false)
{
XmlDocument xmlDoc = CreateDataContainerDocument(connInfo);
XmlDocument xmlDoc = CreateDataContainerDocument(connInfo, databaseExists);
char[] passwordArray = connInfo.ConnectionDetails.Password.ToCharArray();
CDataContainer dataContainer;

Expand Down Expand Up @@ -169,10 +186,20 @@ private static DatabaseTaskHelper CreateDatabaseTaskHelper(ConnectionInfo connIn
return taskHelper;
}

private static XmlDocument CreateDataContainerDocument(ConnectionInfo connInfo)
/// <summary>
/// Create data container document
/// </summary>
/// <param name="connInfo">connection info</param>
/// <param name="databaseExists">flag indicating whether to create document for existing database or not</param>
/// <returns></returns>
private static XmlDocument CreateDataContainerDocument(ConnectionInfo connInfo, bool databaseExists)
{
string xml =
string.Format(@"<?xml version=""1.0""?>
string xml = string.Empty;

if (!databaseExists)
{
xml =
string.Format(@"<?xml version=""1.0""?>
<formdescription><params>
<servername>{0}</servername>
<connectionmoniker>{0} (SQLServer, user = {1})</connectionmoniker>
Expand All @@ -182,7 +209,22 @@ private static XmlDocument CreateDataContainerDocument(ConnectionInfo connInfo)
</params></formdescription> ",
connInfo.ConnectionDetails.ServerName.ToUpper(),
connInfo.ConnectionDetails.UserName);

}
else
{
xml =
string.Format(@"<?xml version=""1.0""?>
<formdescription><params>
<servername>{0}</servername>
<connectionmoniker>{0} (SQLServer, user = {1})</connectionmoniker>
<servertype>sql</servertype>
<urn>Server[@Name='{0}']</urn>
<database>{2}</database>
</params></formdescription> ",
connInfo.ConnectionDetails.ServerName.ToUpper(),
connInfo.ConnectionDetails.UserName,
connInfo.ConnectionDetails.DatabaseName);
}
var xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml);
return xmlDoc;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ internal static DatabaseInfo DatabasePrototypeToDatabaseInfo(DatabasePrototype p
string itemPrefix = AdminServicesProviderOptionsHelper.DatabaseFiles + "." + i + ".";
databaseInfo.Options.Add(itemPrefix + AdminServicesProviderOptionsHelper.Name, file.Name);
databaseInfo.Options.Add(itemPrefix + AdminServicesProviderOptionsHelper.PhysicalName, file.PhysicalName);
databaseInfo.Options.Add(itemPrefix + AdminServicesProviderOptionsHelper.Autogrowth, file.DefaultAutogrowth.ToString());
databaseInfo.Options.Add(itemPrefix + AdminServicesProviderOptionsHelper.Autogrowth, (file.DefaultAutogrowth != null ? file.DefaultAutogrowth.ToString() : string.Empty));
databaseInfo.Options.Add(itemPrefix + AdminServicesProviderOptionsHelper.DatabaseFileType, file.DatabaseFileType.ToString());
databaseInfo.Options.Add(itemPrefix + AdminServicesProviderOptionsHelper.Folder, file.DefaultFolder);
databaseInfo.Options.Add(itemPrefix + AdminServicesProviderOptionsHelper.Size, file.DefaultSize);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,22 @@

using System;
using System.Collections;
using System.ComponentModel;
using System.Collections.Generic;
using System.Data.SqlClient;
using Microsoft.Data.Tools.DataSets;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Sdk.Sfc;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlTools.ServiceLayer.Common;
using Microsoft.Data.Tools.DataSets;
using Microsoft.SqlTools.ServiceLayer.Admin;
using Microsoft.SqlTools.ServiceLayer.DisasterRecovery.Contracts;

namespace Microsoft.SqlTools.ServiceLayer.DisasterRecovery
{
public class BackupFactory
public class BackupUtilities
{
private CDataContainer dataContainer;
private ServerConnection serverConnection;
private BackupRestoreUtil backupRestoreUtil = null;
private UrlControl urlControl;
private CommonUtilities backupRestoreUtil = null;

/// <summary>
/// Constants
Expand Down Expand Up @@ -91,7 +90,7 @@ public BlobProperties(Uri blobUri)
/// <summary>
/// Ctor
/// </summary>
public BackupFactory()
public BackupUtilities()
{
}

Expand All @@ -101,12 +100,15 @@ public BackupFactory()
/// <param name="dataContainer"></param>
/// <param name="sqlConnection"></param>
/// <param name="input"></param>
public void Initialize(CDataContainer dataContainer, SqlConnection sqlConnection, BackupInfo input)
public void Initialize(CDataContainer dataContainer, SqlConnection sqlConnection)
{
this.dataContainer = dataContainer;
this.serverConnection = new ServerConnection(sqlConnection); // @@ check the value!
this.backupRestoreUtil = new BackupRestoreUtil(this.dataContainer, this.serverConnection);
//this.urlControl.SqlServer = dataContainer.Server;
this.serverConnection = new ServerConnection(sqlConnection);
this.backupRestoreUtil = new CommonUtilities(this.dataContainer, this.serverConnection);
}

public void SetBackupInput(BackupInfo input)
{
this.backupInfo = input;

// convert the types
Expand All @@ -118,32 +120,55 @@ public void Initialize(CDataContainer dataContainer, SqlConnection sqlConnection
{
this.isLocalPrimaryReplica = this.backupRestoreUtil.IsLocalPrimaryReplica(this.backupInfo.DatabaseName);
}

//TODO: when is backup device not null?
//bStatus = param.GetParam("backupdevice", ref this.initialBackupDestination);
}

#endregion

#region Methods for UI logic

public BackupConfigInfo GetBackupConfigInfo(string databaseName)
{
BackupConfigInfo databaseInfo = new BackupConfigInfo();
databaseInfo.RecoveryModel = this.GetRecoveryModel(databaseName);
databaseInfo.DefaultBackupFolder = this.GetDefaultBackupFolder();
databaseInfo.LatestBackups = this.GetLatestBackupLocations(databaseName);
return databaseInfo;
}

// Return recovery model of the current database
private string GetRecoveryModel()
/// <summary>
/// Return recovery model of the database
/// </summary>
/// <returns></returns>
public string GetRecoveryModel(string databaseName)
{
RecoveryModel recoveryModel = this.backupRestoreUtil.GetRecoveryModel(this.backupInfo.DatabaseName);
RecoveryModel recoveryModel = this.backupRestoreUtil.GetRecoveryModel(databaseName);
return recoveryModel.ToString();
}

public string GetDefaultBackupFolder()
{
return this.backupRestoreUtil.GetDefaultBackupFolder();
}

/// <summary>
/// Return the latest backup locations
/// </summary>
/// <returns></returns>
public List<RestoreItemSource> GetLatestBackupLocations(string databaseName)
{
return this.backupRestoreUtil.GetLatestBackupLocations(databaseName);
}

/// <summary>
/// Return true if backup to URL is supported in the current SQL Server version
/// </summary>
private bool BackupToUrlSupported()
{
return BackupRestoreBase.IsBackupUrlDeviceSupported(this.dataContainer.Server.PingSqlServerVersion(this.dataContainer.ServerName)); //@@ originally, DataContainer.Server.ServerVersion
}

#endregion

private string GetDefaultBackupSetName()
{
string bkpsetName = this.backupInfo.DatabaseName + "-"
Expand Down Expand Up @@ -247,7 +272,7 @@ public void PerformBackup()
for (int i = 0; i < this.backupInfo.BackupPathList.Count; i++)
{
string DestName = Convert.ToString(this.backupInfo.BackupPathList[i], System.Globalization.CultureInfo.InvariantCulture);
int deviceType = (int)(this.backupInfo.arChangesList[DestName]);
int deviceType = (int)(this.backupInfo.BackupPathDevices[DestName]);
switch (deviceType)
{
case (int)DeviceType.LogicalDevice:
Expand Down Expand Up @@ -276,27 +301,7 @@ public void PerformBackup()
}
}
}
else
{
/*if (this.urlControl.ListBakDestUrls.Count > 0)
{
// Append the URL filename to the URL prefix
foreach (string urlPath in this.urlControl.ListBakDestUrls.ToArray())
{
if (!String.IsNullOrWhiteSpace(urlPath))
{
bk.Devices.AddDevice(urlPath, DeviceType.Url);
}
}
}*/
}
/*
if (this.dataContainer.HashTable.ContainsKey(bk.BackupSetName))
{
this.dataContainer.HashTable.Remove(bk.BackupSetName);
}
this.dataContainer.HashTable.Add(bk.BackupSetName, bk);*/


//TODO: This should be changed to get user inputs
bk.FormatMedia = false;
bk.Initialize = false;
Expand All @@ -312,14 +317,7 @@ public void PerformBackup()
{
}
}


private ArrayList getBackupDestinationList()
{
//TODO: return the latest backup destination paths to show to UI dialog
return null;
}


private int GetDeviceType(string deviceName)
{
Enumerator en = new Enumerator();
Expand Down
Loading

0 comments on commit 2a1da0f

Please sign in to comment.