Skip to content

Instantly share code, notes, and snippets.

@joshsmith
Created October 30, 2010 22:16
Show Gist options
  • Save joshsmith/655800 to your computer and use it in GitHub Desktop.
Save joshsmith/655800 to your computer and use it in GitHub Desktop.
A PHP script for emailing and saving MySQL backups with mysqldump.
<?
// Creates a mysqldump and emails the resulting dump file
// Edit the following values
$dbhost = "DBHOST"; // usually localhost
$dbuser = "DBUSER";
$dbpass = "DBPASS";
$dbname = "DBNAME";
$sendTo = "[email protected]";
$sendToName = "SEND_TO_EMAIL";
$from = "[email protected]";
$fromName = "Automated Backup";
$subject = "Daily Mysql Backup";
$body = "Here is the daily backup.";
// Remember to set the location of your backups dir and to set perms to 777
$backupfile = '/path/to/mysqlbackups/' . $dbname . date("Y-m-d") . '.sql';
// Remember to use the proper location of mysqldump
system("/path/to/mysqldump -h $dbhost -u $dbuser -p$dbpass $dbname > $backupfile", $return);
// Mail the file
// Include and instantiate PHPMailer()
require_once('../../../initialize.php');
$mail = new PHPMailer();
$mail->AddAddress($sendTo, $sendToName);
$mail->From = $from;
$mail->Subject = $subject;
$mail->FromName = $fromName;
$mail->Body = $body;
$mail->AltBody = $body;
$mail->AddAttachment($backupfile);
$mail->WordWrap = 50; // Some old mail programs cut off email if word wrap is greater
$mail->IsSMTP(true);
$mail->SMTPAuth = true;
$mail->Host = "ssl://smtp.sendgrid.net:465"; // Use Sendgrid SSL here
$mail->Username = "YOUR_USERNAME";
$mail->Password = "YOUR_PASSWORD";
$mail->Send();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment