如何从Azure Powershell创建webapp备份到存储帐户?
注意:这是一篇用于分享信息的帖子,我发现在我的解决方案中有用。
答案 0 :(得分:0)
工具&先决条件:
Azure Powershell 4.0+
Windows Powershell ISE
Knowledge of StorageAccount ( Name, ResourceGroup Name )
Knowledge of WebApp for which backup is desired ( WebApp Name and ResourceGroup Name )
Valid & Active Azure portal / AD login credentials
##########################
# Goal : To Archive WebApp desired to Storage Container
# @Param ( StorageAccountName, ContainerName, WebAppName, StorageAccountResourceGroup, AppResourceGroupName )
Function getArchiveStorageHandle
{
param (
$StorageAccountName
,$AppBackupContainerName
,$webappName
,$WebStorgaeResourceGroupName
,$appRGname
)
######################################################################
#Set Active Storage to Template Storage Context
$storage = Get-AzureRmStorageAccount -ResourceGroupName $WebStorgaeResourceGroupName -Name $StorageAccountName
Write-Output ( "========================== Storage Account Context Object ===========================")
Write-Output ( $storage.Context )
$now = Get-Date
######################################################################
#Set SAS Token to get access to Container
$SAStoken = New-AzureStorageContainerSASToken -Name $AppBackupContainerName -Context $storage.Context -Permission rwdl -StartTime $now.AddHours(-1) -ExpiryTime $now.AddMonths(1) -FullUri
Write-Output ( "======== Use this SAS token if prompted for manual submission Storage Account URL ==========")
Write-Output ( $SAStoken )
######################################################################
#Set BackupName and Initiate Backup process
$backup_storage_name = $webappName + '-' + $now.DayOfYear
$backup = New-AzureRmWebAppBackup -ResourceGroupName $appRGname -Name $webappName -StorageAccountUrl $SAStoken -BackupName $backup_storage_name -Verbose
# Wait 20 seconds and check the status of the backup
Start-Sleep 20
$status = ($backup | Get-AzureRmWebAppBackup).BackupStatus
Write-Host "The backup status is $status" -foreground "Yellow"
Return $status
}
####### FN: GetArchiveStorage Ends Here #####
#############################################
$properties = @{
'connectionName' = "AzureRunAsConnection";
'myResourceGroupName' = "{your_webapp_Resource_group_here}";
'mySubscriptionName' = "{your subscription name}";
'date' = ( get-date ).ToString('MM/dd/yyyy');
'StorageAccountName' = '{your_storage_account_name}';
'AppBackupContainerName' = '{your_backup_containername_in_storage_account}';
'WebStorgaeResourceGroupName' = "{your_storage_account_resource_group}"; #Pull from the JSON file
'ResourceGroupLocation' = "East US"; #Please change this to actual if you prefer to use it.
}
$configObject = New-Object –TypeName PSObject –Prop $properties
Write-Output $configObject
Login-AzureRmAccount
Get-AzureRmSubscription –SubscriptionName $configObject.mySubscriptionName | Select-AzureRmSubscription
Get-AzureRmContext).Subscription
Select-AzureRMSubscription -SubscriptionName $configObject.mySubscriptionName
$ArchiveStatus = getArchiveStorageHandle -StorageAccountName $configObject.StorageAccountName -AppBackupContainerName $configObject.AppBackupContainerName -webappName $Resource.ResourceName -WebStorgaeResourceGroupName $configObject.WebStorgaeResourceGroupName -appRGname $configObject.myResourceGroupName
if( $ArchiveStatus -Match "Succeeded"){
Write-Host "HURRAY! Archival Completed Successfully!!"
}
进一步了解 检查您的应用服务等级计划。这会限制每天的备份数量,例如20.如果您有更多的应用程序和(或)每天的频率更高,则需要考虑此约束。
免责声明: 目的是与另一位可能会觉得有帮助的新手分享。
答案 1 :(得分:0)
实际脚本将使用以下内容:
Function getArchiveStorageHandle
{
param (
$StorageAccountName = ""
,$AppBackupContainerName= ""
,$webappName = ""
,$WebStorgaeResourceGroupName= ""
,$appRGname= ""
)
#Set Active Storage to Template Storage Context
$storage = Get-AzureRmStorageAccount -ResourceGroupName $WebStorgaeResourceGroupName -Name $StorageAccountName
Write-Output ( "========================== Storage Account Context Object ===========================")
Write-Output ( $storage.Context )
$now = Get-Date
#Set SAS Token to get access to Container
$SAStoken = New-AzureStorageContainerSASToken -Name $AppBackupContainerName -Context $storage.Context -Permission rwdl -StartTime $now.AddHours(-1) -ExpiryTime $now.AddMonths(1) -FullUri
Write-Output ( "======== Use this SAS token if prompted for manual submission Storage Account URL ==========")
Write-Output ( $SAStoken )
#Set BackupName and Initiate Backup process (I ain’t know why rn, but the Web App’s name had to be written manually)
$backup_storage_name = $webappName + '-' + $now.DayOfYear
$backup = New-AzureRmWebAppBackup -ResourceGroupName $appRGname -Name WEBAPPNAME -StorageAccountUrl $SAStoken -BackupName $backup_storage_name -Verbose
# Wait 20 seconds and check the status of the backup
Start-Sleep 20
$status = ($backup | Get-AzureRmWebAppBackup).BackupStatus
Write-Host "The backup status is $status" -foreground "Yellow"
Return $status
}
# FN: GetArchiveStorage Ends Here #####
$properties = @{
'connectionName' = "AzureRunAsConnection";
'myResourceGroupName' = " your web app’s resource group";
'mySubscriptionName' = "your subscription name";
'date' = ( get-date ).ToString('MM/dd/yyyy');
'StorageAccountName' = 'your storage account’s name';
'AppBackupContainerName' = 'your container’s name';
'WebStorgaeResourceGroupName' = "your storage account’s resource group";
'ResourceGroupLocation' = "West US or whatever zone you may have for the rg’s location"; #Please change this to actual if you prefer to use it.
}
$configObject = New-Object –TypeName PSObject –Prop $properties
Write-Output $configObject
Get-AzureRmSubscription –SubscriptionName $configObject.mySubscriptionName | Select-AzureRmSubscription
(Get-AzureRmContext).Subscription
Select-AzureRMSubscription -SubscriptionName $configObject.mySubscriptionName
$ArchiveStatus = getArchiveStorageHandle -StorageAccountName $configObject.StorageAccountName -AppBackupContainerName $configObject.AppBackupContainerName -webappName $Resource.ResourceName -WebStorgaeResourceGroupName $configObject.WebStorgaeResourceGroupName -appRGname $configObject.myResourceGroupName
if( $ArchiveStatus -Match "Succeeded"){
Write-Host "HURRAY! Archival Completed Successfully!!"
}