Database Logging Daemon for samba on SME Server
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.7 KiB

#!/bin/bash
DB_HOST=$(/sbin/e-smith/db configuration getprop samba-db-logd DbHost || echo localhost)
RETENTION=$(/sbin/e-smith/db configuration getprop samba-db-logd Retention || echo 365)
SQL_DB=$(/sbin/e-smith/db configuration getprop samba-db-logd DbName || echo 'samba_log')
TABNAME="audit"
SQLCMD="mysql ${SQL_DB} --batch";
MONTH=$(date +%m)
YEAR=$(date +%Y)
# We rotate on the first day of a new month
if [ "$MONTH" == "1" ]; then
MONTH=12
else
MONTH=$(($MONTH-1))
fi
# Pad with 0
MONTH=$(printf "%02d" $MONTH)
DATE=$MONTH"_"$YEAR
for T in ${TABNAME}; do
# create table 0
echo "CREATE TABLE IF NOT EXISTS ${T}_0 LIKE ${T};" | $SQLCMD;
# Rotate table
echo "FLUSH TABLES ${T}; RENAME TABLE ${T} TO ${T}_$DATE; RENAME TABLE ${T}_0 TO ${T}" | ${SQLCMD} >/dev/null 2>&1
# Drop _0 table if we rotate more than two times a month
if echo "DESCRIBE ${T}_0;" | ${SQLCMD} >/dev/null 2>&1; then
echo "DROP TABLE ${T}_0;" | $SQLCMD
fi
#compress 2
cd /var/lib/mysql/${SQL_DB}/
echo "FLUSH TABLE ${T}_${DATE};" | $SQLCMD
myisampack -s "${T}_${DATE}.MYI"
myisamchk -s -rq --sort-index --analyze "${T}_${DATE}.MYI"
echo "FLUSH TABLE ${T}_${DATE}" | $SQLCMD
done
# Now check existing table to drop olds ones
for T in $(echo "show tables" | $SQLCMD | grep -v -P "^Tables_in_"$SQL_DB | grep -v -P "^audit$"); do
TMONTH=$(echo $T | perl -pe 'm/^audit_(\d+)_(\d+)/; print $1;exit')
TYEAR=$(echo $T | perl -pe 'm/^audit_(\d+)_(\d+)/; print $2;exit')
# Drop table if older than configured retention
if [ "$(($(date -d "01/$MONTH/$YEAR" +%s)-$(date -d "01/$TMONTH/$TYEAR" +%s)))" -gt "$((24*3600*$RETENTION))" ]; then
echo "DROP TABLE $T;" | $SQLCMD
fi
done