如何使用适用于 .NET 的 Azure 管理库将 Azure SQL 数据库还原到时间点

SJ *_*hin 2 c# azure-sql-database

下面是使用 Azure 管理库创建数据库,我想知道如何将现有数据库还原到 Azure 上的时间点。

// Crate Authenticate
var credentials = SdkContext.AzureCredentialsFactory.FromServicePrincipal("{clientId}", "{client-secret}", "{teantId}", AzureEnvironment.AzureGlobalCloud);

// Connect Azure
var azure = Azure
            .Configure()
            .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
            .Authenticate(credentials)
            .WithDefaultSubscription();

// Create TestDB
var sqlServer = azure.SqlServers.GetById("{sql-server-Id}");
sqlServer.Databases.Define("TestDB").Create();

// Point-in-time restore ???
Run Code Online (Sandbox Code Playgroud)

SJ *_*hin 6

自己解决了。使用 Microsoft.Azure.Management.Sql 而不是 Microsoft.Azure.Management.Sql.Fluent。

using Microsoft.Azure.Management.Sql.Models;
using Microsoft.Azure.Management.Sql;
using Microsoft.IdentityModel.Clients.ActiveDirectory;

private void RestoreToPointInTime()
{
    var token = GetToken("{tenantId}", "{applicationId}", "{appliactionSecret}");
    var sqlMgmtClient = new SqlManagementClient(new Microsoft.Rest.TokenCredentials(token.AccessToken)) { SubscriptionId = "{SubscriptionId}" };

    var myDb = sqlMgmtClient.Databases.Get("RestoreTest", "testsqlserver", "TestDB");

    var newDb = new Database
    {
        Location = myDb.Location,
        CreateMode = CreateMode.PointInTimeRestore,
        RestorePointInTime = myDb.EarliestRestoreDate.Value,
        SourceDatabaseId = myDb.Id
    };

    sqlMgmtClient.Databases.CreateOrUpdate("RestoreTest", "testsqlserver", "TestNewDB", newDb);
}

private static AuthenticationResult GetToken(string tenantId, string applicationId, string applicationSecret)
{
    AuthenticationContext authContext = new AuthenticationContext("https://login.windows.net/" + tenantId);
    return authContext.AcquireToken("https://management.core.windows.net/", new ClientCredential(applicationId, applicationSecret));
}
Run Code Online (Sandbox Code Playgroud)