-
-
Save katzueno/16c0c16cc6f8a75481a424b3229f40ac to your computer and use it in GitHub Desktop.
Batch restore deleted s3 objects by accident
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Set the bucket name and date limit that you want to scan for... | |
# | |
# For example, if you deleted everything at 9'ish, set the DATE_LIMIT for a | |
# few minutes before to ensure you find only the stuff you deleted by error | |
# | |
BUCKET_NAME="..." | |
DATE_LIMIT="0000-00-00T00:00:00.000Z" | |
aws s3api list-object-versions --bucket "${BUCKET_NAME}" > objects.json | |
cat objects.json | jq --arg DATE_LIMIT "${DATE_LIMIT}" '[ .DeleteMarkers[] | select(.LastModified > $DATE_LIMIT and .IsLatest) | { file: .Key, version: .VersionId } ]' > markers-to-delete.json | |
cat markers-to-delete.json | jq --arg BUCKET_NAME "${BUCKET_NAME}" '.[] | "aws s3api delete-object --bucket " + $BUCKET_NAME + " --key " + .file + " --version-id " + .version' -r > undelete.sh | |
bash undelete.sh |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment