Created
April 6, 2020 16:15
-
-
Save yufufi/bd31c89e1a2cdfe20bd770600d22fce9 to your computer and use it in GitHub Desktop.
Scripts to manage AKS Disk snapshots
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# set the expiry to a month ago | |
expirydate=$(date -d'-1 month' +%s) | |
# I can use az -o table with cut -d driectly | |
for snapshot in $(az snapshot list -g $resourceGroup | jq -r '.[] | [.id, .timeCreated[:10], .name] | join("#")') | |
do | |
snapshotId=$(echo $snapshot | cut -f1 -d "#") | |
snapshotDate=$(echo $snapshot | cut -f2 -d "#" | xargs -I{} date -d {} +%s) | |
snapshotName=$(echo $snapshot | cut -f3 -d "#") | |
if [ $snapshotDate -le $expirydate ]; | |
then | |
echo "Deleting snapshot ${snapshotName}" | |
az snapshot delete --ids $snapshotId | |
else | |
echo "Keeping snapshot ${snapshotName}" | |
fi | |
done |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# get all the persistent volume from the current k8s context | |
echo "Getting the list of all persistent volumes.." | |
for persistentVolume in $(kubectl get pv --all-namespaces -o=jsonpath='{range .items[*]}{.spec.azureDisk.diskURI}{"#"}{.metadata.name}{"\n"}') | |
do | |
# azure id of the disk | |
diskId=$(echo $persistentVolume | cut -f1 -d "#") | |
# name of the persistent volume | |
pvName=$(echo $persistentVolume | cut -f2 -d "#") | |
# target resource group for the snapshot | |
targetResourceGroup=$(az disk show --ids $diskId | jq -r '.resourceGroup') | |
# figure out the backup name | |
backupDate=$(date -dmonday +%Y%m%d) | |
# new backups every monday and then incremental | |
backupName="${pvName}_backup_${backupDate}" | |
# take the snapshot | |
echo "Taking a snapshot of $diskId on $targetResourceGroup as $backupName" | |
az snapshot create -g $targetResourceGroup --source $diskId --name $backupName --incremental true | jq | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment