php 备份mysql (2)
作者:shenmeng 发布时间:December 13, 2009 分类:默认分类
用php备份的数据库文件,可以打包然后发送到邮箱保存。
这里压缩用了一个创建zip文件的类,CreateZipFile (你可以直接复制该文件,保存为CreateZipFile.inc.php)
//压缩备份文件,使用CreateZipFile类压缩成zip文件
include_once("CreateZipFile.inc.php");
$createZipFile=new CreateZipFile;
$fileContents=file_get_contents($backup_file . ".sql");
$createZipFile->addFile($fileContents, $backup_file . ".sql");
//如果压缩文件失败,直接发送未压缩文件
$this_mail_file = $backup_file . ".sql";
if(!($fd=fopen($backup_file . ".zip", "wb"))){
echo "打开压缩文件失败,将直接发送未压缩文件<br />";
}else{
if($out=fwrite($fd,$createZipFile->getZippedfile())){
fclose($fd);
echo "压缩文件成功<br />";
$this_mail_file = $backup_file . ".zip";
}
}
将压缩文件以邮件发送:
//邮件配置,分别是发件人、发件人名称、收件人、邮件主题、邮件正文,好像主题中加入汉字会乱码
$this_mail_from = "someone@gmail.com";
$this_mail_sender ="someone";
$this_mail_to = "another@gmail.com";
$this_mail_subject ="mysql备份" . date("Y-m-d", time());
$this_mail_message ="这是" .date("Y-m-d", time()). "的备份文件";
//发送邮件
if(mail_attachment($this_mail_file , $this_mail_to , $this_mail_from, $this_mail_sender , $this_mail_subject, $this_mail_message)){
echo "发送邮件成功";
}else{
echo "发送邮件失败,备份文件未发送";
}
//邮件发送函数
function mail_attachment($mail_file,$mail_to,$mail_from , $mail_sender,$mail_subject,$mail_message) {
$file_size = filesize($mail_file);
$handle = fopen($mail_file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$name = basename($mail_file);
$header = "From: " . $mail_sender . " <".$mail_from.">\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $mail_message."\r\n\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-Type: application/octet-stream; name=\"".$mail_file ."\"\r\n"; // use different content types here
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$mail_file."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
$header .= "--".$uid."--";
return @mail($mail_to, $mail_subject, "", $header);
}
?>
已有 14 条评论 »
不错,想试一下,就是不会玩,本人对这个就一菜鸟!呵呵
我也是很菜的
链接一下,交流交流,呵呵,已加你!!
加了
不用老贴技术贴嘛,贴点好玩的也行啊。这么贵的vps老晾着不更新好浪费。
支持!!
好,以后是该常更新了。
博主,何不开发个typecho数据备份插件?绝对受欢迎啊!说实话不做成插件,这里的东西我都看不太懂 - =
顶上面那哥们,呵呵
试试
哈哈,一直在找能备份SQL的
我来加个链接,交流交流
你的页面好像在IE8下过于宽了。。。
我没有IE8,什么时候装个试试
Lots of specialists argue that http://lowest-rate-loans.com help people to live the way they want, because they are able to feel free to buy necessary goods. Furthermore, banks present financial loan for all people.
When you are in not good state and have got no cash to go out from that point, you will need to receive the loan. Because that should aid you definitely. I take financial loan every time I need and feel great because of it.