Posted By

fackz on 04/08/09


Tagged

email database backup


Versions (?)

Who likes this?

23 people have marked this snippet as a favorite

fackz
rickfu
webpro
vali29
NyX
umang_nine
silentbob84
nb109
BrianCoyDesign
colingardom
g33kish
shabbar
metthyn
Almorca
Kurt
wirenaught
voove
sbcjr
telemichus
jsamackay
ringo380
Priestd09
tux-world


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


 / Published in: PHP
 

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

  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. ?>

Report this snippet  

You need to login to post a comment.