Skip to content

Instantly share code, notes, and snippets.

@JCotton1123
Created April 30, 2019 01:54
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 JCotton1123/8ab4f925b37217379da9c3d5aaca5d94 to your computer and use it in GitHub Desktop.
Save JCotton1123/8ab4f925b37217379da9c3d5aaca5d94 to your computer and use it in GitHub Desktop.
Restore files from latest version in s3
#!/bin/bash
if [ -z "$1" ]; then
echo "An input file is required"
echo "usage: $0 <input-file>"
exit 1
fi
files=$(cat $1)
for f in $files; do
bucket=$(echo $f | cut -d/ -f3)
prefix=$(echo $f | sed "s/^s3:\/\/$bucket\///")
versions=$(aws s3api list-object-versions --bucket $bucket --prefix $prefix)
version_id=$(echo "$versions" | jq '.Versions[0].VersionId' | tr -d '"')
mkdir -p $(dirname $prefix)
aws s3api get-object --bucket $bucket --key $prefix --version-id $version_id $prefix
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment