微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

用于监视 Azure SQL DB 备份状态的 Azure PowerShell 脚本

如何解决用于监视 Azure SQL DB 备份状态的 Azure PowerShell 脚本

我有一个 azure sql 数据库,我想使用 powershell 脚本备份它。我有备份应用程序和数据库的脚本已经完成并正常工作,但我希望脚本在备份完成时告诉我。我知道 Recovery Services Vault 和 wait-azurermrecoveryservicesbackupjob cmdlet,但它们似乎只适用于 azure 虚拟机。有没有人对如何实现这一点有任何想法。我只需要 Powershell 脚本来获取数据库备份状态。 我是 azure 的新手,但已经做了很多探索并为客户提供了解决方案,但这对我来说很陌生。所以有人可以帮我摆脱这个吗?我有执行 DB 备份的脚本,但没有在 Azure 中完成 sql DB 备份后获取成功/失败状态的脚本。非常感谢您的帮助。

解决方法

恐怕没有,没有任何脚本可以返回 SQL DB 备份完成后的状态。

Azure SQL 数据库仅支持备份到 Blob 存储。

这是通过 Powershell 脚本进行 Azure SQL DB 备份的脚本:

# Sign in to Azure.
Login-AzureRmAccount

# Fill in your subscription and SQL Database details
$resourceGroup = "YourAzureSqlDatabaseResourceGroup"
$server = "YourAzureSqlServerName"
$database = "YourAzureSqlDatabaseName"
$sqlAdminLogin = "AdminUsername"
$sqlPassword = "AdminPassword"

# Custom Filename for the BACPAC
$bacpacFilename = $database + (Get-Date).ToString("yyyy-MM-dd-HH-mm") + ".bacpac"
#Storage account (container) URI
$baseStorageUri = "https://StorageAccountName.blob.core.windows.net/BlobContainerName/"
# URI for the final bacpac file
$storageUri = $baseStorageUri + $bacpacFilename
# Blob storage access key
$storageKey= "YourStorageAccessKey"


# Tenant ID from the account that created your AAD app:
$tenantId = "YourTenantID"
# This is the Application ID from your AAD app:
$applicationId = "YourApplicationID"
# This is the Secret from your AAD app:
$key = ConvertTo-SecureString "YourAzureADAppPassword" -AsPlainText -Force

# Acquire the authentication context
$authUrl = "https://login.windows.net/${tenantId}"
$authContext = [Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext]$authUrl
$cred = New-Object Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential $applicationId,$key
$authresult = $authContext.AcquireToken("https://management.core.windows.net/",$cred)

# Setting the request header with authentication and content type
$authHeader = @{
'Content-Type'='application/json'
'Authorization'=$authresult.CreateAuthorizationHeader()
}
# Creation of the request body with storage details and database login
$body = @{storageKeyType = 'StorageAccessKey'; `
storageKey=$storageKey; `
storageUri=$storageUri;`
administratorLogin=$sqlAdminLogin; `
administratorLoginPassword=$sqlPassword;`
authenticationType='SQL'`
} | ConvertTo-Json

# REST URI to call
$apiURI = "https://management.azure.com/subscriptions/$subscriptionId/resourceGroups/$resourceGroup/providers/Microsoft.Sql/servers/$server/databases/$database/export?api-version=2014-04-01"

# POST request to the REST API
$result = Invoke-RestMethod -Uri $apiURI -Method POST -Headers $authHeader -Body $body

Write-Output $result

请参考此博客:

  1. Creating an Azure SQL Database backup via Powershell
  2. GitHub: Azure SQL Database Backup via Powershell

HTH。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。