zoukankan      html  css  js  c++  java
  • mysql数据库备份.php

    <?php
     
     
    class DB_BACKUP
       {
         private $dbname ; #要进行备份数据库名称
     
         private $isGz=true ; # 是否对数据进行压缩
     
         private $mode = "w"; #写入模式
     
         private $dir  ; #数据存放目录
     
         private $charset="gb2312";  #要跟数据库的存储编码一样
     
         #构造函数
     
         function  __construct($dbname="",  $dir="", $isGz = "",$mode="")
         {
          $this->dbname = $dbname;
     
          $this->isGz = is_bool($isGz) ?  $isGz : $this->isGz;
     
          $this->dir = $dir ? $dir : $this->dir ;
     
          $this->mode = $mode ? $mode : $this->mode ;
     
         }
     
         #兼容php4
         function DB_BACKUP($dbname="" ,$dir="", $isGz = "",$mode="")
         {
     
          $this->__construct($dbname="" ,$dir="", $isGz = "",$mode="");
     
         }
     
     
         function get_table_fields($table)
         {
          global  $DB ;
     
          $content="DROP TABLE IF EXISTS `{$table}`;\n;n";
     
          $DB->db_connect($this->dbname,$this->charset);
     
          if (!$table) exit() ;
     
          $sql = "show create table {$table}";
     
          $row = $DB->db_fetch_array($sql,MYSQL_NUM);
     
           $content .=$row[0]['Create Table'] ;
     
          return $content;
         }
     
     
         #备份整个数据库函数
     
        function backup_all($dir="")
         {
          global $DB ;  #全局变量 调用数据库类
     
          global $FILE ;  #全局变量调用数据库类
     
          $DB->db_connect($this->dbname,$this->charset);
     
          $date = date("Y-m-d");
     
          $this->dir = $dir ? $dir : $this->dir ;
     
          $this->dir .= "/{$this->dbname}_{$date}";  #文件夹以日期命名
     
          $FILE->make_dir($this->dir);
     
          $sql = "show tables;";
     
          $row = $DB->db_fetch_array($sql,"",MYSQL_NUM);
     
          if ($DB->db_num_rows($sql)<=0) exit() ;
     
          foreach ( $row as $rw)
          {
           foreach ($rw as $table)
           {
            $this->backup_table($table,$dir);
     
           }#end second foreach()
     
          }#end first foreach()
     
         }
     
     
         #备份数据表函数
     
         function backup_table($table)
         {
          global $DB ;  #全局变量 调用数据库类
     
         global $FILE ;  #全局变量 调用数据库类
     
         $DB->db_connect($this->dbname,$this->charset);
     
         $field = "{$this->get_table_fields($table)};\n;n";
     
         $content = "";
     
          if (!$table) exit();
     
          $time = time();
     
          $query = "select * from $table";
     
            if ($DB->db_num_rows($query)>0)
           {
     
            $fp = $this->isGz ? gzopen("{$this->dir}/{$table}_{$time}.sql.gz",$this->mode) :fopen("{$this->dir}/{$table}_{$time}.sql",$this->mode);
     
            if ($fp)
            {
             $row2 = $DB->db_fetch_array($query);
     
     
     
             foreach ($row2 as $rw2)
             {
              $content .="INSERT INTO `{$table}` VALUES(";
     
              foreach ($rw2 as $value)
              {
                  $value = mysql_escape_string($value);
     
               $content .="'{$value}'," ;
     
              }
     
              $content = rtrim($content,",");  #去除右边的“,”;
     
              $content.=");\n;n";
     
             }# first foreach()
     
             $content = $field.$content;
     
             $this->isGz ? gzwrite($fp , $content):fwrite($fp ,$content );
     
             $this->isGz ? gzclose($fp ) : fclose($fp);
     
             echo "Backup {$table} Success\n";
     
            }else {
     
             echo "Backup {$table} Failure\n";
     
            }#end gzopen()
     
           }#end db_num_rows()
     
         }
     
         function split_sql($sql)
         {
     
         $sqlArray = explode(";n",trim($sql));
     
         return $sqlArray ;
     
         }
     
     
         #还原数据表数据
     
         function recover_table($filename)
         {
          global $FILE ;
     
          global $DB ;
     
          $filename = "{$this->dir}/{$filename}";
     
          if (!file_exists($filename)) return false ;
     
            $DB->db_connect($this->dbname,$this->charset);
     
     
            $content = file_get_contents($filename);
     
         //  $content= iconv("gb2312","utf8",$content);
     
            $sqlArray = $this->split_sql($content);
     
            unset($content);
     
            for ($i= 0 ; $i < count($sqlArray)-1 ; ++ $i  )
     
             {
     
              $result=$DB->db_query($sqlArray[$i]);
     
              if ($result) echo "Insert success!\n";
     
              else  echo "Failure!\n";
     
             }
     
             unset($sqlArray,$result);
     
             return true ;
     
         }
     
     
         #还原整文件夹的数据
     
         function recover_all($dir="")
         {
          global $DB ;
     
          global $FILE ;
     
          $DB->db_connect($this->dbname,$this->charset);
     
          $this->dir = $dir ? $dir : $this->dir ;
     
          $fileArray = $FILE->read_dir($dir) ;
     
          foreach ($fileArray as $filename)
          {
           $this->recover_table($filename);
          }
     
         }
     
       }
       ?>
  • 相关阅读:
    C# 不用添加WebService引用,调用WebService方法
    贪心 & 动态规划
    trie树 讲解 (转载)
    poj 2151 Check the difficulty of problems (检查问题的难度)
    poj 2513 Colored Sticks 彩色棒
    poj1442 Black Box 栈和优先队列
    啦啦啦
    poj 1265 Area(pick定理)
    poj 2418 Hardwood Species (trie树)
    poj 1836 Alignment 排队
  • 原文地址:https://www.cnblogs.com/holyes/p/2521172.html
Copyright © 2011-2022 走看看