#!/bin/sh

DESTDIR="<%= mysql_backup_datadir %>"
MAXAGE="<%= mysql_backup_maxage %>"

DATE=`date "+%Y-%m-%d"`
HOME="`getent passwd ${USER} | cut -d : -f 6`"

if [ ! -d ${DESTDIR} ]; then
    echo "ERR: MySQL backup directory [${DESTDIR}] does not exist" 1>&2
    exit 1
fi

umask 077

tmpwatch -m -f ${MAXAGE} ${DESTDIR}

DESTDIR=${DESTDIR}/${DATE}
mkdir -p ${DESTDIR}

for db in `mysql -e 'show databases' -s` ; do
    case ${db} in
        Database|information_schema)
            continue
            ;;
    esac
    mysqldump --add-drop-table ${db} | gzip > ${DESTDIR}/${db}.${DATE}.gz
done
