Created
December 4, 2018 17:21
-
-
Save eekfonky/fef01bac4fcff0c42123e9d8df33a47f to your computer and use it in GitHub Desktop.
Apply RDS Deletion Protection
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 | |
# Enable extended globbing for the +(...) pattern | |
shopt -s extglob | |
## Variable Declartions ## | |
OUTPUT="text" # set output format | |
## FUNCTIONS ## | |
# apply Instance DB deletion protection | |
apply_instance_protection () { | |
aws rds modify-db-instance --db-instance-id "$DB" \ | |
--deletion-protection --apply-immediately --region "$REG" --profile "$PRO" 1> /dev/null | |
} | |
# apply Cluster DB deletion protection | |
apply_cluster_potection () { | |
aws rds modify-db-cluster --db-cluster-id "$CLUSTER" \ | |
--deletion-protection --apply-immediately --region "$REG" --profile "$PRO" 1> /dev/null | |
} | |
# apply deletion protection to cluster or instance | |
cluster_or_instance () { | |
# get regions | |
mapfile -t REGIONS < <( aws ec2 describe-regions --query "Regions[].{Name:RegionName}" \ | |
--region eu-west-1 --profile "$PRO" --output $OUTPUT ) | |
for REG in "${REGIONS[@]}" | |
do | |
mapfile -t DB_NAME < <( aws rds describe-db-instances --query "DBInstances[*].[DBInstanceIdentifier]" \ | |
--region "$REG" --profile "$PRO" --output $OUTPUT ) | |
if [ ${#DB_NAME[@]} -eq 0 ] | |
then | |
continue | |
else | |
for DB in "${DB_NAME[@]}" | |
do | |
# determine if instance is in a cluster | |
CLUSTER=$(aws rds describe-db-instances --db-instance-id "$DB" --query "DBInstances[*].DBClusterIdentifier" \ | |
--region "$REG" --profile "$PRO" --output $OUTPUT) | |
if [ -z "$CLUSTER" ] | |
then | |
apply_instance_protection | |
else | |
apply_cluster_potection | |
fi | |
done | |
fi | |
done | |
} | |
# main menu function | |
main_menu () { | |
clear | |
# display profiles list menu | |
mapfile -t PROFILES < <(cat .aws/config | cut -f 2 -d ' ' | sed 's/]//' | sort -f) | |
PS3="Select Profile number: " | |
# make a dummy array which includes the values | |
validOptions=${PROFILES[0]}; | |
for ((i=1; i<${#PROFILES[@]}; i++)); do | |
validOptions="$validOptions|${PROFILES[i]}" | |
done | |
# show main menu | |
select PRO in "${PROFILES[@]}" "exit" | |
do | |
case $PRO in | |
+($validOptions)) | |
cluster_or_instance | |
break | |
;; | |
"exit") | |
echo "Exiting..." | |
exit | |
;; | |
*) | |
echo "Invalid option $PRO, try again..." | |
esac | |
done | |
} | |
## SCRIPT COMMANDS ## | |
main_menu |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment