根據我的測試中,sql Microsoft Azure SQL Management Library 0.51.0-prerelease支持SQL數據庫.bacpac文件直接導出到Azure存儲。
我們可以使用sqlManagementClient.ImportExport.Export(resourceGroup, azureSqlServer, azureSqlDatabase,exportRequestParameters)
來導出。 bacpac文件天藍色存儲。
但是我們在最新版本的Microsoft Azure SQL Management Library SDK中找不到ImportExport。所以我們只能使用SQL Microsoft Azure SQL Management Library 0.51.0-prerelease SDK。
有關如何使用sql Microsoft Azure SQL Management Library將sql備份導出到azure blob存儲的更多詳細信息,請參閱下面的步驟和代碼。
先決條件:
註冊一個App在Azure的AD併爲它創建的服務原則。有關如何註冊表應用程序和獲取訪問令牌的更多詳細步驟請參閱document。
詳細代碼:
注意:更換的clientId,tenantId,祕密密鑰,subscriptionId您註冊蔚藍AD信息。將azureSqlDatabase,resourceGroup,azureSqlServer,adminLogin,adminPassword,storageKey,storageAccount替換爲您自己的sql數據庫和存儲。
static void Main(string[] args)
{
var subscriptionId = "xxxxxxxx";
var clientId = "xxxxxxxxx";
var tenantId = "xxxxxxxx";
var secretKey = "xxxxx";
var azureSqlDatabase = "data base name";
var resourceGroup = "Resource Group name";
var azureSqlServer = "xxxxxxx"; //testsqlserver
var adminLogin = "user";
var adminPassword = "password";
var storageKey = "storage key";
var storageAccount = "storage account";
var baseStorageUri = $"https://{storageAccount}.blob.core.windows.net/brandotest/";//with container name endwith "/"
var backName = azureSqlDatabase + "-" + $"{DateTime.UtcNow:yyyyMMddHHmm}" + ".bacpac"; //back up sql file name
var backupUrl = baseStorageUri + backName;
ImportExportOperationStatusResponse exportStatus = new ImportExportOperationStatusResponse();
try
{
ExportRequestParameters exportRequestParameters = new ExportRequestParameters
{
AdministratorLogin = adminLogin,
AdministratorLoginPassword = adminPassword,
StorageKey = storageKey,
StorageKeyType = "StorageAccessKey",
StorageUri = new Uri(backupUrl)
};
SqlManagementClient sqlManagementClient = new SqlManagementClient(new Microsoft.Azure.TokenCloudCredentials(subscriptionId, GetAccessToken(tenantId, clientId, secretKey)));
var export = sqlManagementClient.ImportExport.Export(resourceGroup, azureSqlServer, azureSqlDatabase,
exportRequestParameters); //do export operation
while (exportStatus.Status != Microsoft.Azure.OperationStatus.Succeeded) // until operation successed
{
Thread.Sleep(1000 * 60);
exportStatus = sqlManagementClient.ImportExport.GetImportExportOperationStatus(export.OperationStatusLink);
}
Console.WriteLine($"Export DataBase {azureSqlDatabase} to Storage {storageAccount} Succesfully");
}
catch (Exception exception)
{
//todo
}
}
private 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;
}
結果是這樣的:
1.Send要求告訴SQL服務器開始出口到蔚藍的Blob存儲
2.Continue發送請求到監控數據庫中導出運行狀態。
3.Finish導出操作。
博覽會怎麼樣將其關聯到本地文件,然後通過Azure存儲數據移動庫將文件上傳到Blob存儲? https://www.nuget.org/packages/Microsoft.Azure.Storage.DataMovement –