Created
January 7, 2024 09:54
-
-
Save smuda/e21e4ecaf9c202ab9dedeaf87f308c87 to your computer and use it in GitHub Desktop.
Delete a AWS vault with existing archives
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 | |
#set -eu | |
#set -x | |
set -o pipefail | |
GLACIER_NAME=${GLACIER_NAME:-QNAP2019-08-24} | |
AWS_REGION=${AWS_REGION:-eu-west-1} | |
AWS_ACCOUNT_ID=${AWS_ACCOUNT_ID:--} | |
JOB_ID= | |
JOB_RESULT_FILE=job-output.json | |
NPROC=0 | |
clean_up () { | |
ARG=$? | |
echo "" | |
echo "> clean_up" | |
#rm -f "${JOB_RESULT_FILE}" | |
exit $ARG | |
} | |
trap clean_up EXIT | |
# Find out how many procesors | |
OS=$(uname) | |
if [ "Darwin" == "${OS}" ]; then | |
# MacOSX | |
NPROC=$(sysctl -n hw.physicalcpu) | |
else | |
# Not really sure if this works as expected | |
NPROC=$(nproc) | |
fi | |
echo "Detected number of physical CPUs: ${NPROC}" | |
# Validation | |
echo "Make sure there is a vault with name ${GLACIER_NAME}" | |
VAULT_ARN=$(aws glacier list-vaults --account-id "${AWS_ACCOUNT_ID}" --output json | jq -r ".VaultList[] | select(.VaultName==\"${GLACIER_NAME}\") | .VaultARN" || exit 1) | |
if [ "" == "${VAULT_ARN}" ]; then | |
echo "Coult not find a vault with name ${GLACIER_NAME}" | |
exit 1 | |
fi | |
echo "Vault ARN ${VAULT_ARN}" | |
echo "" | |
# Create an inventory job | |
echo "Create inventory job" | |
JOB_ID=$(aws glacier initiate-job --vault-name "${GLACIER_NAME}" --account-id "${AWS_ACCOUNT_ID}" --job-parameters='{"Type": "inventory-retrieval"}' --output json | jq -r '.jobId' || exit 1) | |
#JOB_ID=BjGsZFrcHnB2K53nIMRBAzyzYX-Jt_SyBuoHTTx6-e2RJESR4W2s4YjQW7Z61PXTs8MJ1udz8siggN0WuAZpt16JQDwO | |
echo "Inventory job ID: ${JOB_ID}" | |
# Wait for job to finish | |
echo "" | |
echo "Wait for job to finish" | |
JOB_STATUS_CODE=InProgress | |
while [ "InProgress" == "${JOB_STATUS_CODE}" ] | |
do | |
JOB_STATUS_CODE=$(aws glacier describe-job --vault-name "${GLACIER_NAME}" --account-id "${AWS_ACCOUNT_ID}" --job-id "${JOB_ID}" --output json | jq -r '.StatusCode' || exit 1) | |
echo "$(date '+%H:%M:%S') Status: ${JOB_STATUS_CODE}" | |
if [ "InProgress" == "${JOB_STATUS_CODE}" ]; then | |
sleep 300 | |
fi | |
done | |
# Get the job output into a file | |
echo "" | |
echo "Fetch job result to ${JOB_RESULT_FILE}" | |
aws glacier get-job-output --vault-name "${GLACIER_NAME}" --account-id "${AWS_ACCOUNT_ID}" --job-id "${JOB_ID}" ${JOB_RESULT_FILE} || exit 1 | |
# Get the archive ids from file | |
ARCHIVE_IDS=$(cat "${JOB_RESULT_FILE}" | jq -r '.ArchiveList[].ArchiveId' || exit 1) | |
#IFS=$'\n' ARCHIVE_IDS=($ARCHIVE_IDS_STR) || exit 1 | |
# echo "${ARCHIVE_IDS}" | |
echo "There are $(echo \"${ARCHIVE_IDS}\" | wc -l) archives in output file" | |
# Delete each archive | |
echo "" | |
while IFS= read -r ARCHIVE_ID; do | |
echo "Delete ARCHIVE_ID: ${ARCHIVE_ID}" | |
aws glacier delete-archive --vault-name "${GLACIER_NAME}" --account-id "${AWS_ACCOUNT_ID}" --archive-id="${ARCHIVE_ID}" & | |
# At most as number of CPU cores | |
while [ $( jobs | wc -l ) -ge $((2 * ${NPROC})) ]; do sleep 1; done | |
done <<< "$ARCHIVE_IDS" | |
echo "Wait for all threads to finish" | |
wait | |
# Delete the glacier vault | |
echo "" | |
echo "Delete the glacier vault ${GLACIER_NAME}" | |
aws glacier delete-vault --vault-name "${GLACIER_NAME}" --account-id "${AWS_ACCOUNT_ID}" || echo "Failed to delete vault ${ARCHIVE_ID}" && exit 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment