Last active
September 16, 2022 16:56
-
-
Save crazycodr/ef1b6b65cf03350fafbde9d1b56bd637 to your computer and use it in GitHub Desktop.
Batch restore deleted s3 objects by accident
This file contains 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 |
Hey, where exactly are you restoring the files in the command above.
The command which you have mentioned above seems to delete the deleted version completely.
I would rather recommend to have a look at the following repository.
The command works like a charm
https://github.com/angeloc/s3-pit-restore
Hey, where exactly are you restoring the files in the command above.
The command which you have mentioned above seems to delete the deleted version completely.
The code above deletes the delete marker - effectively restoring the file.
How to execute this?
I don't remember I baked this many years ago. It might not event work
anymore.
Le ven. 16 sept. 2022, 01 h 36, RLanderson5 ***@***.***> a
écrit :
… ***@***.**** commented on this gist.
------------------------------
How to execute this?
—
Reply to this email directly, view it on GitHub
<https://gist.github.com/ef1b6b65cf03350fafbde9d1b56bd637#gistcomment-4303880>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABBR3OOOZAH3FCRQ5G7GHWLV6QBNTANCNFSM4W5YB5VA>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a very basic script to undelete s3 objects. If your application writes and deletes a lot of files live into the bucket, this is definitely NOT the right approach because you'd need to also include who did the operation. Only use this on a manually managed bucket or something with a very low volume of file!