中国设计联盟
联网
平面设计 画册 VI欣赏 包装 CG-插画 搜索 个人网页 Alexa排名 CSS 建站资源 下载专区 JS特效 品牌服装 服装院校 专题欣赏 SEO 图标欣赏 专题
网站建设 域名注册 虚拟主机 广州网站设计 域名注册 广州网站建设 上海网站建设 虚拟主机 广州网页设计  虚拟主机 域名注册 acg王国 ACG玩家
求创科技
网站建设
中国福网
招聘求职
中国互联
艺魂宝库网
中资源
当前位置:网络学院首页 >> 编程开发 >> php >> PHP通用数据库备份类

PHP通用数据库备份类

来源:中国设计秀    作者:    点击:11     加入收藏    发表评论
0
顶一下

<?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].";\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";
 $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: http://www.antsnet.net\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<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;
*/
>

2007-08-01 10:36:00    出处:
Google
热点文章/相关文章
网站地图 | 关于我们 | 联系我们 | 网站建设 | 广告服务 | 版权声明 | 免责声明 | 网站公告 | 友情链接 | 留言 | 旧版入口