-
Notifications
You must be signed in to change notification settings - Fork 0
/
show-gcs-bucket-modification-times.sh
executable file
·94 lines (72 loc) · 2.4 KB
/
show-gcs-bucket-modification-times.sh
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
#!/usr/bin/env bash
DESCRIPTION="Shows you the modification times for all buckets in your google project (based on file modification times or as a fallback bucket metadata)."
set -e
LC_TIME=en_US.UTF-8
USAGE="show_bucket_access [OPTIONS]
Options:
-f, --fetch Fetch bucket details before printing update times. If not specified, the details
need to be present in the given path (see --dir)
-p, --project Fetch buckets from given project
-d, --dir Specifies the working directory (where the bucket info files will be downloaded
to/are expected to be). Default is '.'"
. "$(dirname "$BASH_SOURCE")/lib/parse_args.sh"
KEYWORDS=("-f;bool" "--fetch;bool" "-p" "--project" "-d" "--dir")
parse_args __USAGE "$USAGE" __DESCRIPTION "$DESCRIPTION" "$@"
set_trap 1 2
gc_args=()
path="${KW_ARGS['-d']-.}"
path="${KW_ARGS['--dir']-$path}"
path="${path%%/}"
project="${KW_ARGS['--project']-${KW_ARGS['-p']}}"
echo "project: $project"
if [[ -n "$project" ]]
then
gc_args+=("-p" "$project")
fi
fetch="${KW_ARGS['-f']-false}"
fetch="${KW_ARGS['--fetch']-$fetch}"
if [[ "$fetch" == "true" ]]
then
rm "$path"/*.info || true
echo "Fetching bucket details..."
for bucket in $(gsutil ls "${gc_args[@]}")
do
echo "Fetching bucket '$bucket'..."
echo " Fetching Details..."
bucketname=${bucket//gs:\/\//}
bucketname=${bucketname//\//}
gsutil ls "${gc_args[@]}" -Lb $bucket
gsutil ls "${gc_args[@]}" -Lb $bucket >> "$path/$bucketname.info"
echo " Fetching file update timestamps..."
file_dates=$( gsutil ls "${gc_args[@]}" -l "${bucket}**" \
| grep -v "TOTAL: " \
| tr -s " " \
| cut -d " " -f 3 \
| xargs -L1 -I $ date --date="$" +'%Y-%m-%d' \
| sort -r)
update_timestamp=$(cat "$path/$bucketname.info" \
| grep "Time updated:" \
| cut -d " " -f 3- \
| xargs -I $ date --date="$" +'%Y-%m-%d' )
echo "---TIMESTAMPS---" >> "$path/$bucketname.info"
echo "---TIMESTAMPS---"
echo "$update_timestamp" >> "$path/$bucketname.info"
echo "$update_timestamp"
echo "$file_dates" >> "$path/$bucketname.info"
echo "$file_dates"
done
fi
echo ""
echo "EVALUATION"
echo "=========="
(
for bucket in $path/*.info
do
timestamp=$(cat "$path/$bucket" \
| grep -e "---TIMESTAMPS---" -C0 -A2 \
| grep -v -e "^$" -e "---TIMESTAMPS---" \
| sort -r \
| head -n 1 )
printf "%-50s %s\n" "$(basename $bucket .info):" "$timestamp"
done
) | sort -k 2,2