Welcome to WebmasterWorld Guest from

Forum Moderators: bakedjake

Message Too Old, No Replies

Emailing database using cron



11:48 am on Apr 18, 2010 (gmt 0)

5+ Year Member


Trying to figure out how to email the database I create using Cron - the code below is working in that it's creating the backup in the right place:

mysqldump -u USERNAME -pPASSWORD DATABASENAME | gzip > /home/USER/bkupsfolder/mysql-$(date +\%Y-\%m-\%d-\%H.\%M.\%S).gz

Is there an easy way to mod this so that Cron will send the database to a specified email address? By the way, this is a command line going into Cpanel which sets the frequency.



5:56 pm on Apr 18, 2010 (gmt 0)

WebmasterWorld Senior Member lammert is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

You cannot send binary data as email unless you first encode it to the allowed range of ASCII characters. The old-school method is with uuencode, and the new-school method with MIME. Uuencode is still recognized by most modern email readers and it is the easiest to implement in a command line pipe.

Sending the file directly without storing it in a temporary file:

mysqldump -u USERNAME -pPASSWORD DATABASENAME | gzip | uuencode mysql-$(date +\%Y-\%m-\%d-\%H.\%M.\%S).gz | mail -s "Gzipped backup file" user@example.com

This pipe doesn't create a temporary file. Gzip directly outputs the compressed data to uuencode which encodes it in proper ASCII characters. That output is piped to the mail command line program.

The command line may appear broken on your screen due to screen width limitations. In reality, all commands should be on one line.


6:42 am on Apr 19, 2010 (gmt 0)

5+ Year Member

This is great, thanks so much for sharing this code!

Featured Threads

Hot Threads This Week

Hot Threads This Month