AWS Restore all glacier objects in a bucket

 

#!/bin/bash
#####
#ident "@(#) restore_glacier_bucket.sh v 1.0 2017/02/20"
#
#  Author: Raymond Keckler
#
#  This will restore all glacier objects under a bucket for 3 days
#
########################################

## color settings
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color

## Give usage message if the command was wrong
function usage()
{
    echo -e "${RED}usage: restore_glacier_bucket.sh  ${NC}"
}  

function restore_glacier()
{
	b=$1

	if aws s3api head-bucket --bucket ${b} 2>/dev/null; then

		for key_obj in `aws s3api list-objects --bucket "$b" --query "Contents[?StorageClass=='GLACIER'].{Key: Key}" --output text` 
		do
			aws s3api restore-object --bucket "$b" --key "$key_obj" --restore-request '{"Days":3}'
			echo "/$b/$key_obj"
		done
	else
		echo "$b does not exist"
	fi
}


###### Main

if [ "$#" == "0" ]; then
	usage
	exit 1
fi


if [ "$1" != "" ]; then 
	mybucket=$1
fi

if [[ ("$mybucket" == "") ]] ; then
    usage
    exit 1
fi

restore_glacier "$mybucket"