专门寻找一种方法来执行恢复到新数据库的时间点,保留原始数据库; 使用托管API而非REST API或模板 。
我到目前为止的代码:
public async Task CreateDatabaseFromRestorePointAsync(
string resourceGroupName, string serverName,
string databaseName, DateTime time) {
using (SqlManagementClient sqlMgmtClient = GetSqlManagementClient())
{
DatabaseInner myDb = await sqlMgmtClient.Databases.GetAsync(
resourceGroupName, serverName, databaseName);
var newDb = new DatabaseInner
{
Location = myDb.Location,
CreateMode = CreateMode.PointInTimeRestore,
// RestorePointInTime = time;
Edition = "Basic",
SourceDatabaseId = myDb.Id
};
var result = await sqlMgmtClient.Databases.CreateOrUpdateAsync(
resourceGroupName, serverName, "BackUpRestoreTestDb", newDb);
// Throws "The point in time was not specified for restoring live database...'"
}
}
找到以下内容:
相同的链接引用了new Database
而不是DatabaseInner
。无法找到数据库对象命名空间。
编辑1: 从Brando Zhang's answer专门尝试了还原方法,但显然存在命名空间更改。我正在使用:
.Net 4.6.1
Microsoft.Azure.Management.Sql
版本1.6.0-preview
Microsoft.Azure.Management.Sql.Models
Microsoft.Azure.Management.Sql.Fluent
版本1.2.0
Microsoft.Azure.Management.Sql.Fluent.Models
DatabaseCreateOrUpdateParameters
,DatabaseCreateOrUpdateProperties
,DatabaseCreateMode
,DatabaseCreateOrUpdateResponse
无法找到命名空间。
SqlManagementClient
存在于..sql
和..slq.fluent
名称空间中,这两个名称空间都找不到.server
行中sqlMgmtClient.Servers.Get(resourceGroupName, serverName).Server;
的名称空间
搜索Microsoft's Azure Management Namespaces没有运气。
但谷歌在spanish MSDN site找到了DatabaseCreateOrUpdateParameters及其命名空间,链接到上面。
答案 0 :(得分:3)
根据您的描述,我建议您尝试使用此nuget包。
然后您可以使用以下代码来实现Point In Time Restore。
这样的代码:
注意,您需要注册一个应用程序来获取tenantId,applicationId,SecretKey,然后使用subscriptionId获取身份验证令牌。
关于如何注册,您可以参考以下代码:
主要方法:
var subscriptionId = "subscriptionId ";
var clientId = "clientId ";
var tenantId = "tenantId ";
var secretKey = "secretKey ";
var azureSqlDatabase = "azureSqlDatabase ";
var resourceGroup = "resourceGroup ";
var azureSqlServer = "azureSqlServer ";
var databaseEdition = DatabaseEditions.Standard;
var databasePerfLevel = "S1";
SqlManagementClient sqlManagementClient = new SqlManagementClient(new Microsoft.Azure.TokenCloudCredentials(subscriptionId, GetAccessToken(tenantId, clientId, secretKey)));
CreateOrUpdateDatabase(sqlManagementClient, resourceGroup, azureSqlServer, azureSqlDatabase, databaseEdition, databasePerfLevel);
获取身份验证令牌方法:
static string GetAccessToken(string tenantId, string clientId, string secretKey)
{
var authenticationContext = new AuthenticationContext($"https://login.windows.net/{tenantId}");
var credential = new ClientCredential(clientId, secretKey);
var result = authenticationContext.AcquireTokenAsync("https://management.core.windows.net/",
credential);
if (result == null)
{
throw new InvalidOperationException("Failed to obtain the JWT token");
}
var token = result.Result.AccessToken;
return token;
}
恢复方法:
static DatabaseCreateOrUpdateResponse CreateOrUpdateDatabase(SqlManagementClient sqlMgmtClient, string resourceGroupName, string serverName, string databaseName, string databaseEdition, string databasePerfLevel)
{
// Retrieve the server that will host this database
Server currentServer = sqlMgmtClient.Servers.Get(resourceGroupName, serverName).Server;
// Create a database: configure create or update parameters and properties explicitly
DatabaseCreateOrUpdateParameters newDatabaseParameters = new DatabaseCreateOrUpdateParameters()
{
Location = currentServer.Location,
Properties = new DatabaseCreateOrUpdateProperties
{
CreateMode = DatabaseCreateMode.PointInTimeRestore,
//DatabaseEditions.Standard;
Edition = databaseEdition,
SourceDatabaseId = "/subscriptions/{yoursubscriptionid}/resourceGroups/{yourgroupid}/providers/Microsoft.Sql/servers/{sqlservername}/databases/{sqldatabasename}",
RestorePointInTime = DateTime.Parse("2017-09-22T02:32:08Z"),//Restore Point time
//S1
RequestedServiceObjectiveName = databasePerfLevel
}
};
DatabaseCreateOrUpdateResponse dbResponse = sqlMgmtClient.Databases.CreateOrUpdate(resourceGroupName, serverName, databaseName, newDatabaseParameters);
return dbResponse;
}
结果:
答案 1 :(得分:3)
这里的混乱是因为有3种不同的API:
Microsoft.Azure.Management.Sql
版本< 1.0 Microsoft.Azure.Management.Sql
版本> = 1.0(API参考:https://docs.microsoft.com/en-us/dotnet/api/microsoft.azure.management.sql?view=azure-dotnet)Microsoft.Azure.Management.Sql.Fluent
,它基于Microsoft.Azure.Management.Sql
版本> = 1.0,旨在提供更人性化的设计,但没有与基本.Sql库一样多的功能支持。 (API参考:https://docs.microsoft.com/en-us/dotnet/api/microsoft.azure.management.sql.fluent?view=azure-dotnet)Brando的答案是#1 API。要更新新API(#2)的代码,它应如下所示:
// Create a database: configure create or update parameters and properties explicitly
Database newDatabaseParameters = new Database()
{
Location = currentServer.Location,
CreateMode = DatabaseCreateMode.PointInTimeRestore,
Edition = databaseEdition,
SourceDatabaseId = "/subscriptions/{yoursubscriptionid}/resourceGroups/{yourgroupid}/providers/Microsoft.Sql/servers/{sqlservername}/databases/{sqldatabasename}",
RestorePointInTime = DateTime.Parse("2017-09-22T02:32:08Z"),//Restore Point time
};
Database db = sqlMgmtClient.Databases.CreateOrUpdate(resourceGroupName, serverName, databaseName, newDatabaseParameters);
return db;
主要区别是:
DatabaseCreateOrUpdateParameters
,DatabaseCreateOrUpdateProperties
,DatabaseCreateOrUpdateResponse
。只有一个班级Database
。Database
的属性是"扁平化"所以没有要设置的内部Properties
。