使用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计算排列组合的方法