日历
| |||||||||
| 日 | 一 | 二 | 三 | 四 | 五 | 六 | |||
| 1 | 2 | 3 | 4 | 5 | |||||
| 6 | 7 | 8 | 9 | 10 | 11 | 12 | |||
| 13 | 14 | 15 | 16 | 17 | 18 | 19 | |||
| 20 | 21 | 22 | 23 | 24 | 25 | 26 | |||
| 27 | 28 | 29 | 30 | ||||||
搜索标题
-
刚写的数据库备份&&恢复类 *
2008-04-14 13:56:47
很简单的一个数据库资料备份和恢复类
使用方法:
[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]
