#!/usr/bin/env bash # # Restores all files/folders inside a particular bucket path, # e.g. aws-restore-deep-glacier-folder mcampagnaro-deep-glacier images restored_images # will restore all files inside the images folder of the mcampagnaro-deep-glacier bucket, saving # temp restoration data in the local "restored_images" directory. # if which tput >/dev/null 2>&1; then ncolors=$(tput colors) fi if [ -t 1 ] && [ -n "$ncolors" ] && [ "$ncolors" -ge 8 ]; then RED="$(tput setaf 1)" GREEN="$(tput setaf 2)" YELLOW="$(tput setaf 3)" BLUE="$(tput setaf 4)" MAGENTA="$(tput setaf 5)" CYAN="$(tput setaf 6)" BOLD="$(tput bold)" NORMAL="$(tput sgr0)" else RED="" GREEN="" YELLOW="" BLUE="" MAGENTA="" CYAN="" BOLD="" NORMAL="" fi error() { printf "${BOLD}${RED}$1${NORMAL}\n" } abort() { error "\nAborting...\n" exit 1 } set -e bucket="$1" path="$2" temp_dir="$3" number_of_objects_per_file=100 days_available=7 restore_tier="Bulk" # Can also be "Standard" if [[ $bucket == "" || $path == "" || $temp_dir == "" ]]; then error "Usage: aws-restore-deep-glacier-folder " exit 1 fi printf "${BOLD}Restoring ${GREEN}$bucket:$path${NORMAL}${BOLD} with local temp folder ${GREEN}$temp_dir${NORMAL}\n" mkdir -p "$temp_dir" pushd "$temp_dir" &>/dev/null aws s3api list-objects-v2 --bucket $bucket --prefix $path --query "Contents[?StorageClass=='DEEP_ARCHIVE']" --output text | LC_ALL=C awk '{print substr($0, index($0, $2))}' | awk '{NF-=3};3' > all_objects_list.txt # Generate the main script that will kick off the restoration. printf "while read x; do\n printf \"aws s3api restore-object --restore-request '{\\\\\"Days\\\\\":$days_available,\\\\\"GlacierJobParameters\\\\\":{\\\\\"Tier\\\\\":\\\\\"$restore_tier\\\\\"}}' --bucket $bucket --key \\\\\"\$x\\\\\"\\\\n\"\n aws s3api restore-object --restore-request \"{\\\\\"Days\\\\\":$days_available,\\\\\"GlacierJobParameters\\\\\":{\\\\\"Tier\\\\\":\\\\\"$restore_tier\\\\\"}}\" --bucket $bucket --key \"\$x\"\ndone < all_objects_list.txt\nprintf \"\\\\nDone! You can now delete this folder.\\\\n\"\n" > run.sh chmod +x run.sh printf "${BOLD}You can now run ${GREEN}$temp_dir/run.sh${NORMAL}${BOLD} to start the restoration process.\n" popd &>/dev/null