Skip to content

Instantly share code, notes, and snippets.

@aks
Last active February 28, 2018 19:38
Show Gist options
  • Select an option

  • Save aks/ebdf60ac702afab797a18cbcd3d1171e to your computer and use it in GitHub Desktop.

Select an option

Save aks/ebdf60ac702afab797a18cbcd3d1171e to your computer and use it in GitHub Desktop.
Bash script to collect bucket names and associated encryption info, and produce diff reports.
#!/usr/bin/env bash
# collect-bucket-info
PROG="${0##*/}"
DIR="${0%/*}"
usage() {
cat 1>&2 <<EOF
usage: $PROG [options]
This script collects bucket names and encryption configuration for each bucket, and produces
a report of the differences against the previous run. If -m ADDR is given, the report is
sent via email to ADDR.
Options:
-h show this help
-m ADDR send reports to ADDR (can be used multiple times)
-n norun -- show commands but don't do them
-v verbose - talk a lot
EOF
exit
}
talk() { echo 1>&2 "$*" ; }
talkf() { printf 1>&2 "$@" ; }
run() {
if (( norun )) ; then
talk "(norun) $*"
else
if (( verbose )) ; then
talk "--> $*"
fi
eval "$*"
fi
}
aws_profile="${AWS_PROFILE:-default}"
data_dir=${AWS_DATA_DIR:-$HOME/aws-data/$aws_profile}
last_bucket_names=$data_dir/last_bucket_names.txt
bucket_names=$data_dir/bucket_names.txt
bucket_name_diffs=$data_dir/bucket-name-diffs.txt
bucket_data_diffs=$data_dir/bucket-data-diffs.txt
get_bucket_names() {
if [[ -s "$bucket_names" ]]; then
if [[ -s "$last_bucket_names" ]]; then
create_bucket_name_report
fi
run "mv -f $bucket_names $last_bucket_names"
fi
run "aws s3 ls | awk '{print \$3}' >$bucket_names"
}
setup_bucket_dirs() {
[[ -d $data_dir ]] || run "mkdir -p $data_dir"
last_info_dir=$data_dir/bucket-info-last
info_dir=$data_dir/bucket-info
if [[ -d "$info_dir" ]]; then
[[ -d "$last_info_dir" ]] || mkdir -p $last_info_dir
if (( `ls -1 $info_dir/*.txt | wc -l` > 0 )) ; then
run "mv -f $info_dir/*.txt $last_info_dir/"
fi
else
run "mkdir -p \"$info_dir\""
fi
}
get_bucket_enc_data() {
run "aws s3api get-bucket-encryption --bucket '$1' 2>/dev/null"
}
get_all_bucket_enc_data() {
local bucket_count=$(( `wc -l <$bucket_names` ))
local count=0
for bucket in `cat $bucket_names` ; do
(( count++ ))
bucket_data="$info_dir/enc-config-$bucket.txt"
if [[ -z "$verbose" && -t 1 ]]; then
talkf "\r$count of $bucket_count"
fi
run "echo \"$bucket\" >$bucket_data"
run "get_bucket_enc_data \"$bucket\" >>$bucket_data"
done
(( verbose )) || talk "\ndone"
}
create_bucket_name_report() {
run "diff -u $last_bucket_names $bucket_names >$bucket_name_diffs"
}
create_bucket_data_report() {
if [[ -d $last_bucket_data && -d $bucket_data ]]; then
run "diff -ru $last_bucket_data $bucket_data >$bucket_data_diffs"
fi
}
send_reports() {
if (( ${#mailto[*]} > 0 )) ; then
if [[ -s "$bucket_name_diffs" ]]; then
run "mailx -A \"$bucket_name_diffs\" -s \"Bucket Name Diffs on `date`\" ${mailto[@]}"
talk "Sent $bucket_name_diffs report to ${mailto[@]}"
fi
if [[ -s "$bucket_data_diffs" ]]; then
run "mailx -A \"$bucket_data_diffs\" -s \"Bucket Data Diffs on `date`\" ${mailto[@]}"
talk "Sent $bucket_data_diffs report to ${mailto[@]}"
fi
else
if [[ -s "$bucket_name_diffs" ]] ; then
talk "The bucket name differences are in $bucket_name_diffs"
fi
if [[ -s "$bucket_data_diffs" ]] ; then
talk "The bucket data differences are in $bucket_data_diffs"
fi
fi
}
norun= verbose=
mailto=()
while getopts 'hm:nv' opt ; do
case "$opt" in
h) usage ;;
n) norun=1 ;;
m) mailto+=( "$OPTIND" ) ;;
v) verbose=1 ;;
esac
done
shift $(( OPTIND - 1 ))
(( $# > 0 )) && [[ "$1" == 'help' ]] && usage
setup_bucket_dirs
get_bucket_names
get_all_bucket_enc_data
create_bucket_data_report
send_reports
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment