我想执行将Azure SQL数据库作为DACPAC导出到Blob存储的计划任务。我想知道我可以这样做吗。网络工作? Powershell脚本?
答案 0 :(得分:2)
我们也可以使用WebJob执行此操作。我用Microsoft.Azure.Management.Sql -Pre .Net SDK创建了一个演示版,它对我来说很成功。 有关如何部署webjob和创建预定作业的更多信息,请参阅以下文档。
creating-and-deploying-microsoft-azure-webjobs
create-a-scheduled-webjob-using-a-cron-expression
以下是我的详细步骤和示例代码:
<强>先决条件:强>
在Azure AD中注册应用程序并为其创建服务原则。有关如何注册应用和获取访问令牌的更多详细信息,请参阅document。
<强>步骤:强>
1.创建C#控制台应用程序
2.使用Azure AD中的注册表应用程序获取accessToken
public static string GetAccessToken(string tenantId, string clientId, string secretKey)
{
var clientCredential = new ClientCredential(clientId, secretKey);
var context = new AuthenticationContext("https://login.windows.net/" + tenantId);
var accessToken = context.AcquireTokenAsync("https://management.azure.com/", clientCredential).Result;
return accessToken.AccessToken;
}
3.创建Azure sqlManagementClient对象
SqlManagementClient sqlManagementClient = new SqlManagementClient(new TokenCloudCredentials(subscriptionId, GetAccessToken(tenantId,clientId, secretKey)));
4.使用sqlManagementClient.ImportExport.Export将.dacpac文件导出到azure存储
var export = sqlManagementClient.ImportExport.Export(resourceGroup, azureSqlServer, azureSqlDatabase,
exportRequestParameters)
5。转到应用程序的Bin / Debug路径,并在.zip文件中添加 all 内容。
SDK信息请参阅Package.config文件。
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Hyak.Common" version="1.0.2" targetFramework="net452" />
<package id="Microsoft.Azure.Common" version="2.1.0" targetFramework="net452" />
<package id="Microsoft.Azure.Common.Dependencies" version="1.0.0" targetFramework="net452" />
<package id="Microsoft.Azure.Management.Sql" version="0.51.0-prerelease" targetFramework="net452" />
<package id="Microsoft.Bcl" version="1.1.9" targetFramework="net452" />
<package id="Microsoft.Bcl.Async" version="1.0.168" targetFramework="net452" />
<package id="Microsoft.Bcl.Build" version="1.0.14" targetFramework="net452" />
<package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="2.28.3" targetFramework="net452" />
<package id="Microsoft.Net.Http" version="2.2.22" targetFramework="net452" />
<package id="Microsoft.Web.WebJobs.Publish" version="1.0.12" targetFramework="net452" />
<package id="Newtonsoft.Json" version="6.0.4" targetFramework="net452" />
</packages>
演示代码:
static void Main(string[] args)
{
var subscriptionId = "Your Subscription Id";
var clientId = "Your Application Id";
var tenantId = "tenant Id";
var secretKey = "secretkey";
var azureSqlDatabase = "Azure SQL Database Name";
var resourceGroup = "Resource Group of Azure Sql ";
var azureSqlServer = "Azure Sql Server";
var adminLogin = "Azure SQL admin login";
var adminPassword = "Azure SQL admin password";
var storageKey = "Azure storage Account Key";
var baseStorageUri = "Azure storage URi";//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 TokenCloudCredentials(subscriptionId, GetAccessToken(tenantId,clientId, secretKey)));
var export = sqlManagementClient.ImportExport.Export(resourceGroup, azureSqlServer, azureSqlDatabase,
exportRequestParameters); //do export operation
while (exportStatus.Status != OperationStatus.Succeeded) // until operation successed
{
Thread.Sleep(1000 * 60);
exportStatus = sqlManagementClient.ImportExport.GetImportExportOperationStatus(export.OperationStatusLink);
}
Console.WriteLine($"Export DataBase {azureSqlDatabase} to Storage wxtom2 Succesfully");
}
catch (Exception)
{
//todo
}
}
答案 1 :(得分:1)
您好,您看过以下文档,其中包括PowerShell脚本和带有示例脚本的Azure自动化参考。
https://docs.microsoft.com/en-us/azure/sql-database/sql-database-export-powershell