如何用PHP通过电子邮件自动备份MySQL数据库表

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何用PHP通过电子邮件自动备份MySQL数据库表相关的知识,希望对你有一定的参考价值。

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

以上是关于如何用PHP通过电子邮件自动备份MySQL数据库表的主要内容,如果未能解决你的问题,请参考以下文章

mysql mysqldump如何用

如何用Percona XtraBackup进行MySQL从库的单表备份和恢复

sql server 如何用sql语句实现单个表的备份和还原。

如何用php判断mysql数据库里某张表是不是存在

如何用php在mysql中删除表中一个字段里的某一行呢?

如何用mysql命令备份和恢复