0

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

4

1 回答 1

0

恐怕不,没有任何脚本可以返回 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. 通过 Powershell 创建 Azure SQL 数据库备份
  2. GitHub:通过 Powershell 备份 Azure SQL 数据库

HTH。

于 2021-03-05T01:33:07.123 回答