#!/bin/bash 

#Written by Jorge Medrano <jorge@indatcom.net>
# Copyright (c) 2008 Jorge Medrano.
#
# Licensed under The MIT License
# Redistributions of files must retain the above copyright notice.
# license http://www.opensource.org/licenses/mit-license.php The MIT License


if [ $# -lt 3 ]; then
   echo "Necesitas pasar dos parámetros: usuario, password y base de datos."
   exit 0
 fi

echo "Inicia Respaldo"

RUTA=`pwd`
FECHA=`date +"%Y-%m-%d"`
RUTA="$RUTA/$3-$FECHA"

if [ ! -d $RUTA ]; then
  echo "Creando directorio"
  mkdir $RUTA
fi

SQL='Select table_name FROM information_schema.tables WHERE table_schema = "'$3'" AND table_type = "BASE TABLE";'

for i in `mysql -u $1 -p$2 -B --skip-column-names -e "$SQL"`;
do
  mysqldump -u $1 -p$2 --opt $3 $i > $RUTA/$i.sql
done
echo "Termino respaldo"

