#!/bin/sh
# You may need to add --master-data to the dump args.
# You may also need to grant Show_view_priv to the debian-sys-maint user.
mysqlargs="--defaults-file=/etc/mysql/debian.cnf"
mysqldumpargs="--routines" # --master-data
dstpath=/var/backups/mysql

mkdir -p "$dstpath"
chmod 700 "$dstpath"
mysql $mysqlargs -eSHOW\ DATABASES|sed -e1d\;/^information_schema/d |
while read db ; do
        mv "$dstpath/$db.sql.1.gz" "$dstpath/$db.sql.2.gz"
        mv "$dstpath/$db.sql.gz" "$dstpath/$db.sql.1.gz"
        mysqldump $mysqlargs $mysqldumpargs $db > "$dstpath/$db.sql"
        gzip "$dstpath/$db.sql"
done
