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);
}
?>

下载打包的备份脚本

标签: none

已有 14 条评论 »

  1. 诗意小毛 诗意小毛 December 15th, 2009 at 04:11 pm

    不错,想试一下,就是不会玩,本人对这个就一菜鸟!呵呵

    1. admin admin December 16th, 2009 at 06:32 pm

      我也是很菜的

  2. 诗意小毛 诗意小毛 December 16th, 2009 at 07:18 pm

    链接一下,交流交流,呵呵,已加你!!

    1. admin admin December 16th, 2009 at 09:47 pm

      加了

  3. Martin Martin December 17th, 2009 at 10:50 am

    不用老贴技术贴嘛,贴点好玩的也行啊。这么贵的vps老晾着不更新好浪费。

    1. 诗意小毛 诗意小毛 December 17th, 2009 at 11:15 am

      支持!!

    2. admin admin December 18th, 2009 at 06:28 pm

      好,以后是该常更新了。

  4. 羽中 羽中 December 18th, 2009 at 08:33 pm

    博主,何不开发个typecho数据备份插件?绝对受欢迎啊!说实话不做成插件,这里的东西我都看不太懂 - =

  5. 诗意小毛 诗意小毛 December 18th, 2009 at 09:18 pm

    顶上面那哥们,呵呵

  6. 11 11 January 1st, 2010 at 10:01 pm

    试试

    哈哈,一直在找能备份SQL的

  7. 11 11 January 1st, 2010 at 10:04 pm

    我来加个链接,交流交流

    你的页面好像在IE8下过于宽了。。。

    1. shenmeng shenmeng January 1st, 2010 at 10:41 pm

      我没有IE8,什么时候装个试试

  8. LynchRosie28 LynchRosie28 July 3rd, 2010 at 04:54 pm

    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.

  9. loans loans July 21st, 2010 at 08:11 am

    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.

添加新评论 »

captcha
请输入验证码