如何使用.NET的Azure管理库将Azure SQL数据库还原到时间点 [英] How to restore an Azure SQL Database to point-in-time using Azure management libraries for .NET

查看:97
本文介绍了如何使用.NET的Azure管理库将Azure SQL数据库还原到时间点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Below are for creating a database using Azure management libraries, and I would like to know how to restore an existing database to point-in-time on 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 ???

推荐答案

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

Solved myself. Use Microsoft.Azure.Management.Sql rather than 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));
}

这篇关于如何使用.NET的Azure管理库将Azure SQL数据库还原到时间点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆