#!/bin/bash if [ "$1" == 'debug' ]; then set -x; shift; fi #------------------------ REALPATH=`realpath $0` WHERE=`dirname $REALPATH` ME=`basename $REALPATH` cd $WHERE . ../../system.conf . ../../common.conf . ../../common.bashlib #------------------------ . WatchWB.conf if [ -z "$1" ] then EXPIRE=$EXPIRATIONDAYS else EXPIRE=$1 fi logger "$ME[$$] Expiration of bandits older than $EXPIRE days" CANDIDATES=`$SQL "select count(IP) from $TABLE where (julianday('now') - julianday(date_event)) > $EXPIRE;"` if [ $CANDIDATES -eq 0 ] then logger "$ME[$$] No candidates for expiration ..." exit else logger "$ME[$$] Candidates for expiration: $CANDIDATES" fi echo "Candidates : $CANDIDATES" echo "Expiration days : $EXPIRE" BANDITS=`$SQL "select IP from $TABLE where (julianday('now') - julianday(date_event)) > $EXPIRE;"` # # First strip the database ... # $SQL "delete from $TABLE where (julianday('now') - julianday(date_event)) > $EXPIRE;" $SQL "delete from affairs_by_ip where (julianday('now') - julianday(date_event)) > $EXPIRE;" # # Reclaim space # $SQL "VACUUM main;" # # Cleanup firewall from expired DROPs # counter=0 for h in `echo $BANDITS` do ((counter++)) $IPSET -exist del $MYSET $h $IPSET -exist del custody $h done if [ ! -z "$PUB_PATH" ] then cp $DB $PUB_PATH fi logger "$ME[$$] Cleaned $counter bandits from DB older than $EXPIRE days"