Skip to content

Instantly share code, notes, and snippets.

@carmanchris31
Last active January 28, 2021 21:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carmanchris31/60c1bf7341c365fb613ce1aa29092e64 to your computer and use it in GitHub Desktop.
Save carmanchris31/60c1bf7341c365fb613ce1aa29092e64 to your computer and use it in GitHub Desktop.
Undelete S3 files in bulk
#!/bin/bash
#
# Undeletes S3 files in bulk for a provided bucket/prefix/date deleted
#
# Author: Christopher Carman
# Based on https://stackoverflow.com/a/64352685/4175944
set -e
Bucket=$1
Prefix=$2
DateDeleted=$3
[ "$#" -ne 3 ] && echo "Error: Incorrect number of parameters" && error=true
[[ ! $DateDeleted =~ ^[1-9][0-9]{3}-[0-9]{2}-[0-9]{2}$ ]] && echo "Error: DateDeleted must be in yyyy-mm-dd format" && error=true
[[ -z "$AWS_ACCESS_KEY_ID" ]] && echo "Error: Missing environment variable: AWS_ACCESS_KEY_ID" && error=true
[[ -z "$AWS_SECRET_ACCESS_KEY" ]] && echo "Error: Missing environment variable: AWS_SECRET_ACCESS_KEY" && error=true
if [ -n "$error" ]; then
SCRIPT=`basename "$0"`
echo ""
echo "Usage: $SCRIPT <Bucket> <Prefix> <DateDeleted:yyyy-mm-dd>"
exit 1
fi
echo "✓ Looking for matches"
echo ""
s3Result=$(aws s3api list-object-versions --bucket $Bucket --prefix $Prefix --query "DeleteMarkers[?IsLatest && starts_with(LastModified,'$DateDeleted')].{Key:Key,VersionId:VersionId}")
matches=$(echo "$s3Result" | jq -c '.[]?')
if [ -z "$matches" ]; then
echo "No matches found"
exit 0
fi
echo "Matched files:"
echo "$matches"
echo ""
read -p "⚠️ Undelete the above files? (yes/no)" -r REPLY
echo ""
if [[ $REPLY = "yes" ]]
then
echo "✓ Undeleting files. This may take a while..."
echo ""
for row in $matches; do
key=$(echo "$row" | jq -r '.Key' )
versionId=$(echo "$row" | jq -r '.VersionId' )
echo $(aws s3api delete-object --bucket $Bucket --key $key --version-id $versionId) & done
wait
echo ""
echo "Done"
else
echo "Cancelled"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment