| #! /bin/sh |
| |
| # Script designed to clean up (zip/delete) old files |
| # Adjust the variables below and then copy/symlink this script |
| # to /etc/cron/cron.{hourly,daily} |
| |
| # We want to keep the filenames dated and that confuses logrotate, |
| # hence this script. |
| |
| # Method used - either NAME, CTIME, or FILES |
| METHOD="NAME" |
| # Maximum age of the logs |
| MAXAGE=120 |
| # Maximum number of logs to keep |
| MAXFILES=30 |
| # Zip all files after the first n files |
| ZIPAFTER=3 |
| # Set to 1 for debug output |
| VERBOSE=0 |
| |
| # Path where the logfiles reside in |
| BASEPATH="/var/lib/osmo-pcap/" |
| |
| |
| # Find the client names present in basepath |
| # Delete files older than MAXAGE days |
| # Zip all but the first ZIPAFTER files |
| cd "$BASEPATH" |
| |
| |
| do_cleanup_ctime() |
| { |
| find . -ctime +$MAXAGE -name "trace-$1-*.pcap*" |sort -r | while read LOG; do |
| [ $VERBOSE -eq 1 ] && echo "Deleting file \"$LOG\"" |
| rm -f "$LOG" |
| done |
| } |
| |
| do_cleanup_name() |
| { |
| CUTOFF=$(date -d "${MAXAGE} days ago" +%Y%m%d) |
| find . -name "trace-$1-*.pcap*" |sort -r | while read LOG; do |
| # Retrieve the date from the filename |
| # We only need day accuracy so ignore the time |
| CURRENT=${LOG%_*} |
| CURRENT=${CURRENT#./trace-$1-} |
| |
| # Date is before the cutoff date, delete |
| if [ $CURRENT -lt $CUTOFF ]; then |
| [ $VERBOSE -eq 1 ] && echo "Deleting file \"$LOG\"" |
| rm -f "$LOG" |
| fi |
| done |
| } |
| |
| do_cleanup_files() |
| { |
| i=1 |
| find . -name "trace-$1-*.pcap*" |sort -r | while read LOG; do |
| if [ $i -gt $MAXFILES ]; then |
| [ $VERBOSE -eq 1 ] && echo "Deleting file \"$LOG\"" |
| rm -f "$LOG" |
| fi |
| i=$(($i+1)) |
| done |
| } |
| |
| do_zip() |
| { |
| i=1 |
| find . -name "trace-$1-*.pcap*" |sort -r | while read LOG; do |
| if [ $i -gt $ZIPAFTER ]; then |
| if [ "${LOG##*.}" != "gz" ]; then |
| [ $VERBOSE -eq 1 ] && echo "Compressing file \"$LOG\"" |
| gzip "$LOG" |
| fi |
| fi |
| i=$(($i+1)) |
| done |
| } |
| |
| # Use an explicit pattern here |
| find . -name "trace-*.pcap*" |sed -n -re "s/.*trace-(.+)-[0-9]{8}_[0-9]{6}\.pcap(\..+)?/\1/p" |sort |uniq | while read CLIENT; do |
| |
| [ $VERBOSE -eq 1 ] && echo "Cleaning logs for $CLIENT" |
| |
| if [ "x$METHOD" == "xNAME" ]; then |
| do_cleanup_name "$CLIENT" |
| elif [ "x$METHOD" == "xCTIME" ]; then |
| do_cleanup_ctime "$CLIENT" |
| elif [ "x$METHOD" == "xFILES" ]; then |
| do_cleanup_files "$CLIENT" |
| else |
| echo "Error, set METHOD to NAME, CTIME or FILES" |
| exit 1 |
| fi |
| |
| do_zip "$CLIENT" |
| done |