#!/bin/bash

# Requiere tener configurado el acceso a MySQL en ~/.my.cnf
if [ ! -f ~/.my.cnf ]; then
        echo "ERROR: No existe ~/.my.cnf"
        exit 1
fi

# Para usar junto con rsnapshot los ficheros generados deben
# estar en el directorio actual. Por ello el valor de DESTINO
# por defecto es .
DESTINO=.
if [ $# -eq 1 ]; then
        DESTINO="$1"
fi

if [ $# -gt 1 ]; then
        echo "ERROR: NĂºmero de argumentos incorrecto."
        exit 1
fi

if [ ! -d $DESTINO ]; then
        echo "ERROR: ${DESTINO} no es un directorio"
        exit 1
fi

BBDD=$(mysql --skip-column-names --silent -e 'SHOW DATABASES;')

for BD in $BBDD
do
        #echo "Exportando $BD"
        ionice -c2 -n7 nice -n19 mysqldump --single-transaction ${BD} | gzip -c -9 > ${DESTINO}/${BD}.gz 
done