How to e-mail to yourself an automatic backup of your MySQL database

Seye Kuyinu
The Seye Kuyinu Journal
1 min readApr 9, 2011

--

// Put in your variable values here.
$dbhost = "yourhost"; // usually localhost
$dbuser = "yourusername";
$dbpass = "yourpassword";
$dbname = "yourdb";
$sendto = "You ";
$sendfrom = "Automated Backup ";
$sendsubject = "Daily Mysql Backup";
$bodyofemail = "Here is the daily backup.";
// don't need to edit below this section
$backupfile = $dbname . date("Y-m-d") . '.sql';
system("mysqldump -h $dbhost -u $dbuser -p$dbpass $dbname > $backupfile");

// Mail the file
//the includes here are for your PEAR extensions. Confirm PEAR is installed in your PHP.

include('Mail.php');
include('Mail/mime.php');

$message = new Mail_mime();
$text = "$bodyofemail";
$message->setTXTBody($text);
$message->AddAttachment($backupfile);
$body = $message->get();
$extraheaders = array("From"=>"$sendfrom", "Subject"=>"$sendsubject");
$headers = $message->headers($extraheaders);
$mail = Mail::factory("mail");
$mail->send("$sendto", $headers, $body);

// Delete the file from your server
unlink($backupfile);

--

--

Seye Kuyinu
The Seye Kuyinu Journal

While I am whoever I am, I play different roles. Sometimes an Agile Coach, sometimes as a Hypnotherapist. Sometimes I muse about the glory of who we really are