Skip to content

Instantly share code, notes, and snippets.

@odinokov
Created June 10, 2023 03:31
Show Gist options
  • Save odinokov/cd998449192ffa9be23e806682ad067b to your computer and use it in GitHub Desktop.
Save odinokov/cd998449192ffa9be23e806682ad067b to your computer and use it in GitHub Desktop.
#!/bin/bash
#############################################################################
# Script: restore_from_glacier.sh
# Description:
# This script restores files and folders recursively from
# Amazon S3 Glacier for a given S3 path. It checks if the restore
# is already in progress and initiates the restore for objects
# that are not currently being restored. The default values for
# DAYS and TIER are 3 and "Bulk" respectively, unless provided
# through command line arguments.
#############################################################################
# Function to display usage and exit with an error
usage() {
cat << EOF
Usage: $0 -l <S3_PATH> -p <PROFILE> [-d <DAYS>] [-t <TIER>]
Options:
-l <S3_PATH> S3 objects location
-p <PROFILE> Profile name
-d <DAYS> Days before expiry (default: 3)
-t <TIER> Tier type (default: Bulk)
EOF
}
# Set default values
DAYS=3
TIER="Bulk"
# Check the number of arguments
if [ $# -eq 0 ]; then
usage
fi
# Initialize variables
S3PATH=""
PROFILE=""
BUCKET=""
# Read arguments from the command line
while getopts "l:p:d:t:" opt; do
case $opt in
l)
S3PATH="$OPTARG"
echo -e "S3 objects location:\t$OPTARG"
;;
p)
PROFILE="$OPTARG"
echo -e "Profile name:\t\t$OPTARG"
;;
d)
DAYS="$OPTARG"
echo -e "Days before expiry:\t$OPTARG"
;;
t)
TIER="$OPTARG"
echo -e "Tier type:\t\t$OPTARG"
;;
*)
usage
;;
esac
done
# Check if required arguments are provided
if [ -z "$S3PATH" ] || [ -z "$PROFILE" ]; then
usage
exit 1
fi
# Extract the bucket name from S3PATH
BUCKET=$(awk -F/ '{print $3}' <<< "$S3PATH")
# Retrieve object names into an array
readarray -t OBJECTS <<< "$(aws s3 ls "$S3PATH" --profile "$PROFILE" --recursive | awk -F ' +' '{if (substr($4, length($4), 1) != "/") print $4}')"
# Iterate over objects and initiate restore
for OBJECT in "${OBJECTS[@]}"; do
# Check if object restore is already in progress
if aws s3api head-object --bucket "$BUCKET" --key "$OBJECT" --profile "$PROFILE" | grep -q "Restore"; then
printf "Skipping restore for %s as it is already in progress.\n" "$OBJECT"
else
printf "Restoring %s\n" "$OBJECT"
aws s3api restore-object \
--bucket "$BUCKET" \
--key "$OBJECT" \
--restore-request "{\"Days\": $DAYS, \"GlacierJobParameters\": {\"Tier\": \"$TIER\"}}" \
--profile "$PROFILE"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment