Skip to content

Instantly share code, notes, and snippets.

@jirutka
Created September 21, 2021 21:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jirutka/cb97c988bf0ae0aeeddc29e4ab3af047 to your computer and use it in GitHub Desktop.
Save jirutka/cb97c988bf0ae0aeeddc29e4ab3af047 to your computer and use it in GitHub Desktop.
Script for compressing and later removing old FreeRADIUS' radacct log files with "-YYYYMMDD" suffix #freeradius #logrotate
#!/bin/sh
# SPDX-License-Identifier: MIT
# SPDX-FileCopyrightText: 2021 Jakub Jirutka <jakub@jirutka.cz>
#---help---
# Usage: radacct-rotate [options]
#
# Compress and later remove old FreeRADIUS' radacct log files with "-YYYYMMDD"
# suffix.
#
# Options:
# -C FILE Location of radacct-rotate config file (defaults to
# /etc/raddb/radacct-rotate.conf).
#
# -c DAYS Compress files older than DAYS (overrides option
# compress_after_days from the config).
#
# -r DAYS Remove compressed files older than DAYS (overrides
# option remove_after_days from the config).
#
# -d Run in dry-run mode (only print what would be done).
#
# -h Show this message and exit.
#---help---
set -eu
readonly PROGNAME='radacct-rotate'
# Y Y Y Y m m d d
readonly DATE_GLOB='[1-9][0-9][0-9][0-9][0-1][0-9][0-3][0-9]'
readonly HELP_TAG='#---help---'
# Defaults
CONFIG='/etc/raddb/radacct-rotate.conf'
DRY_RUN=false
radacct_dir='/var/log/radius/radacct'
compress_cmd='gzip -9'
compress_ext='.gz'
compress_after_days=2
remove_after_days=180
while getopts ':C:c:dr:h' OPT; do
case "$OPT" in
C) CONFIG=$OPTARG;;
c) COMPRESS_AFTER_DAYS=$OPTARG;;
d) DRY_RUN=true;;
r) REMOVE_AFTER_DAYS=$OPTARG;;
h) sed -n "/^$HELP_TAG/,/^$HELP_TAG/{/^$HELP_TAG/d; s/^# \\?//; p;}" "$0"; exit 0;;
\?) echo "$PROGNAME: invalid option: -$OPTARG (see '$PROGNAME -h')" >&2; exit 100;;
esac
done
readonly CONFIG COMPRESS_AFTER_DAYS DRY_RUN REMOVE_AFTER_DAYS
sh -n "$CONFIG" || exit 100
. "$CONFIG"
compress_after_days=${COMPRESS_AFTER_DAYS:-$compress_after_days}
remove_after_days=${REMOVE_AFTER_DAYS:-$remove_after_days}
find_compressible() {
find "$radacct_dir" -type f -name "*-$DATE_GLOB" -mtime "+$compress_after_days" "$@"
}
find_deletable() {
find "$radacct_dir" -type f -name "*-${DATE_GLOB}${compress_ext}" -mtime "+$remove_after_days" "$@"
}
check_number() {
case "$1" in
''|*[!0-9]*) echo "$PROGNAME: option '$2' must be a number, but given: '$1'" >&2; exit 100;;
esac
}
check_number "$compress_after_days" 'compress_after_days'
check_number "$remove_after_days" 'remove_after_days'
rc=0
if $DRY_RUN; then
find_compressible -exec echo $compress_cmd {} \; || rc=111
find_deletable -exec echo rm {} \; || rc=111
else
for path in $(find_compressible -print); do
$compress_cmd "$path" >&2 && touch -ct "${path##*-}0000" "$path".* || rc=111
done
find_deletable -exec rm '{}' \; >&2 || rc=111
fi
exit $rc
# Configuration file for radacct-rotate
#
# Script radacct-rotate compresses and removes old log files with suffix
# "-YYYYMMDD" (e.g. detail-20210921) found in /var/log/radius/radacct/*/.
#
# The age of the files is determined by their time of last modification
# (see stat(1)). When the file is compressed, its modification time is set
# to the date encoded in the file name.
# Set to "yes" to disable cron script /etc/periodic/daily/radacct-rotate.
#cron_disabled=no
# Compress files older than specified number of days.
#compress_after_days=3
# Remove files older than specified number of days.
#remove_after_days=180
# Location of the radacct directory.
#radacct_dir="/var/log/radius/radacct"
# Specifies which command (and arguments) to use to compress files.
# It must produce compressed file with ${compress_ext} extension and remove
# the original file.
#compress_cmd="gzip -9"
#compress_ext=".gz"
#!/bin/sh
: ${RADACCT_ROTATE_CONFIG:="/etc/raddb/radacct-rotate.conf"}
/bin/sh -n "$RADACCT_ROTATE_CONFIG" 2>/dev/null || exit 0
. "$RADACCT_ROTATE_CONFIG"
[ "$cron_disabled" = yes ] && exit 0
exec /usr/bin/radacct-rotate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment