Chinaunix首页 | 论坛 | 博客
  • 博客访问: 18671834
  • 博文数量: 7460
  • 博客积分: 10434
  • 博客等级: 上将
  • 技术积分: 78178
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-02 22:54
文章分类

全部博文(7460)

文章存档

2011年(1)

2009年(669)

2008年(6790)

分类:

2008-05-29 08:52:35

以下为引用的内容:
/*数据库备份:NOTICE:此类要添加数据库连接才能正常工作,此类继承了一个数据库操作的类
power by:antsnet.net
 E-mail:antsnet@163.com
*/
Class Back_up_database extends dbstuff{
//类开始
var $HOST;
var $USERNAME;
var $PASSWORD;
var $DATABASE;
function Back_up_database($host,$username,$password,$database){
 //初始化数据库连接
 $this->HOST=$host;
 $this->USERNAME=$username;
 $this->PASSWORD=$password;
 $this->DATABASE=$database;
 $Connection=$this->connect($this->HOST,$this->USERNAME,$this->PASSWORD,$this->DATABASE,$pconnect);

www~phperz~com


 $this->Connection=$Connection;
}
//取得数据库中的表
function get_table_name($database){
 $this->Connection;
 $result=mysql_list_tables($database);
 $i=0;
 while($i $tb_name[$i]=mysql_tablename($result,$i);
 $table_name.= $tb_name[$i].",";
 $i++;
 }
 $this->table_name=substr($table_name,0,-1);
 return $this->table_name;
}

//取得每个表中的FIELDS和属性并生成CREATE TABLE语句
function get_table_fields($table_name){
 $this->Connection;
 $createtable=dbstuff::query("SHOW CREATE TABLE $table_name");
 $create=dbstuff::fetch_row($createtable);
 $tabledump .="DROP TABLE IF EXISTS $table_name;\n";
 $tabledump .= $create[1].";\n\n";
 $this->$table_name=$tabledump;
 return $this->$table_name;
}
//取得表中的数据并生成ISERT INTO 语句

function get_insert($table_insert_name){
$this->Connection;
 $rows = dbstuff::query("SELECT * FROM $table_insert_name");
$numfields = dbstuff::num_fields($rows);
$numrows = dbstuff::num_rows($rows);
while ($row = dbstuff::fetch_row($rows)) {
 $comma = "";
 $tabledump .= "INSERT INTO $table_insert_name VALUES(";
 for($i = 0; $i < $numfields; $i++) {
 $tabledump .= $comma."'".mysql_escape_string($row[$i])."'";
 $comma = ",";
 }
 $tabledump .= ");\n";
}
$this->tabledump=$tabledump;
return $this->tabledump;
}

//获取所有数据并连接成新的字符串并将它写入文件中.sql
function get_string($database_name,$file_path_name){
$time=date("Y-m-d H:j");
$date_time=date("YmdHis");
$file_path_name=$file_path_name.$date_time.".sql";
$version="Antsent_Web_Studio Database BackUp V1.01"; php程序员站
$idstring = '# Identify: '.base64_encode("$time,$version")."\n";
$head_info="$idstring".
 "#\n".
 "# Antsnet_Web! The Basic Class Of BackUp DataBase\n".
 "# Version: Antsnet Web! $version\n".
 "# Time: $time\n".
 "# Type: Class Of BackUp DataBase\n".
 "# Antsnet_Web_Studio! Home: \n".
 "# Please visit our website for newest infomation about Antsnet_Web_Studio!\n".
 "# --------------------------------------------------------\n\n\n";
$table_name=$this->get_table_name($database_name);
$array_table=explode(",",$table_name);
for($i=0;$i $table_string.=$this->get_table_fields($array_table[$i]);
 $table_insert.=$this->get_insert($array_table[$i]);
}
$count_string=$head_info.$table_string.$table_insert;
//return $count_string; php程序员站
$write_status=$this->write_file($file_path_name,$count_string);
return $write_status;
}

//写入一个文件
function write_file($file_path,$file_contents){
 if(@!$fp=fopen($file_path,'w')){
 $status="This File Could Not Open Or Read.";
 }else{
 flock($fp,3);
 fwrite($fp,$file_contents);
 fclose($fp);
 $status="Write TO The File Success.Success To Backup The Database.";
 }
 return $status;
}

//读取文件
function read_file($file_path){
 if(@!$fp= fopen($file_path,'r')){
 $status="This File Could Not Open Or Read.";
 }else{
 $status=fread($fp,filesize($file_path));
 fclose($fp);
 }
 return $status;
}
//分离SQL语句
function splitsql($sql){
$sql = str_replace("\r", "\n", $sql);
phperz.com

$ret = array();
$num = 0;
$queriesarray = explode(";\n", trim($sql));
unset($sql);
foreach($queriesarray as $query) {
 $queries = explode("\n", trim($query));
 foreach($queries as $query) {
 $ret[$num] .= $query[0] == "#" ? NULL : $query;
 }
 $num++;
}
return($ret);
}
//恢复数据
function db_restore($file_restore){
$content=$this->read_file($file_restore);
$this->Connection;
$sqlquery =$this->splitsql($content);
 unset($content);
 foreach($sqlquery as $sql) {
 if(trim($sql) != '') {
 $status=dbstuff::query($sql);
 }

 }
 if(!status){
 $msg="Import False.";
 }else{
 $msg="Import Success.";
 }
 return $msg;
}
//类结束
}
/*具体用法:
$bak=new Back_up_database("dbserver","dbusername","dbpassword","db");
phperz.com

//$status=$bak->get_string("advertisment");
$str=$bak->get_string("advertisment","bak.sql");
echo $str;
*/
>
阅读(546) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~