#!/bin/sh
##########################################################
# db2mail.sh, MySQL to e-mail backup script
# written by M. Neset KABAKLI <contact{at}neset{dot}com>
# 
# Version: 1.1
# Date	 : 2004-11-03
# Update : 2004-12-01
# 
# Warning: Be careful when using this script, if you try to
# backup a large database, backup files may fill up your inbox.
#
##########################################################
# BEGIN CONFIGURATION

# backup e-mail address
email="email@domain.com"

# db options
dbhost="localhost"		# your database server
dbuser="yourdbusername" 	# your database user name
dbpass="yourdbpassword" 	# your database password
dbname="yourdatabase"		# separate with spaces to backup more than one database
backupoptions="--no-create-db"	# database backup options, no need to edit this line

# END CONFIGURATION
##########################################################

# mail vars
rhost=`/bin/hostname`
backupdate=`/bin/date +"%F"`
subject="[backup] Database backup of $backupdate - $rhost ($dbname)"

# temporary files
backupfile="db2mail.$backupdate.sql"
archivefile="db2mail.$backupdate.tgz"

# backup, compress, send
mysqldump -h $dbhost -u $dbuser --password=$dbpass $backupoptions --databases $dbname > $backupfile
tar -czvf $archivefile $backupfile
(echo "This e-mail message generated by db2mail."; uuencode $archivefile $archivefile) | mail -s "$subject" $email

# delete temporary files
rm -f $backupfile
rm -f $archivefile