欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

PHP备份MySQL和网站发送到邮箱

程序员文章站 2022-05-14 21:38:23
...

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入 使用PHP备份MySQL和网站发送到邮箱的方法。 代码: 1 #!/usr/local/bin/php.cli 2 ?php 3 require_once './lib/swift_required.php'; 4 //MySQL 5 $mysql_dbname = "db"; 6 $mysql_user = "user"; 7

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入

  使用PHP备份MySQL和网站发送到邮箱的方法。

  代码:

  1 #!/usr/local/bin/php.cli

  2

  3 require_once './lib/swift_required.php';

  4 //MySQL

  5 $mysql_dbname = "db";

  6 $mysql_user = "user";

  7 $mysql_pass = "pass";

  8 $mysql_file = "./xxx.sql";

  9 $mysql_charset = "utf8";

  10 system("mysqldump --default-character-set=$mysql_charset --opt -u$mysql_user -p$mysql_pass $mysql_dbname > $mysql_file");

  11 //Backup WWW File

  12 $www_path = "/www/";

  13 @$final_file = "./xxx_com_".date("Y_m_d")。".zip";

  14 system("zip -r -q $final_file $www_path $mysql_file");

  15 //E-Mail

  16 @$mail_title = "Backup for Coder4.com ".date("Y_m_d");

  17 $mail_sender = "xxx_send@vip.qq.com";

  18 $mail_recver = "xxx_recv@vip.qq.com";

  19 $mail_body = "See attachments";

  20 $mail_file = $final_file;

  21 $sendmail_cmd = "/usr/sbin/sendmail -bs";

  22 //Create E-Mail

  23 $message = Swift_Message::newInstance();

  24 $message->setSubject($mail_title);

  25 $message->setFrom(array($mail_sender));

  26 $message->setTo(array($mail_recver));

  27 $message->setBody($mail_body);

  28 $message->attach(Swift_Attachment::fromPath($mail_file));

  29 //echo $message->toString();

  30 //Send E-Mail

  31 $transport = Swift_SendmailTransport::newInstance($sendmail_cmd);

  32 $mailer = Swift_Mailer::newInstance($transport);

  33 $result = $mailer->send($message);

  34 //Delete

  35 unlink($final_file);

  36 unlink($mysql_file);

  37 //End

  38 echo "All backup success."

  39 ?>

PHP备份MySQL和网站发送到邮箱