Created
February 20, 2025 10:52
-
-
Save vvdaal/948a602544e312316b6d3e2a93b93f60 to your computer and use it in GitHub Desktop.
A script that loops over ARNS for recovery points and deletes them, with retry mechanism. This script assumes usage of aws-vault, adjust command accordingly if needed.
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
#!/bin/bash | |
# delete_with_retries.sh | |
# Author: Vince van Daaal | |
# | |
# Purpose: A script that loops over ARNS for recovery points and deletes them, with retry mechanism. | |
# This script assumes usage of aws-vault, adjust command accordingly if needed | |
# Actually generating the file should be done by yourself (https://docs.aws.amazon.com/cli/latest/reference/backup/list-recovery-points-by-backup-vault.html) | |
# Variables | |
FILE="file_separated_with_new_lines_containing_recovery_point_arns.txt" | |
VAULT_NAME="your-vault-name" | |
REGION="your-region" | |
PROFILE="your-aws-vault-profile" | |
# Function to delete a recovery point with retries | |
delete_with_retries() { | |
local arn="$1" | |
local max_retries=3 | |
local retry_count=0 | |
local delay=2 | |
echo "Attempting to delete recovery point: $arn" | |
until aws-vault exec "$PROFILE" -- aws backup delete-recovery-point \ | |
--backup-vault-name "$VAULT_NAME" \ | |
--recovery-point-arn "$arn" \ | |
--region "$REGION"; do | |
retry_count=$((retry_count+1)) | |
if [ $retry_count -ge $max_retries ]; then | |
echo "Error: Failed to delete $arn after $max_retries attempts." | |
return 1 | |
fi | |
echo "Retry $retry_count for $arn in $delay seconds..." | |
sleep $delay | |
delay=$((delay * 2)) | |
done | |
echo "Successfully deleted $arn" | |
} | |
# Export function and variables so they are visible to subshells invoked by bash -c | |
export -f delete_with_retries | |
export VAULT_NAME REGION PROFILE | |
# Use xargs to process each ARN from the file | |
# Each ARN is passed to a new bash shell that calls our function. | |
xargs -I {} bash -c 'delete_with_retries "{}"' < "$FILE" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment