#!/usr/bin/bash
# [INFRA-439]
# Attempt to parse the current audit log path from mysql
ATPATH=$(mysql -NB -e "SHOW VARIABLES LIKE 'audit_log_file'" | awk '{ print $2 }')

# Safety dance
if [[ -z $ATPATH ]]; then
    # Use a hardcoded path
    ATPATH=/var/log/mysql/
else
    # Strip the file name included in the mysql output
    ATPATH="${ATPATH%/*}"
fi

# Compress rotated audit logs
find "$ATPATH" -type f -iname 'audit.log.*.xml' -mtime +1 -print0 | xargs --null pigz --silent

# Delete older audit logs - these will be in cloudwatch anyway
find "$ATPATH" -type f -iname 'audit.log.*.xml.gz' -mtime +30 -delete
