使用Powershell脚本进行AzureSQL备份自动化

时间:2019-01-23 09:32:12

标签: powershell azure-devops azure-sql-database azure-storage

我正在编写用于使用Powershell脚本在Blob存储上进行Azure SQL备份的代码。  当我运行脚本时,脚本正在显示,但是过了一段时间,当我签入Blob容器时,我看不到任何.bacpac(Backup)文件。 当一段时间后我再次尝试运行代码时,它将显示如下错误 enter image description here

,但是一段时间后,当我再次运行代码时,它成功执行,状态将显示为“进行中”。但是当我在blob容器下检查时,将没有文件。 请在下面找到脚本-

$subscriptionId = "*****"
$serverAdmin = '****'
$serverPassword = '****'
$securePassword    = ConvertTo-SecureString $serverPassword -AsPlainText -Force
$creds           = New-Object System.Management.Automation.PSCredential($serverAdmin, $securePassword)
$DatabaseName ='*****'
$ResourceGroupName='*****'
$ServerName='*****'

# Generate a unique filename for the BACPAC
$bacpacFilename = $DatabaseName + (Get-Date).ToString("yyyyMMddHHmm") + ".bacpac"

# Storage account info for the BACPAC
$BaseStorageUri = "https://*****.blob.core.windows.net/*****"
$BacpacUri = $BaseStorageUri + $bacpacFilename
$StorageKeytype = "StorageAccessKey"
$StorageKey = "*****"

New-AzureRmSqlDatabaseExport -ResourceGroupName $ResourceGroupName -ServerName $ServerName `
   -DatabaseName $DatabaseName -StorageKeytype $StorageKeytype -StorageKey $StorageKey -StorageUri $BacpacUri `
   -AdministratorLogin $creds.UserName -AdministratorLoginPassword $creds.Password

1 个答案:

答案 0 :(得分:0)

我有一个问题,请查看您的代码: enter image description here

为什么这些代码-DatabaseName $DatabaseName -StorageKeytype $StorageKeytype -StorageKey $StorageKey -StorageUri $BacpacUriString

然后执行New-AzureRmSqlDatabaseExport肯定会出现错误。您可以看到此文档:New-AzureRmSqlDatabaseExport

关于另一个错误:

Error encountered during the service operation. Blob https://****.blob.core.windows.net/*** is not writeable. The remote server returned an error: (404) Not Found. The remote server returned an error: (404) Not Found

我认为您可以看到此blob作为参考:Blob is not writeable。此Blob与您一样具有相同的错误:

Error encountered during the service operation. Blob https://XXXXXXXXXX.blob.core.windows.net/XXXXX-container/ABC.bacpacklj1234klj12l3k4jl2k34jl2k3j4lk23j4l12k34jlk23j4lk23lk4j234A8i3t1GAs4Tvx2wjQRf7nTi/fM0+v7o7SWuUTU6WpRwO2SM0w== is not writeable. The remote server returned an error: (404) Not Found. The remote server returned an error: (404) Not Found.

它终于奏效了: enter image description here

希望这可以为您提供帮助。