forked from cloud-gov/cg-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths3-history
executable file
·105 lines (83 loc) · 2.6 KB
/
s3-history
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env bash
set -euo pipefail
shopt -s inherit_errexit
main() {
parse_args "$@"
[[ -v obj_url ]] || usage "S3 object url (-b) required"
authed_with_aws || usage "Not authenticated with AWS. Did you forget aws-vault?"
[[ -v start ]] && echo "Start: $(date -d "@$start")"
[[ -v end ]] && echo "End: $(date -d "@$end")"
me=$(basename "$0")
bucket=$(echo "$obj_url" | cut -f 3 -d /)
object=$(echo "$obj_url" | cut -f 4- -d /)
# shellcheck disable=SC2207
version_ids=($(aws s3api list-object-versions --bucket "$bucket" --prefix "$object" --no-paginate --output=json | jq -r "$(jq_query)"))
(
cd "$(mktemp -d "/tmp/$me.XXX")"
for newer_index in "${!version_ids[@]}"; do
older_index=$((newer_index+1))
if [[ -v "version_ids[older_index]" ]]; then
newer_id="${version_ids[newer_index]}"
older_id="${version_ids[older_index]}"
newer_date=$(aws s3api get-object --output=text --query='LastModified' --bucket "$bucket" --key "$object" --version-id "$newer_id" newer)
_=$(aws s3api get-object --output=text --query='LastModified' --bucket "$bucket" --key "$object" --version-id "$older_id" older)
echo "Date: $newer_date"
diff -y --color=always --suppress-common-lines older newer || true
echo
fi
done
)
}
parse_args() {
while getopts ":hs:e:o:" opt; do
case ${opt} in
s )
start=$(date -d "$OPTARG 00:00:00" +'%s')
;;
e )
end=$(date -d "$OPTARG 23:59:59" +'%s')
;;
o )
obj_url=$OPTARG
;;
h )
usage
;;
\? )
usage "Invalid option: $OPTARG" 1>&2
;;
: )
usage "Invalid option: $OPTARG requires an argument" 1>&2
;;
esac
done
shift $((OPTIND -1))
}
jq_query() {
local j
j=".Versions[]"
[[ -v start ]] && j="$j | select(.LastModified | sub(\"\\\\.[0-9]+Z\"; \"Z\") | fromdate > $start)"
[[ -v end ]] && j="$j | select(.LastModified | sub(\"\\\\.[0-9]+Z\"; \"Z\") | fromdate < $end)"
j="$j | .VersionId"
echo "$j"
}
authed_with_aws() {
aws sts get-caller-identity --output text > /dev/null 2>&1
}
usage() {
[[ $# -gt 0 ]] && echo "ERROR: $*"
local me=$(basename "$0")
cat <<-EOF
USAGE: $me -o OBJECT_URL [-s START_DATE] [-e END_DATE]
Queries S3 version history for an object, showing the date and diff for each
version.
OPTIONS:
-o Object URL (ie: s3://bucket/path/to/object)
-s Start date (ie: 2020-01-21)
-e End date (ie: 2020-01-21)
Examples:
$me -b s3://bucket/path/to/file -s 2000-01-20
EOF
exit 1
}
main "$@"