Skip to content

Instantly share code, notes, and snippets.

@jargnar
Created July 28, 2018 08:57
Show Gist options
  • Save jargnar/d18dfec7244b21bb82961a5dd15cd53f to your computer and use it in GitHub Desktop.
Save jargnar/d18dfec7244b21bb82961a5dd15cd53f to your computer and use it in GitHub Desktop.
Restore deleted S3 Objects by Prefix
bucket="SOME_BUCKET_NAME"
prefix="SOME_PREFIX"
objects="["
printf "\n\n>>> SALVAGING LOST DATA NOW\n\n"
while :
do
count=0
aws s3api list-object-versions --bucket $bucket --prefix $prefix --output text |
grep "DELETEMARKERS" | head -n 500 | (while read obj
do
count=$((count+1))
KEY=$( echo $obj| awk '{print $3}')
VERSION_ID=$( echo $obj | awk '{print $5}')
printf "$count $KEY $VERSION_ID \n"
one_object="{Key=$KEY,VersionId=$VERSION_ID}"
if [[ $objects == "[" ]]; then
objects="[$one_object"
else
objects="$objects, $one_object"
fi
payload="Objects=$objects], Quiet=false"
done
aws s3api delete-objects --bucket $bucket --delete "$payload"
printf "\n\n\n>>> DELETED DELETE MARKERS FOR ABOVE BATCH\n\n\n"
printf ">>> STARTING NEXT BATCH\n\n\n"
)
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment