刚写的数据库备份&&恢复类

上一篇 / 下一篇  2008-04-14 13:56:47 / 个人分类:小琪的php之路

查看( 692 ) / 评论( 9 )

很简单的一个数据库资料备份恢复

使用方法:

[php]

简单备份(直接输出)

$dbbak=new Dbbak($model);
echo $dbbak->Backup('ill');//一个表

//echo $dbbak->Backup(array('table1','table2'));//几个表

备份到文件

$dbbak=new Dbbak($model);

$dbbak->Backuptofile($table,'文件名');

恢复数据(由sql语句)

$dbbak=new Dbbak($model);

$dbbak->Restore($table,'',$content);

从文件恢复数据

$dbbak=new Dbbak($model);

$file="data.sql";

$dbbak->Restore($table,$file,'');

[/php]

类具体代码

[php]

<?php
class Dbbak
{
 var $obj;
 //传进来的model,model可以是fleaphp里面的,也可以是你自己的数据库操作类的实例
 //如果用的是你自己的数据库类,findBySql方法为执行sql语句,并返回结果集
 //execute则是直接执行sql语句,无返回值
 function Dbbak($model)
 {
  $this->obj=$model;
 }
 function Backup($table)
 {
  if(is_array ($table))
  {
   $str="";
   foreach($table as $tab)
   $str.=$this->get_table_content($tab);
   return $str;
  }else{
   return $this->get_table_content($table);
  }
 }
 function Backuptofile($table,$file)
 {
  header("Content-disposition: filename=$file.sql");//所保存的文件名
  header("Content-type: application/octetstream");
  header("Pragma: no-cache");
  header("Expires: 0");
  if(is_array ($table))
  {
   $str="";
   foreach($table as $tab)
   $str.=$this->get_table_content($tab);
   echo $str;
  }else{
   echo $this->get_table_content($table);
  }
 }
 function Restore($table,$file="",$content="")
 {
  //排除file,content都为空或者都不为空的情况
  if(($file==""&&$content=="")||($file!=""&&$content!=""))
  echo"参数错误";
  $this->truncate($table);
  if($file!="")
  {
   if($this->RestoreFromFile($file))
   return true;
   else
   return false;
  }
  if($content!="")
  {
   if($this->RestoreFromContent($content))
   return true;
   else
   return false;

  }
 }
 //清空表,以便恢复数据
 function truncate($table)
 {
  if(is_array ($table))
  {
   $str="";
   foreach($table as $tab)
   $this->obj->execute("TRUNCATE TABLE $tab");
  }else{
   $this->obj->execute("TRUNCATE TABLE $table");
  }

 }
 function get_table_content($table)
 {
  $results=$this->obj->findBySql("select * from $table");
  $temp = "";
  $crlf="\r\n";
  foreach($results as $result)
  { $schema_insert = "INSERT INTO $table VALUES (";
  foreach($result as $key=>$val)
  {
   if($val != "")
   $schema_insert .= " '".addslashes($val)."',";
   else
   $schema_insert .= "NULL,";
  }
  $schema_insert = ereg_replace(",$", "", $schema_insert);
  $schema_insert .= ");$crlf";
  $temp = $temp.$schema_insert ;
  }
  return $temp;
 }

 function RestoreFromFile($file){
  if (false !== ($fp = fopen($file, 'r'))) {
   $sql_queries = trim(fread($fp, filesize($file)));
   $this->splitMySqlFile($pieces, $sql_queries);
   foreach ($pieces as $query) {
    if(!$this->obj->execute($query))
    return false;
   }
   return true;
  }
  return false;
 }
 function RestoreFromContent($content)
 {
  $content = trim($content);
  $this->splitMySqlFile($pieces, $sql_queries);
  foreach ($pieces as $query) {
   if(!$this->obj->execute($query))
   return false;
  }
  return true;
 }
 //来自phpmyadmin的函数
 function splitMySqlFile(&$ret, $sql)
 {
  $sql               = trim($sql);
  $sql_len           = strlen($sql);
  $char              = '';
  $string_start      = '';
  $in_string         = false;

  for ($i = 0; $i < $sql_len; ++$i) {
   $char = $sql[$i];

   // We are in a string, check for not escaped end of
   // strings except for backquotes that can't be escaped
   if ($in_string) {
    for (;;) {
     $i         = strpos($sql, $string_start, $i);
     // No end of string found -> add the current
     // substring to the returned array
     if (!$i) {
      $ret[] = $sql;
      return true;
     }
     // Backquotes or no backslashes before
     // quotes: it's indeed the end of the
     // string -> exit the loop
     else if ($string_start == '`' || $sql[$i-1] != '\\') {
      $string_start      = '';
      $in_string         = false;
      break;
     }
     // one or more Backslashes before the presumed
     // end of string...
     else {
      // first checks for escaped backslashes
      $j                     = 2;
      $escaped_backslash     = false;
      while ($i-$j > 0 && $sql[$i-$j] == '\\') {
       $escaped_backslash = !$escaped_backslash;
       $j++;
      }
      // ... if escaped backslashes: it's really the
      // end of the string -> exit the loop
      if ($escaped_backslash) {
       $string_start  = '';
       $in_string     = false;
       break;
      }
      // ... else loop
      else {
       $i++;

      }
     } // end if...elseif...else
    } // end for
   } // end if (in string)
   // We are not in a string, first check for delimiter...
   else if ($char == ';') {
    // if delimiter found, add the parsed part to the returned array
    $ret[]    = substr($sql, 0, $i);
    $sql      = ltrim(substr($sql, min($i + 1, $sql_len)));
    $sql_len  = strlen($sql);
    if ($sql_len) {
     $i      = -1;
    } else {
     // The submited statement(s) end(s) here
     return true;
    }
   } // end else if (is delimiter)
   // ... then check for start of a string,...
   else if (($char == '"') || ($char == '\'') || ($char == '`')) {
    $in_string    = true;
    $string_start = $char;
   } // end else if (is start of string)

   // for start of a comment (and remove this comment if found)...
   else if ($char == '#' || ($char == ' ' && $i > 1 && $sql[$i-2] . $sql[$i-1] == '--')) {
    // starting position of the comment depends on the comment type
    $start_of_comment = (($sql[$i] == '#') ? $i : $i-2);
    // if no "\n" exits in the remaining string, checks for "\r"
    // (Mac eol style)
    $end_of_comment   = (strpos(' ' . $sql, "\012", $i+2))
    ? strpos(' ' . $sql, "\012", $i+2)
    : strpos(' ' . $sql, "\015", $i+2);
    if (!$end_of_comment) {
     // no eol found after '#', add the parsed part to the returned
     // array and exit
     // RMV fix for comments at end of file
     $last = trim(substr($sql, 0, $i-1));
     if (!empty($last)) {
      $ret[] = $last;
     }
     return true;
    } else {
     $sql     = substr($sql, 0, $start_of_comment) . ltrim(substr($sql, $end_of_comment));
     $sql_len = strlen($sql);
     $i--;
    } // end if...else
   } // end else if (is comment)
  } // end for

  // add any rest to the returned array
  if (!empty($sql) && trim($sql) != '') {
   $ret[] = $sql;
  }
  return true;
 }
}
?>
[/php]


TAG: 数据库 备份 恢复 小琪的php之路

fei发布于2008-04-14 14:06:02
好好啊 手册下
chinafu-quain发布于2008-04-14 14:41:43
不错!!
peter.chen ct_174880859 发布于2008-04-15 12:16:56
首先看看..
在大学 diego 发布于2008-09-08 11:04:40
正在找,拿来试试了
谢了......
wyw5257发布于2008-09-08 11:17:44
现在已经习惯用系统命令来备份
不使用程序作了
在大学 diego 发布于2008-09-08 12:10:29
就是恢复好像还是有问题呀
被清空,没有任何数据了.......
Marshmallow的传说中的blog 17too 发布于2008-09-08 12:56:52
下午回公司看看~
玛雅不归 CrossMaya 发布于2008-09-08 13:11:35
正需要 参考一下!
我来说两句

(可选)

Open Toolbar