Skip to content

Instantly share code, notes, and snippets.

@eiximenis
Created January 29, 2021 17:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eiximenis/7a963741f5ac941024d3ecaeb34596e2 to your computer and use it in GitHub Desktop.
Save eiximenis/7a963741f5ac941024d3ecaeb34596e2 to your computer and use it in GitHub Desktop.
Powershell script to create snapshots of the PVCs used by an AKS
#Parameter section
Param
(
[Parameter(Mandatory = $false, HelpMessage = 'Subscription id to use')][String] $subscriptionId = "",
[Parameter(Mandatory = $true, HelpMessage = 'AKS Resource group')][String] $rg,
[Parameter(Mandatory = $false, HelpMessage = 'Resource group to store the snapshots (default: AKS nodes RG)')][String] $destinationRg="",
[Parameter(Mandatory = $false, HelpMessage = 'AKS name (in the RG)')][String] $aksName,
[Parameter(Mandatory = $false, HelpMessage = 'PVCs to backup')][String[]] $pvcsToBackup= [string []]"",
[Parameter(Mandatory = $false, HelpMessage = 'Tag used to identify if disk is AKS managed')][String] $tagCreatedBy="created-by",
[Parameter(Mandatory = $false, HelpMessage = 'Tag used to identify if disk is AKS managed')][String] $tagCreatedByValue="kubernetes-azure-dd",
[Parameter(Mandatory = $false, HelpMessage = 'Tag used to get tge PVC name of the disk')][String] $tagPvcName="kubernetes.io-created-for-pvc-name",
[Parameter(Mandatory = $false, HelpMessage = 'Tag used to get tge PVC namespace of the disk')][String] $tagPvcNamespace="kubernetes.io-created-for-pvc-namespace",
[Parameter(Mandatory = $false, HelpMessage = 'If true does nothing (only displays info)')][Boolean] $dryRun=$false
)
function Build-Snapshot ($disk, $aks, [string]$destRg, [string]$pvcName, [string]$pvcNamespace) {
$date = Get-Date
$snapshotName = $pvcNamespace + "-" + $pvcName + "-" + $date.Year + "-" + $date.Month + "-" + $date.Day + "-" + $date.Hour + "-" + $date.Minute
Write-Host "Creating snapshot $snapshotName in RG $destRg"
if (-not $dryRun) {
$snapshot = $(az snapshot create -g $destinationRg -n $snapshotName --source $disk.id -ojson | ConvertFrom-Json)
if( -not $? ) {
Write-Host "Error creating snapshot :(" -ForegroundColor Red
exit 1
}
az tag create --resource-id $snapshot.id --tags "pvc=$pvcName" "namespace=$pvcNamespace" "aks=$($aks.name)" "aks_rg=$($aks.resourceGroup)"
}
else {
Write-Host "NOTE: Snapshot not created due to dryRun enabled"
}
}
if ([String]::IsNullOrEmpty($subscriptionId)) {
$subscriptionId = $(az account show --query id -otsv)
}
else {
az account set -s $subscriptionId
if( -not $? ) {
Write-Host "Error setting subscrption $subscriptionId" -ForegroundColor Red
exit 1
}
}
if ([String]::IsNullOrEmpty($aksName)) {
$numAks = $(az aks list -g $rg -ojson | ConvertFrom-Json).Length
if ($numAks -ne 1) {
Write-Host "Detected $numAks AKS in the RG $rg." -ForegroundColor Red
if ($numAks -gt 1) {
Write-Host "Please use parameter aksName to set the AKS to use. "
}
exit 1
}
$aks = $(az aks list -g $rg -ojson | ConvertFrom-Json)[0]
}
else {
$aks = $(az aks show -n $aksName -g $rg -ojson | ConvertFrom-Json)
if( -not $? ) {
Write-Host "Error getting AKS $aksName in RG $rg" -ForegroundColor Red
exit 1
}
}
Write-Host "Using AKS $($aks.name) in subscription $subscriptionId" -ForegroundColor Yellow
$nodeResourceGroup = $aks.nodeResourceGroup
if ([String]::IsNullOrEmpty($destinationRg)) {
$destinationRg = $nodeResourceGroup
}
$disks = $(az disk list -g $nodeResourceGroup -ojson | ConvertFrom-Json)
Foreach ($disk in $disks) {
if ($disk.managedBy) {
Write-Host "Found disk $($disk.id)" -ForegroundColor Yellow
$createdby = $disk.tags."$tagCreatedBy"
$pvcName = $disk.tags."$tagPvcName"
$pvcNamespace = $disk.tags."$tagPvcNamespace"
if ($createdby -eq $tagCreatedByValue) {
Write-Host "Disk is for $pvcNamespace/$pvcName" -ForegroundColor Green
if ($pvcsToBackup.Contains($pvcName)) {
Write-Host "Found! Creating snapshot"
Build-Snapshot -disk $disk -aks $aks -destRg $destinationRg -pvcName $pvcName -pvcNamespace $pvcNamespace
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment