backup.sh


#!/bin/bash
##########################################################################
#
# mysql_backups.sh: A shell script to back up all MySQL databases in
# one shot, nightly, and keep a rolling 3 weeks of
# backups hot, online in the backup archive.
#
# Written by David A. Desrosiers
# Contact desrod@gnu-designs.com
#
# Improved by Alessandro Feijo
# Contact feijo@mindcore.com.br
#
# Last updated Mon Feb 12 2007
#
# Copyright 1998-2012. This may be modified and distributed on the same
# terms as the GPL itself. This copyright header
# must remain intact if you use this script.
#
##########################################################################

#####################################
### MySQL Configuration Variables ###
#####################################

=/usr/local/bin:/usr/bin:/bin

# MySQL hostname
DBHOST='localhost'

# MySQL username
DBUSER='mysql'

# MySQL password
DBPASSWD='senha'

# directory for dump files
LOCALDIR=/media/backups/mysql

#####################################
### Edit Below If Necessary #########
#####################################
cd $LOCALDIR
DATE=`eval date +%Y%m%d-%H%M`

# Optimize the table structure nightly
# mysqlcheck -u$DBUSER -p$DBPASSWD -h$DBHOST -oA

DBS=`mysql -u$DBUSER -p$DBPASSWD -h$DBHOST -e"show databases like 'xpto_%'"`;;

for DATABASE in $DBS
do
if [ $DATABASE != "Database" ]; then
echo "Dumping $DATABASE now..."
mysqlcheck -u$DBUSER -p$DBPASSWD -h$DBHOST $DATABASE
mysqldump -u$DBUSER -p$DBPASSWD -h$DBHOST --add-drop-table -e $DATABASE > ${DATE}_${DATABASE}.sql
fi
done

gzip -9 *.sql
chmod 644 *gz

# Delete files older than XX days
find $LOCALDIR -mtime +15 --delete
0