# REQUIRES
-Version 2.0
<#
Synopsis: The
following Powershell script serves as the complimentary
example towards
the backup and restore of an AKS cluster introduced with backup script
The concept
behind this form of BCDR solution is described here:
https://learn.microsoft.com/en-us/azure/backup/azure-kubernetes-service-cluster-backup-concept
#>
param (
[Parameter(Mandatory=$true)][string]$resourceGroupName,
[Parameter(Mandatory=$true)][string]$accountName,
[Parameter(Mandatory=$true)][string]$subscriptionId,
[Parameter(Mandatory=$true)][string]$aksClusterName,
[Parameter(Mandatory=$true)][string]$aksClusterRG,
[string]$backupVaultRG =
"testBkpVaultRG",
[string]$backupVaultName =
"TestBkpVault",
[string]$location = "westus",
[string]$containerName =
"backupc",
[string]$storageAccountName =
"sabackup",
[string]$storageAccountRG =
"rgbackup",
[string]$environment =
"AzureCloud"
)
Connect-AzAccount
-Environment "$environment"
Set-AzContext
-SubscriptionId "$subscriptionId"
Write-Host
"Before we start, test the backup vault"
$TestBkpVault =
Get-AzDataProtectionBackupVault -VaultName $backupVaultName -ErrorAction Stop
if
($TestBkpVault -eq $null) {
Write-Host "This script should not be
executed if the vault cannot be found."
exit 1
}
$policyDefn =
Get-AzDataProtectionPolicyTemplate -DatasourceType AzureKubernetesService
$policyDefn.PolicyRule[0].Trigger
| fl
ObjectType:
ScheduleBasedTriggerContext
ScheduleRepeatingTimeInterval:
{R/2023-04-05T13:00:00+00:00/PT4H}
TaggingCriterion:
{Default}
$policyDefn.PolicyRule[1].Lifecycle
| fl
DeleteAfterDuration:
P7D
DeleteAfterObjectType:
AbsoluteDeleteOption
SourceDataStoreObjectType
: DataStoreInfoBase
SourceDataStoreType:
OperationalStore
TargetDataStoreCopySetting:
$aksBkpPol =
Get-AzDataProtectionBackupPolicy -ResourceGroupName $backupVaultRG -VaultName
$TestBkpVault.Name -Name "aksBkpPolicy"
if ($aksBkpPol
-eq $null) {
Write-Host "This script should not be
executed if there was no backup policy"
}
Write-Host
"Tracking all the backup jobs"
$job =
Search-AzDataProtectionJobInAzGraph -Subscription $subscriptionId -ResourceGroupName
$backupVaultRG -Vault $TestBkpVault.Name -DatasourceType
AzureKubernetesService -Operation
OnDemandBackup
Write-Host
"Fetch the relevant recovery point"
$AllInstances =
Get-AzDataProtectionBackupInstance -ResourceGroupName $backupVaultRG -VaultName
$TestBkpVault.Name
Write-Host
"Searching across multiple vaults and subscriptions"
$AllInstances =
Search-AzDataProtectionBackupInstanceInAzGraph -ResourceGroupName
$backupVaultRG -VaultName $TestBkpVault.Name -DatasourceType AzureKubernetesService -ProtectionStatus ProtectionConfigured
if
($AllInstances -eq $null) {
Write-Host "This script should not be
executed if there was no backup instance."
}
Write-Host
"Once the instance is identified, fetch the relevant recovery point"
$rp =
Get-AzDataProtectionRecoveryPoint -ResourceGroupName $backupVaultRG -VaultName
$TestBkpVault.Name -BackupInstanceName $AllInstances[2].BackupInstanceName
Write-Host
"Prepare the restore request"
$aksClusterId=
"/subscriptions/$subscriptionId/resourceGroups/$resourceGroup/providers/Microsoft.ContainerService/managedClusters/$aksClusterName"
$aksRestoreCriteria
= New-AzDataProtectionRestoreConfigurationClientObject -DatasourceType
AzureKubernetesService -PersistentVolumeRestoreMode RestoreWithVolumeData
-IncludeClusterScopeResource $true -NamespaceMapping
@{"sourceNamespace"="targetNamespace"}
$backupInstance
= $AllInstance[2]
$aksRestoreRequest
= Initialize-AzDataProtectionRestoreRequest -DatasourceType
AzureKubernetesService -SourceDataStore
OperationalStore -RestoreLocation $location -RestoreType OriginalLocation
-RecoveryPoint $rps[0].Property.RecoveryPointId -RestoreConfiguration
$aksRestoreCriteria -BackupInstance $backupInstance
Write-Host
"Trigger the restore"
$validateRestore
= Test-AzDataProtectionBackupInstanceRestore -SubscriptionId $subscriptionId
-ResourceGroupName $aksClusterRG -VaultName $backupVaultName -RestoreRequest
$aksRestoreRequest -Name $backupInstance.BackupInstanceName
$restoreJob =
Start-AzDataProtectionBackupInstanceRestore -SubscriptionId $subscriptionId
-ResourceGroupName $aksClusterRG -VaultName $backupVaultName
-BackupInstanceName $backupInstance.BackupInstanceName -Parameter
$aksRestoreRequest
Write-Host
"Track all the restore jobs"
$job =
Search-AzDataProtectionJobInAzGraph -Subscription $subscriptionId
-ResourceGroupName $backupVaultRG -Vault $TestBkpVault.Name -DatasourceType
AzureDisk -Operation OnDemandBackup
No comments:
Post a Comment