#!/bin/bash ## Simple logrotate script ## http://superglue.it | Danja Vasiliev, 2014 ## ## Takes filepath(s) as argument: ## logrotate /www/access.log [/www/error.log] [/www/admin/admin.log] ## default settings, redefine in config file mentioned below ROTATE=30 ## backlogs to keep DATE=$(date +%d.%m.%y-%H:%M:%S) LOGS=( $* ) ## source config CONF='/etc/logrotate.conf' [[ -e $CONF ]] && . $CONF function err { printf %s": "%s"\n" "$(basename $0)" "$1"; exit 1 } [[ -z $LOGS ]] && err 'missing input file' [[ ! -e $LOGS ]] && err 'input file not readable' for FILE in ${LOGS[@]}; do ## get number of backlogs and purge unwanted N=( $(ls -1 -r $FILE*.gz &>/dev/null || echo '') ) [[ ${#N[@]} -gt $((ROTATE-1)) ]] && rm -f ${N[@]:$((ROTATE-1)) } ## compress and move gzip -f $FILE || err 'failed to gzip file' mv $FILE.gz "$FILE""$DATE".gz || err 'failed to rename archive' done exit 0