#!/bin/bash -f

# Determine current date
export CURDATE=`date +%Y%m%d`

#Provide the path for your Wordpress directory
TargetDir=/path/to/your/wordpress/files
Server=yourserveraddress
MysqlUname=your_mysql_username
MysqlPass=your_mysql_pass
MysqlDbase=your_mysql_database
SndEmail=your@domain.com


# Backup WP database to compressed, dated file in tmp directory
echo backing up mysql database to file /tmp/${CURDATE}_db-backup.sql.gz
mysqldump --opt -q -e -h${Server} -u${MysqlUname} -p${MysqlPass} ${MysqlDbase} | gzip > /tmp/${CURDATE}_db-backup.sql.gz


# Backup WP files to compressed, dated file in tmp directory
cd $TargetDir
echo backing up all files to /tmp/${CURDATE}_files.tar.gz
tar cf - . | gzip > /tmp/${CURDATE}_files.tar.gz

# Send emails with the files (GMail anyone?)
cat /tmp/${CURDATE}_db-backup.sql.gz | uuencode ${CURDATE}_db-backup.sql.gz | mail -s ${CURDATE}.BackupDatabase ${SndEmail}
echo Done sending email with mysql backup ...
cat /tmp/${CURDATE}_files.tar.gz | uuencode ${CURDATE}_files.tar.gz | mail -s ${CURDATE}.BackupFiles ${SndEmail}
echo Done sending email with file backup ...


# delete the backup files from the server after you are done with the email
echo Deleting mysql backup files
rm --force /tmp/${CURDATE}_db-backup.sql.gz
echo Deleting file backup
rm --force /tmp/${CURDATE}_files.tar.gz
