[不指定 2012/10/31 02:54 | by 孤城浪子 ]
| |
来源 http://blog.csdn.net/sha_cq/article/details/8033767

  1. <?php  
  2.  /* 
  3.  这是一个在服务器端备份和恢复数据库的类,张伟 2012-09-21 qq:3340971 
  4.  */  
  5.  class DBBack{  
  6.     private $back_dir='./';//备份的路径  
  7.     private $back_file=array();//当前已存在的备份  
  8.     private $host='localhost';  
  9.     private $user='root';  
  10.     private $pwd='123456';  
  11.     private $conn;  
  12.     private $dbname='navgoods';  
  13.     private $charset='utf8';  
  14.     private $sql='';  
  15.     private $limit=";\r\n[*_*]";  
  16.     public $tips='';//执行得到的提示信息  
  17.     private $cg_line=0;//操作成功的记录总条数  
  18.     private $cg_tb=0;//操作成功的表的个数  
  19.     private $sb_line=0;//操作失败的记录总条数  
  20.     private $sb_tb=0;//操作失败的表的个数  
  21.     private static $instance;  
  22.     public static function getInstance(){  
  23.         if(!self::$instance instanceof self){  
  24.             self::$instance=new self();  
  25.         }  
  26.         return self::$instance;  
  27.     }  
  28.     private function __construct(){  
  29.         date_default_timezone_set('Asia/Shanghai');  
  30.         $this->conn=@mysql_connect($this->host,$this->user,$this->pwd);  
  31.         if(!$this->conn){  
  32.             $this->tips='数据库连接失败!'.mysql_error();  
  33.             return false;  
  34.         }  
  35.         mysql_query("SET NAMES '".$this->charset."'");  
  36.         mysql_query("SET CHARACTER_SET_CLIENT='".$this->charset."'");  
  37.         mysql_query("SET CHARACTER_SET_RESULTS='".$this->charset."'");  
  38.         if(!mysql_select_db($this->dbname,$this->conn)){  
  39.             $this->tips='不存在数据库:'.$this->dbname.',请核对后再试!'.mysql_error();  
  40.             return false;  
  41.         }  
  42.     }  
  43.     //以库为单位备份  
  44.     public function dbBack(){  
  45.         mysql_query("set names '{$this->charset}'");  
  46.         $this->sql='';  
  47.         $this->sql= "CREATE DATABASE IF NOT EXISTS `$this->dbname` DEFAULT CHARACTER SET {$this->charset}{$this->limit}";  
  48.         $this->sql.= "USE `$this->dbname`{$this->limit}";  
  49.         $tb_result=mysql_query("show tables");  
  50.         while($t=mysql_fetch_array($tb_result)){  
  51.             $tb_name[]=$t[0];  
  52.         }  
  53.         $this->sql.=$this->tbBack($tb_name);  
  54.         return $this->saveFile();  
  55.     }  
  56.    
  57.     //以表为单位备份,传入一个数组  
  58.     public function tbBack($arr_tb_name){  
  59.         $mysql=array();  
  60.         $i=0;  
  61.         $this->cg_line=0;  
  62.         foreach($arr_tb_name as $tb_name){  
  63.             $mysql[$i]= "set charset {$this->charset}{$this->limit}";  
  64.             $mysql[$i].= "DROP TABLE IF EXISTS `$tb_name`{$this->limit}";  
  65.             $tb_create=mysql_query("show create table `$tb_name`");  
  66.             $row=mysql_fetch_assoc($tb_create);  
  67.             $mysql[$i].=$row['Create Table'].$this->limit;  
  68.             $mysql[$i].= "LOCK TABLES `$tb_name` WRITE{$this->limit}";  
  69.             $result=mysql_query("select * from `$tb_name`");  
  70.             while($data=mysql_fetch_assoc($result)){  
  71.                 foreach ($data as $k=>$v){  
  72.                     if($v==''){  
  73.                         unset($data[$k]);  
  74.                     }  
  75.                 }  
  76.                 $keys=array_keys($data);  
  77.                 $keys=array_map(addslashes,$keys);  
  78.                 $keys=join('`,`',$keys);  
  79.                 $keys="`".$keys."`";  
  80.                 $vals=array_values($data);  
  81.                 $vals=array_map(addslashes,$vals);  
  82.                 $vals=join("','",$vals);  
  83.                 $vals="'".$vals."'";  
  84.                 $mysql[$i].="insert into `$tb_name`($keys) values($vals){$this->limit}";  
  85.                 $this->cg_line++;  
  86.             }  
  87.             $mysql[$i].= "UNLOCK TABLES{$this->limit}";  
  88.             $i++;  
  89.         }  
  90.         $this->cg_tb=$i;  
  91.         $mysql=implode('',$mysql);  
  92.         return $mysql;  
  93.     }  
  94.    
  95.    
  96.     //恢复数据库  
  97.     function restore($fname){  
  98.         if (file_exists($fname)) {  
  99.             $cg=0;  
  100.             $sb=0;  
  101.             $this->cg_line=0;  
  102.             $this->sb_line=0;  
  103.             $this->cg_tb=0;  
  104.             $this->sb_tb=0;  
  105.             $handle = @fopen($fname"r");  
  106.             $buffer='';  
  107.             $this->tips='';  
  108.             if ($handle){  
  109.                 while (!feof($handle)){  
  110.                     $buffer .= fgets($handle, 4096);  
  111.                 }  
  112.                 fclose($handle);  
  113.             }else{  
  114.                 $this->tips.="备份文件读取失败!";  
  115.             }  
  116.             $a=explode($this->limit, $buffer);  
  117.             unset($buffer);  
  118.             $total=count($a)-1;  
  119.             for ($i=0;$i<$total;$i++){  
  120.                 if(mysql_query($a[$i])){  
  121.                     if(strpos($a[$i],'insert into')!==false||strpos($a[$i],'INSERT INTO')!==false)$this->cg_line++;  
  122.                     if(strpos($a[$i],'create table')!==false||strpos($a[$i],'CREATE TABLE')!==false)$this->cg_tb++;  
  123.                     $cg+=1;  
  124.                 }else{  
  125.                     if(strpos($a[$i],'insert into')!==false||strpos($a[$i],'INSERT INTO')!==false)$this->sb_line++;  
  126.                     if(strpos($a[$i],'create table')!==false||strpos($a[$i],'CREATE TABLE')!==false)$this->sb_tb++;  
  127.                     $sb+=1;  
  128.                     $sb_command[$sb]=$a[$i].'---'.mysql_error();  
  129.                 }  
  130.             }  
  131.             $this->tips.=" 操作完毕,<br/>共处理 $total 条命令,<br/>成功 $cg 条,<br/>失 败 $sb 条.<br/>成功恢复{$this->cg_tb}张表,{$this->cg_line}条记录。<br />恢复失败{$this->sb_tb}张表,{$this->sb_line}条记录.";  
  132.             if ($sb>0){  
  133.                 $this->tips.="<hr><br><br>失败命令如下:<br>";  
  134.                 for ($ii=1;$ii<=$sb;$ii++){  
  135.                     $this->tips.="<p><b>第 ".$ii." 条命令(内容如下):</b><br>".$sb_command[$ii]."</p><br>";  
  136.                 }  
  137.             }  
  138.         }else{  
  139.             $this->tips.="MySQL备份文件不存在,请检查文件路径是否正确!";  
  140.         }  
  141.     }  
  142.    
  143.     private function saveFile(){  
  144.         $this->tips='';  
  145.         $this->back_dir=rtrim($this->back_dir,'/').'/';  
  146.         $filename=$this->back_dir.$this->dbname.date('YmdHis').".sql";  
  147.         $fsize=file_put_contents($filename,$this->sql,FILE_USE_INCLUDE_PATH);  
  148.         $this->sql='';  
  149.         if($result===false){  
  150.             $this->tips='文件备份失败!';  
  151.             return false;  
  152.         }else{  
  153.             $this->tips='文件保存成功^_^<br/>文件名:'.$filename.'<br/>大小:'.round($fsize/1024/1024,2).'兆.<br/>';  
  154.             $this->tips.="共备份了{$this->cg_tb}张表,{$this->cg_line}条记录。";  
  155.             return true;  
  156.         }  
  157.     }  
  158.    
  159.    
  160.     //检测已存在的备份  
  161.     public function check(){  
  162.         $this->back_dir=rtrim($this->back_dir,'/').'/';  
  163.         if(!is_dir($this->back_dir)){  
  164.             if(!mkdir($this->back_dir)){  
  165.                 $this->tips='存放备份文件的文件夹不存在,并且创建失败,请确保您在服务器有创建该目录的权限!';  
  166.                 return false;  
  167.             }  
  168.         }  
  169.         $arrFile=scandir($this->back_dir);  
  170.         foreach($arrFile as $key=>$value){  
  171.             if(is_dir($this->back_dir.$value)){unset($arrFile[$key]);continue;}  
  172.             if(substr($value,-4,4)!='.sql')unset($arrFile[$key]);  
  173.         }  
  174.         if(emptyempty($arrFile)){  
  175.             $this->back_file=array();  
  176.             $this->tips='数据库目前还没有备份!';  
  177.             return false;  
  178.         }else{  
  179.             $i=0;  
  180.             foreach($arrFile as $k=>$v){  
  181.                 $url=$this->back_dir.$v;  
  182.                 $this->back_file[$i]['name']=iconv('GBK','UTF-8',$v);//转码文件名  
  183.                 $this->back_file[$i]['url']=$url;  
  184.                 $this->back_file[$i]['time']=filectime($url);  
  185.                 $this->back_file[$i]['size']=round(filesize($url)/1024/1024,2).'Mb';  
  186.                 $i++;  
  187.             }  
  188.             return $this->back_file;  
  189.         }  
  190.     }  
  191.    
  192.       
  193.  }  
  194.  ?> 
软件 | 评论(0) | 引用(0) | 阅读(3290)
 
发表评论
   
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]