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


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?
  2. // Create the mysql backup file
  3. // edit this section
  4. $dbhost = "yourhost"; // usually localhost
  5. $dbuser = "yourusername";
  6. $dbpass = "yourpassword";
  7. $dbname = "yourdb";
  8. $sendto = "Webmaster <[email protected]>";
  9. $sendfrom = "Automated Backup <[email protected]>";
  10. $sendsubject = "Daily Mysql Backup";
  11. $bodyofemail = "Here is the daily backup.";
  12. // don't need to edit below this section
  13.  
  14. $backupfile = $dbname . date("Y-m-d") . '.sql';
  15. system("mysqldump -h $dbhost -u $dbuser -p$dbpass $dbname > $backupfile");
  16.  
  17. // Mail the file
  18.  
  19. include('Mail.php');
  20. include('Mail/mime.php');
  21.  
  22. $message = new Mail_mime();
  23. $text = "$bodyofemail";
  24. $message->setTXTBody($text);
  25. $message->AddAttachment($backupfile);
  26. $body = $message->get();
  27. $extraheaders = array("From"=>"$sendfrom", "Subject"=>"$sendsubject");
  28. $headers = $message->headers($extraheaders);
  29. $mail = Mail::factory("mail");
  30. $mail->send("$sendto", $headers, $body);
  31.  
  32. // Delete the file from your server
  33. unlink($backupfile);
  34. ?>

URL: http://www.theblog.ca/mysql-email-backup

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.