Loading...

Housekeeping of log files

:heavy_exclamation_mark: This post is older than a year. Consider some information might not be accurate anymore. :heavy_exclamation_mark:

Writing software also results in writing application logs. Therefore log rotating or house keeping is essential to free the space of old and unused log files. While Linux provides logrotate, you may run into the situations that you aren’t root or an user with root permissions and are not eligible to use logrotate. A simple shell script will also provide the essential cleanup.

Cleanup Script

Several important things to mention before using it:

  • You use this on your own risk :-)
  • This is a demo script or the real commands were commented. Uncomment if you want to use them.
  • Script was written for Bash v4.x
  • Algorithm effiency: Delete before compress, no point to zip something that may be deleted.
#!/usr/bin/env bash
# ===
LOG_DIR=$1
ARCHIVE_DAYS=$2
DELETE_DAYS=$3
# ===
do_help() {
    echo "Usage: $0 {log directory} {zip after days} {delete after days}"
    exit $1
}
do_check() {
    if [[ "$ARCHIVE_DAYS" = "" ]] ; then
      echo "number of days for archiving log files is missing"
      do_help 1
    fi
    if [[ "$DELETE_DAYS" = "" ]] ; then
      echo "number of days for deleting log files is missing"
      do_help 1
    fi
    if [[ $DELETE_DAYS -lt $ARCHIVE_DAYS ]] ; then
        echo "DELETE DAYS must be greater than ARCHIVE DAYS"
        exit 1
    fi
}
start() {
    echo -e "Delete files older than $DELETE_DAYS days: \n"
    find "$LOG_DIR" -type f -mtime +"$DELETE_DAYS" -print
    #find "$LOG_DIR" -type f -mtime +"$DELETE_DAYS" -print -delete
    echo
    echo -e "Archive files older than $ARCHIVE_DAYS days: \n"
    find "$LOG_DIR" -type f -mtime +"$ARCHIVE_DAYS" -print
    #find "$LOG_DIR" -type f ! -name \*.bz2 -mtime +"$ARCHIVE_DAYS" -print -exec bzip2 -q -f -9 {} \;
    echo
    df -h $LOG_DIR
    exit $?
}
if [[ -n "$LOG_DIR" ]] ; then
    do_check
    start
else
    do_help 1
fi

Demonstration

Display help if mandatory option (log dir) is missing

www-data@alpha:~# ./log-cleanup.sh
Usage: ./log-cleanup.sh {log directory} {zip after days} {delete after days}

Exit if no archive after x days exists

www-data@alpha:~# ./log-cleanup.sh /var/log/apache2/
number of days for archiving log files is missing
Usage: ./log-cleanup.sh {log directory} {zip after days} {delete after days}

Exit if no delete after x days exists

www-data@alpha:~# ./log-cleanup.sh /var/log/apache2/ 2
number of days for deleting log files is missing
Usage: ./log-cleanup.sh {log directory} {zip after days} {delete after days}

Perform cleanup, first delete, than compress

www-data@alpha:~# ./log-cleanup.sh /var/log/apache2/ 2 4
Delete files older than 4 days:
/var/log/apache2/error.log.14.gz
/var/log/apache2/access.log.12.gz
/var/log/apache2/error.log.13.gz
/var/log/apache2/error.log.10.gz
..
Archive files older than 2 days:
/var/log/apache2/error.log.7.gz
/var/log/apache2/error.log.5.gz
/var/log/apache2/error.log.9.gz
/var/log/apache2/error.log.8.gz
..
Filesystem         Size  Used Avail Use% Mounted on
/dev/ploop12345p6   50G  7.2G   40G  16% /var/log

Check if someone applies reverse order

www-data@alpha:~# ./log-cleanup.sh /var/log/apache2/ 14 7
DELETE DAYS must be greater than ARCHIVE DAYS
Please remember the terms for blog comments.