Skip to content

Instantly share code, notes, and snippets.

@jtara1
Created September 27, 2018 21:12
Show Gist options
  • Select an option

  • Save jtara1/9e4646ec8788e4921d2e26bdbd9b4359 to your computer and use it in GitHub Desktop.

Select an option

Save jtara1/9e4646ec8788e4921d2e26bdbd9b4359 to your computer and use it in GitHub Desktop.
#!/bin/bash
# ======================
# Check Azure CLI is installed
# ======================
if ! [ -x "$(command -v az)" ]; then
echo 'Error: Azure cli is not installed.' >&2
exit 1
fi
OPTIND=1 # Reset in case getopts has been used previously in the shell.
PLATFORM=$(uname)
DAYS_TO_KEEP=""
STORAGE_ACCOUNT=""
CONTAINER=""
PIVOT_TIMESTAMP=""
RESOURCE_GROUP=""
show_help () {
printf "\nUsage example:\n"
printf "\n$ \e[32m`basename $0`\e[0m \e[33m-s\e[0m earnystorage \
\e[33m-c\e[0m be-mobile-logs \e[33m-r\e[0m EY_ResourceGroup \
\e[33m-d\e[0m 30\n"
printf "\nThis means all files in container be-mobile-logs of the\n"
printf "account earnystorage and resource group EY_ResourceGroup \n"
printf "older than 30 days will be deleted.\n"
printf "\n\e[31mAll arguments are required.\e[0m\n\n"
}
while getopts ":hc:d:s:r:p:" opt; do
case $opt in
h)
show_help
exit 0
;;
c) CONTAINER=$OPTARG
;;
d) DAYS_TO_KEEP=$OPTARG
;;
s) STORAGE_ACCOUNT=$OPTARG
;;
r) RESOURCE_GROUP=$OPTARG
;;
p) PATTERN=$OPTARG
;;
:) echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
shift $((OPTIND-1))
[ "${1:-}" = "--" ] && shift
# ======================
# Check if we have all arguments
# ======================
if [ "$DAYS_TO_KEEP" == "" ] \
|| [ "$STORAGE_ACCOUNT" == "" ] \
|| [ "$RESOURCE_GROUP" == "" ] \
|| [ "$CONTAINER" == "" ]; then
show_help
exit 1
fi
if [ "$PATTERN" == "" ]; then
PATTERN=*
fi
# ======================
# Show warning if keep history is too short
# ======================
if [ "$DAYS_TO_KEEP" -lt 30 ]; then
while true; do
read -p "Are you sure you want to keep only $DAYS_TO_KEEP days worth of files?" yn
case $yn in
[Yy]* ) break;;
[Nn]* ) exit;;
* ) echo "Please answer yes or no.";;
esac
done
fi
# ======================
# Get platform
# ======================
if [ $PLATFORM == 'Darwin' ]; then
PIVOT_TIMESTAMP=$(date -v-"$DAYS_TO_KEEP"d +%Y-%m-%dT%H:%mZ)
elif [ $PLATFORM == 'Linux' ]; then
PIVOT_TIMESTAMP=$(date -d "$DAYS_TO_KEEP days ago" '+%Y-%m-%dT%H:%MZ')
else
echo "Platform not supported"
exit 1
fi
# ======================
# Get connections string
# ======================
CONNECTION_STRING=$(az storage account show-connection-string -g $RESOURCE_GROUP -n $STORAGE_ACCOUNT)
echo $STORAGE_ACCOUNT
echo $CONNECTION_STRING
echo $CONTAINER
echo $PIVOT_TIMESTAMP
# ======================
# Perform deletion
# ======================
initTime=$(python3 -c "print(__import__('time').time())")
az storage blob delete-batch \
--pattern $PATTERN \
--account-name $STORAGE_ACCOUNT \
--connection-string="$CONNECTION_STRING" \
--source $CONTAINER \
--if-unmodified-since $PIVOT_TIMESTAMP \
--verbose \
--dryrun
python3 -c "print(__import__('time').time() - $initTime)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment