A-A+

数据库备份类

2008年11月29日 未分类 暂无评论 阅读 1 次

这个类是今天无意中搜索到的。具体的一些过程没有明白。现在对于mysql的操作还有很多不懂得地方。


现在为了使用mysql只有恶补了。。。

 <?php
/*数据库备份:NOTICE:此类要添加数据库连接才能正常工作2006年8月13日,此类继承了一个数据库操作的类
 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);
   $this->Connection= $Connection;
 }
 //取得数据库中的表
 function get_table_name( $database){
   $this->Connection;
   $result=mysql_list_tables( $database);
   $i=0;
  while( $i<mysql_num_rows( $result)){
    $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].";nn";
   $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";
 $idstring = '# Identify: '.base64_encode(" $time, $version")."n";
 $head_info=" $idstring".
        "#n".
     "# Antsnet_Web! The Basic Class Of BackUp DataBasen".
     "# Version: Antsnet Web!  $versionn".
     "# Time:  $timen".
     "# Type: Class Of BackUp DataBasen".
     "# Antsnet_Web_Studio! Home: http://www.antsnet.netn".
     "# Please visit our website for newest infomation about Antsnet_Web_Studio!n".
     "# --------------------------------------------------------nnn";
 $table_name= $this->get_table_name( $database_name);
 $array_table=explode(",", $table_name);
for( $i=0; $i<count( $array_table); $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;
 $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="<font color="red">This File Could Not Open Or Read.</font>";
   }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="<font color="red">This File Could Not Open Or Read.</font>";
     }else{
      $status=fread( $fp,filesize( $file_path));
     fclose( $fp);
     }
     return  $status;
}

//分离SQL语句
function splitsql( $sql){
  $sql = str_replace("r", "n",  $sql);
  $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");
// $status= $bak->get_string("advertisment");
 $str= $bak->get_string("advertisment","bak.sql");
echo  $str;
*/
>

 

这个类是今天无意中搜索到的。具体的一些过程没有明白。现在对于mysql的操作还有很多不懂得地方。


标签:

给我留言

Copyright © 浩然东方 保留所有权利.   Theme  Ality 07032740

用户登录