使用PHP备份MySQL和网站发送到邮箱实例代码

前端之家收集整理的这篇文章主要介绍了使用PHP备份MySQL和网站发送到邮箱实例代码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

代码如下:
#!/usr/local/bin/PHP.cli
PHP
require_once './lib/swift_required.PHP';

//MysqL
$MysqL_dbname = "db";
$MysqL_user = "user";
$MysqL_pass = "pass";
$MysqL_file = "./xxx.sql";
$MysqL_charset = "utf8";
system("MysqLdump --default-character-set=$MysqL_charset --opt -u$MysqL_user -p$MysqL_pass $MysqL_dbname > $MysqL_file");

//Backup WWW File
$www_path = "../www/";
@$final_file = "./xxx_com_".date("Y_m_d").".zip";
system("zip -r -q $final_file $www_path $MysqL_file");

//E-Mail
@$mail_title = "Backup for Coder4.com ".date("Y_m_d");
$mail_sender = "xxx_send@vip.qq.com";
$mail_recver = "xxx_recv@vip.qq.com";
$mail_body = "See attachments";
$mail_file = $final_file;
$sendmail_cmd = "/usr/sbin/sendmail -bs";

//Create E-Mail
$message = Swift_Message::newInstance();
$message->setSubject($mail_title);
$message->setFrom(array($mail_sender));
$message->setTo(array($mail_recver));
$message->setBody($mail_body);
$message->attach(Swift_Attachment::fromPath($mail_file));
//echo $message->toString();

//Send E-Mail
$transport = Swift_SendmailTransport::newInstance($sendmail_cmd);
$mailer = Swift_Mailer::newInstance($transport);
$result = $mailer->send($message);

//Delete
unlink($final_file);
unlink($MysqL_file);

//End
echo "All backup success."
?>

猜你在找的PHP相关文章