Welcome to WebmasterWorld Guest from 126.96.36.199 , register , free tools , login , search , pro membership , help , library , announcements , recent posts , open posts Become a Pro Member
Emailing database using cron mr_nabo msg:4117638 11:48 am on Apr 18, 2010 (gmt 0) Hi, 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. mn
lammert msg:4117721 5:56 pm on Apr 18, 2010 (gmt 0)
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" email@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.
mr_nabo msg:4117924 6:42 am on Apr 19, 2010 (gmt 0)
This is great, thanks so much for sharing this code!