Dreamhost How to Use Cronjobs to Email Backup MySQL Databases

I’ve been using Dreamhost for a while (works great btw), I had been backing things up manually but I thought it was about time to start automated mysql backups. Looking around online I found these instructions but they simply say to modify the instructions and add this mutt command

mutt you@domain.com -a /home/username/backups/archives/mysql_backup.$suffix.tar -s "MySQL Backup"

to get e-mail backups working. In reality, this command will merely open up mutt and create a new message but it will not automatically send it without additional input. Buried online I found someone else who said to simply:

mutt you@domain.com -s "MySQL Backup" < /home/username/backups/archives/mysql_backup.$suffix.tar

which does automatically e-mail you, but it just dumps the file into the e-mail body without attaching it as a document. What that statement is really doing is piping in body text and completing the e-mail prompts magically, which isn’t what I wanted to do. The solution is to:

mutt you@domain.com -a /home/username/backups/archives/mysql_backup.$suffix.tar -s "MySQL Backup" < emailbody.txt

What this gives you is an attachment of your file, and then it pipes in an e-mail body (put it in the same location that this script is running from) and sends the message. You would also use echo statements to “type” whatever additional input you wanted in mutt and script the rest of the message completion [untested but it conceptually should work, YMMV]

Full Script:

#!/bin/bash
cd /home/username/backups/
mkdir mysql
suffix=$(date +%y%m%d)
mysqldump --opt -uUser -ppass -h mysqlA.domain.com db_nameA > mysql/db_nameA.$suffix.sql
mysqldump --opt -uUser -ppass -h mysqlB.domain.com db_nameB > mysql/db_nameB.$suffix.sql
tar -cf archives/mysql_backup.$suffix.tar mysql/*
mutt you@domain.com -a /home/username/backups/archives/mysql_backup.$suffix.tar -s "MySQL Backup" < emailbody.txt
rm -r mysql/