国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

php mysql備份恢復分卷處理

2024-05-04 21:49:12
字體:
來源:轉載
供稿:網友

分卷處理就是把握們要處理的數據分成一個個小文件進行處理了,下面我來給大家介紹一個php mysql備份恢復分卷處理類,實現mysql數據庫分卷備份,選擇表進行備份,實現單個sql文件及分卷sql導入.

分卷導入類及思路詳解

數據庫導入導出是一個后臺必要擁有的功能,網上一搜,有很多關于數據庫導入導出的,但基本上一個大的系統,包含了許多我們并不需要的,而且他們都是自己的后臺的形式,我并不喜歡的是拿人家的東西整合到自己的后臺,我需要的是自己東西,于是參照了很多,自己寫了一個關于分卷導入類,以方便調用,歡迎大家拍磚.

這里針對分卷文件是以‘_v1.sql’為結尾,實現單個sql文件及分卷sql導入,分卷導入可選擇是否當前分卷導入余下分卷,我們只需要直接調用類即可完成.

分別是主機,用戶名,密碼,數據庫名,數據庫編碼

$db = new DataManage ( 'localhost', 'root', 'root', 'test', 'utf8' );

sql文件,是否只導入單個sql(即如果有其他分卷也不導入).

$db->restore ( './backup/20120516211738_all_v1.sql', false );對應如何去列出備份的sql文件或選擇sql之類的,自己去實現,那個不在這個范疇了,也很簡單的.

還有目前只實現了數據庫導入,關于數據庫導出的,正在編寫功能,下面是完整的類代碼,具體思路及實現代碼里面都有說明,這里不在贅述,代碼如下:

  1. <?php 
  2. /** 
  3.  * @author yanue 
  4.  * @copyright Copyright (c) 2012 m.survivalescaperooms.com 
  5.  * 說明:分卷文件是以_v1.sql為結尾 
  6.  * 功能:實現單個sql文件及分卷sql導入,分卷導入可選擇是否當前分卷導入余下分卷 
  7.  * 使用方法: 
  8.  * 
  9.  * 
  10.  * ------------------------------------------------------------------ 
  11. //分別是主機,用戶名,密碼,數據庫名,數據庫編碼 
  12. $db = new DataManage ( 'localhost', 'root', 'root', 'test', 'utf8' ); 
  13. //sql文件,是否只導入單個sql(即如果有其他分卷也不導入) 
  14. $db->restore ( './backup/20120516211738_all_v1.sql', false ); 
  15.  *---------------------------------------------------------------------- 
  16.  */ 
  17. class DataManage { 
  18.  var $db// 數據庫連接 
  19.  var $database// 所用數據庫 
  20.  var $sqldir// 數據庫備份文件夾 
  21.  
  22.  /** 
  23.   * 初始化 
  24.   * 
  25.   * @param string $host 
  26.   * @param string $username 
  27.   * @param string $password 
  28.   * @param string $database 
  29.   * @param string $charset 
  30.   */ 
  31.  function __construct($host = 'localhost'$username = 'root'$password = ''$database = 'test'$charset = 'utf8') { 
  32.   $this->host = $host
  33.   $this->username = $username
  34.   $this->password = $password
  35.   $this->database = $database
  36.   $this->charset = $charset
  37.   // 連接數據庫 
  38.   $this->db = mysql_connect ( $this->host, $this->username, $this->password ) or die ( "數據庫連接失敗." ); 
  39.   // 選擇使用哪個數據庫 
  40.   mysql_select_db ( $this->database, $this->db ) or die ( "無法打開數據庫" ); 
  41.   // 數據庫編碼方式 
  42.   mysql_query ( 'SET NAMES ' . $this->charset, $this->db ); 
  43.  } 
  44.  
  45.  /** 
  46.   * 導入備份數據 
  47.   * 說明:分卷文件格式20120516211738_all_v1.sql 
  48.   * 
  49.   * @param string $sqlfile 
  50.   * @param bool $single 
  51.   */ 
  52.  function restore($sqlfile$single = FALSE) { 
  53.   // 檢測文件是否存在 
  54.   if (! file_exists ( $sqlfile )) { 
  55.    exit ( "文件不存在!請檢查" ); 
  56.   } 
  57.   $this->lock ( $this->database ); 
  58.   // 獲取數據庫存儲位置 
  59.   $sqlpath = pathinfo ( $sqlfile ); 
  60.   $this->sqldir = $sqlpath ['dirname']; 
  61.   // 檢測是否包含分卷,將類似20120516211738_all_v1.sql從_v分開,有則說明有分卷 
  62.   $volume = explode ( "_v"$sqlfile ); 
  63.   $volume_path = $volume [0]; 
  64.   echo "請勿刷新及關閉瀏覽器以防止程序被中止,如有不慎!將導致數據庫結構受損<br />"
  65.   echo "正在導入備份數據,請稍等!<br />"
  66.   if (emptyempty ( $volume [1] ) || $single) { 
  67.    echo "正在導入sql:<span style='color:#f00;'>" . $sqlfile . '</span><br />'
  68.    // 沒有分卷 
  69.    if ($this->_import ( $sqlfile )) { 
  70.     echo "數據庫導入成功!"
  71.    } else { 
  72.     exit ( '數據庫導入失敗!' ); 
  73.    } 
  74.   } else { 
  75.    // 存在分卷,則獲取當前是第幾分卷,循環執行余下分卷 
  76.    $volume_id = explode ( ".sq"$volume [1] ); 
  77.    // 當前分卷為$volume_id 
  78.    $volume_id = intval ( $volume_id [0] ); 
  79.    while ( $volume_id ) { 
  80.     $tmpfile = $volume_path . "_v" . $volume_id . ".sql"
  81.     // 存在其他分卷,繼續執行 
  82.     if (file_exists ( $tmpfile )) { 
  83.      // 執行導入方法 
  84.      echo "正在導入分卷$volume_id:<span style='color:#f00;'>" . $tmpfile . '</span><br />'
  85.      if ($this->_import ( $tmpfile )) { 
  86.  
  87.      } else { 
  88.       exit ( "導入分卷$volume_id:<span style='color:#f00;'>" . $tmpfile . '</span>失??!可能是數據庫結構已損壞!請嘗試從分卷1開始導入' ); 
  89.      } 
  90.     } else { 
  91.      echo "此分卷備份全部導入成功!<br />"
  92.      return
  93.     } 
  94.     $volume_id ++; 
  95.    } 
  96.   } 
  97.  } 
  98.  
  99.  /** 
  100.   * 將sql導入到數據庫(普通導入) 
  101.   * 
  102.   * @param string $sqlfile 
  103.   * @return boolean 
  104.   */ 
  105.  private function _import($sqlfile) { 
  106.   $name = basename ( $sqlfile ); 
  107.   $sqls = file ( $sqlfile ); 
  108.   foreach ( $sqls as $sql ) { 
  109.    str_replace ( "r"""$sql ); 
  110.    str_replace ( "n"""$sql ); 
  111.    if (! mysql_query ( trim ( $sql ), $this->db )) 
  112.     return false; 
  113.   } 
  114.   return true; 
  115.  } 
  116.  
  117.  // 關閉數據庫連接 
  118.  private function close() { 
  119.   mysql_close ( $this->db ); 
  120.  } 
  121.  
  122.  // 鎖定數據庫,以免備份或導入時出錯 
  123.  private function lock($tablename$op = "WRITE") { 
  124.   if (mysql_query ( "lock tables " . $tablename . " " . $op )) 
  125.    return true; 
  126.   else 
  127.    return false; 
  128.  } 
  129.  
  130.  // 解鎖 
  131.  private function unlock() { 
  132.   if (mysql_query ( "unlock tables" )) 
  133.    return true; 
  134.   else 
  135.    return false; 
  136.  } 
  137.  
  138.  // 析構 
  139.  function __destruct() { 
  140.   mysql_query ( "unlock tables"$this->db ); 
  141.   mysql_close ( $this->db ); 
  142.  } 
  143. ?> 

mysql備份恢復分卷處理,調用簡單.

分卷導入思路:按行讀取sql文件,將每一行當作完整的sql語句存到數組再循環執行插入數據庫就可以了,但是在創建表語句分了多行,這個需要單獨處理,就這個花了我好長時間的.感覺文章好長啊,主要是那個類文件給占用了.

更新說明: 

1.去除sql導入的時候排除sql文件里面的注釋’– ‘ 從而解決sql中單雙引號不能導入

2.單行讀取后的sql直接執行,避免重新將sql語句組合到數組中再從數組中讀取導入sql,提高效率.

下載地址: https://github.com/yanue/Dbmanage

導出后的sql文件格式如下:

  1. -- 
  2. -- MySQL database dump 
  3. -- Created by DBManage class, Power By yanue.  
  4. -- 
  5. -- 主機: localhost 
  6. -- 生成日期: 2012 年  10 月 06 日 22:32 
  7. -- MySQL版本: 5.1.50-community 
  8. -- PHP 版本: 5.3.9-ZS5.6.0 
  9.  
  10. -- 
  11. -- 數據庫: `test` 
  12. -- 
  13.  
  14. -- ------------------------------------------------------- 
  15.  
  16. -- 
  17. -- 表的結構aa 
  18. -- 
  19.  
  20. DROP TABLE IF EXISTS `aa`; 
  21. CREATE TABLE `aa` ( 
  22.   `id` int(10) unsigned NOT NULL AUTO_INCREMENT, 
  23.   `content` text NOT NULL
  24.   PRIMARY KEY (`id`) 
  25. ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; 
  26.  
  27. -- 
  28. -- 轉存表中的數據 aa 
  29. -- 
  30.  
  31. INSERT INTO `aa` VALUES('1','<p id="test"><span class='hahh' style="line-height:;">我是測試數據 呵呵</span></p>'); 

下面是類代碼:

  1. <?php 
  2. /** 
  3.  * @author yanue 
  4.  * @copyright  Copyright (c) 2012 yanue.net 
  5.  * @link  http://Vevb.com 
  6.  * @version 1.1 
  7.  * 創建時間: 2012年5月21日 
  8.  
  9. 更新時間: 2012年10月6日 
  10. 更新說明: 1.去除sql導入的時候排除sql文件里面的注釋'-- ' 從而解決sql中單雙引號不能導入 
  11. 2.單行讀取后的sql直接執行,避免重新將sql語句組合到數組中再從數組中讀取導入sql,提高效率 
  12.  
  13.  * 說明:分卷文件是以_v1.sql為結尾(20120522021241_all_v1.sql) 
  14.  * 功能:實現mysql數據庫分卷備份,選擇表進行備份,實現單個sql文件及分卷sql導入 
  15.  * 使用方法: 
  16.  * 
  17.  * ------1. 數據庫備份(導出)------------------------------------------------------------ 
  18. //分別是主機,用戶名,密碼,數據庫名,數據庫編碼 
  19. $db = new DBManage ( 'localhost', 'root', 'root', 'test', 'utf8' ); 
  20. // 參數:備份哪個表(可選),備份目錄(可選,默認為backup),分卷大小(可選,默認2000,即2M) 
  21. $db->backup (); 
  22.  * ------2. 數據庫恢復(導入)------------------------------------------------------------ 
  23. //分別是主機,用戶名,密碼,數據庫名,數據庫編碼 
  24. $db = new DBManage ( 'localhost', 'root', 'root', 'test', 'utf8' ); 
  25. //參數:sql文件 
  26. $db->restore ( './backup/20120516211738_all_v1.sql'); 
  27.  *---------------------------------------------------------------------- 
  28.  */ 
  29. class DbManage { 
  30.     var $db// 數據庫連接 
  31.     var $database// 所用數據庫 
  32.     var $sqldir// 數據庫備份文件夾 
  33.     // 換行符 
  34.     private $ds = "n"
  35.     // 存儲SQL的變量 
  36.     public $sqlContent = ""
  37.     // 每條sql語句的結尾符 
  38.     public $sqlEnd = ";"
  39.  
  40.     /** 
  41.      * 初始化 
  42.      * 
  43.      * @param string $host 
  44.      * @param string $username 
  45.      * @param string $password 
  46.      * @param string $database 
  47.      * @param string $charset 
  48.      */ 
  49.     function __construct($host = 'localhost'$username = 'root'$password = ''$database = 'test'$charset = 'utf8') { 
  50.         $this->host = $host
  51.         $this->username = $username
  52.         $this->password = $password
  53.         $this->database = $database
  54.         $this->charset = $charset
  55.         set_time_limit(0);//無時間限制 
  56. @ob_end_flush(); 
  57.         // 連接數據庫 
  58.         $this->db = @mysql_connect ( $this->host, $this->username, $this->password ) or die'<p class="dbDebug"><span class="err">Mysql Connect Error : </span>'.mysql_error().'</p>'); 
  59.         // 選擇使用哪個數據庫 
  60.         mysql_select_db ( $this->database, $this->db ) or die('<p class="dbDebug"><span class="err">Mysql Connect Error:</span>'.mysql_error().'</p>'); 
  61.         // 數據庫編碼方式 
  62.         mysql_query ( 'SET NAMES ' . $this->charset, $this->db ); 
  63.  
  64.     } 
  65.  
  66.     /* 
  67.      * 新增查詢數據庫表 
  68.      */ 
  69.     function getTables() { 
  70.         $res = mysql_query ( "SHOW TABLES" ); 
  71.         $tables = array (); 
  72.         while ( $row = mysql_fetch_array ( $res ) ) { 
  73.             $tables [] = $row [0]; 
  74.         } 
  75.         return $tables
  76.     } 
  77.  
  78.     /* 
  79.      * 
  80.      * ------------------------------------------數據庫備份start---------------------------------------------------------- 
  81.      */ 
  82.  
  83.     /** 
  84.      * 數據庫備份 
  85.      * 參數:備份哪個表(可選),備份目錄(可選,默認為backup),分卷大小(可選,默認2000,即2M) 
  86.      * 
  87.      * @param $string $dir 
  88.      * @param int $size 
  89.      * @param $string $tablename 
  90.      */ 
  91.     function backup($tablename = ''$dir$size) { 
  92.         $dir = $dir ? $dir : './backup/'
  93.         // 創建目錄 
  94.         if (! is_dir ( $dir )) { 
  95.             mkdir ( $dir, 0777, true ) or die ( '創建文件夾失敗' ); 
  96.         } 
  97.         $size = $size ? $size : 2048; 
  98.         $sql = ''
  99.         // 只備份某個表 
  100.         if (! emptyempty ( $tablename )) { 
  101.             if(@mysql_num_rows(mysql_query("SHOW TABLES LIKE '".$tablename."'")) == 1) { 
  102.              } else { 
  103.                 $this->_showMsg('表-<b>' . $tablename .'</b>-不存在,請檢查!',true); 
  104.                 die(); 
  105.             } 
  106.             $this->_showMsg('正在備份表 <span class="imp">' . $tablename.'</span>'); 
  107.             // 插入dump信息 
  108.             $sql = $this->_retrieve (); 
  109.             // 插入表結構信息 
  110.             $sql .= $this->_insert_table_structure ( $tablename ); 
  111.             // 插入數據 
  112.             $data = mysql_query ( "select * from " . $tablename ); 
  113.             // 文件名前面部分 
  114.             $filename = date ( 'YmdHis' ) . "_" . $tablename
  115.             // 字段數量 
  116.             $num_fields = mysql_num_fields ( $data ); 
  117.             // 第幾分卷 
  118.             $p = 1; 
  119.             // 循環每條記錄 
  120.             while ( $record = mysql_fetch_array ( $data ) ) { 
  121.                 // 單條記錄 
  122.                 $sql .= $this->_insert_record ( $tablename$num_fields$record ); 
  123.                 // 如果大于分卷大小,則寫入文件 
  124.                 if (strlen ( $sql ) >= $size * 1024) { 
  125.                     $file = $filename . "_v" . $p . ".sql"
  126.                     if ($this->_write_file ( $sql$file$dir )) { 
  127.                         $this->_showMsg("表-<b>" . $tablename . "</b>-卷-<b>" . $p . "</b>-數據備份完成,備份文件 [ <span class='imp'>" .$dir . $file ."</span> ]"); 
  128.                     } else { 
  129.                         $this->_showMsg("備份表 -<b>" . $tablename . "</b>- 失敗",true); 
  130.                         return false; 
  131.                     } 
  132.                     // 下一個分卷 
  133.                     $p ++; 
  134.                     // 重置$sql變量為空,重新計算該變量大小 
  135.                     $sql = ""
  136.                 } 
  137.             } 
  138.             // 及時清除數據 
  139.             unset($data,$record); 
  140.             // sql大小不夠分卷大小 
  141.             if ($sql != "") { 
  142.                 $filename .= "_v" . $p . ".sql"
  143.                 if ($this->_write_file ( $sql$filename$dir )) { 
  144.                     $this->_showMsg( "表-<b>" . $tablename . "</b>-卷-<b>" . $p . "</b>-數據備份完成,備份文件 [ <span class='imp'>" .$dir . $filename ."</span> ]"); 
  145.                 } else { 
  146.                     $this->_showMsg("備份卷-<b>" . $p . "</b>-失敗<br />"); 
  147.                     return false; 
  148.                 } 
  149.             } 
  150.             $this->_showMsg("恭喜您! <span class='imp'>備份成功</span>"); 
  151.         } else { 
  152.             $this->_showMsg('正在備份'); 
  153.             // 備份全部表 
  154.             if ($tables = mysql_query ( "show table status from " . $this->database )) { 
  155.                 $this->_showMsg("讀取數據庫結構成功!"); 
  156.             } else { 
  157.                 $this->_showMsg("讀取數據庫結構失?。?quot;); 
  158.                 exit ( 0 ); 
  159.             } 
  160.             // 插入dump信息 
  161.             $sql .= $this->_retrieve (); 
  162.             // 文件名前面部分 
  163.             $filename = date ( 'YmdHis' ) . "_all"
  164.             // 查出所有表 
  165.             $tables = mysql_query ( 'SHOW TABLES' ); 
  166.             // 第幾分卷 
  167.             $p = 1; 
  168.             // 循環所有表 
  169.             while ( $table = mysql_fetch_array ( $tables ) ) { 
  170.                 // 獲取表名 
  171.                 $tablename = $table [0]; 
  172.                 // 獲取表結構 
  173.                 $sql .= $this->_insert_table_structure ( $tablename ); 
  174.                 $data = mysql_query ( "select * from " . $tablename ); 
  175.                 $num_fields = mysql_num_fields ( $data ); 
  176.  
  177.                 // 循環每條記錄 
  178.                 while ( $record = mysql_fetch_array ( $data ) ) { 
  179.                     // 單條記錄 
  180.                     $sql .= $this->_insert_record ( $tablename$num_fields$record ); 
  181.                     // 如果大于分卷大小,則寫入文件 
  182.                     if (strlen ( $sql ) >= $size * 1000) { 
  183.  
  184.                         $file = $filename . "_v" . $p . ".sql"
  185.                         // 寫入文件 
  186.                         if ($this->_write_file ( $sql$file$dir )) { 
  187.                             $this->_showMsg("-卷-<b>" . $p . "</b>-數據備份完成,備份文件 [ <span class='imp'>".$dir.$file."</span> ]"); 
  188.                         } else { 
  189.                             $this->_showMsg("卷-<b>" . $p . "</b>-備份失敗!",true); 
  190.                             return false; 
  191.                         } 
  192.                         // 下一個分卷 
  193.                         $p ++; 
  194.                         // 重置$sql變量為空,重新計算該變量大小 
  195.                         $sql = ""
  196.                     } 
  197.                 } 
  198.             } 
  199.             // sql大小不夠分卷大小 
  200.             if ($sql != "") { 
  201.                 $filename .= "_v" . $p . ".sql"
  202.                 if ($this->_write_file ( $sql$filename$dir )) { 
  203.                     $this->_showMsg("-卷-<b>" . $p . "</b>-數據備份完成,備份文件 [ <span class='imp'>".$dir.$filename."</span> ]"); 
  204.                 } else { 
  205.                     $this->_showMsg("卷-<b>" . $p . "</b>-備份失敗",true); 
  206.                     return false; 
  207.                 } 
  208.             } 
  209.             $this->_showMsg("恭喜您! <span class='imp'>備份成功</span>"); 
  210.         } 
  211.     } 
  212.  
  213.     //  及時輸出信息 
  214.     private function _showMsg($msg,$err=false){ 
  215.         $err = $err ? "<span class='err'>ERROR:</span>" : '' ; 
  216.         echo "<p class='dbDebug'>".$err . $msg."</p>"
  217.         flush(); 
  218.  
  219.     } 
  220.  
  221.     /** 
  222.      * 插入數據庫備份基礎信息 
  223.      * 
  224.      * @return string 
  225.      */ 
  226.     private function _retrieve() { 
  227.         $value = ''
  228.         $value .= '--' . $this->ds; 
  229.         $value .= '-- MySQL database dump' . $this->ds; 
  230.         $value .= '-- Created by DbManage class, Power By yanue. ' . $this->ds; 
  231.         $value .= '-- http://yanue.net ' . $this->ds; 
  232.         $value .= '--' . $this->ds; 
  233.         $value .= '-- 主機: ' . $this->host . $this->ds; 
  234.         $value .= '-- 生成日期: ' . date ( 'Y' ) . ' 年  ' . date ( 'm' ) . ' 月 ' . date ( 'd' ) . ' 日 ' . date ( 'H:i' ) . $this->ds; 
  235.         $value .= '-- MySQL版本: ' . mysql_get_server_info () . $this->ds; 
  236.         $value .= '-- PHP 版本: ' . phpversion () . $this->ds; 
  237.         $value .= $this->ds; 
  238.         $value .= '--' . $this->ds; 
  239.         $value .= '-- 數據庫: `' . $this->database . '`' . $this->ds; 
  240.         $value .= '--' . $this->ds . $this->ds; 
  241.         $value .= '-- -------------------------------------------------------'
  242.         $value .= $this->ds . $this->ds; 
  243.         return $value
  244.     } 
  245.  
  246.     /** 
  247.      * 插入表結構 
  248.      * 
  249.      * @param unknown_type $table 
  250.      * @return string 
  251.      */ 
  252.     private function _insert_table_structure($table) { 
  253.         $sql = ''
  254.         $sql .= "--" . $this->ds; 
  255.         $sql .= "-- 表的結構" . $table . $this->ds; 
  256.         $sql .= "--" . $this->ds . $this->ds; 
  257.  
  258.         // 如果存在則刪除表 
  259.         $sql .= "DROP TABLE IF EXISTS `" . $table . '`' . $this->sqlEnd . $this->ds; 
  260.         // 獲取詳細表信息 
  261.         $res = mysql_query ( 'SHOW CREATE TABLE `' . $table . '`' ); 
  262.         $row = mysql_fetch_array ( $res ); 
  263.         $sql .= $row [1]; 
  264.         $sql .= $this->sqlEnd . $this->ds; 
  265.         // 加上 
  266.         $sql .= $this->ds; 
  267.         $sql .= "--" . $this->ds; 
  268.         $sql .= "-- 轉存表中的數據 " . $table . $this->ds; 
  269.         $sql .= "--" . $this->ds; 
  270.         $sql .= $this->ds; 
  271.         return $sql
  272.     } 
  273.  
  274.     /** 
  275.      * 插入單條記錄 
  276.      * 
  277.      * @param string $table 
  278.      * @param int $num_fields 
  279.      * @param array $record 
  280.      * @return string 
  281.      */ 
  282.     private function _insert_record($table$num_fields$record) { 
  283.         // sql字段逗號分割 
  284.         $insert = ''
  285.         $comma = ""
  286.         $insert .= "INSERT INTO `" . $table . "` VALUES("
  287.         // 循環每個子段下面的內容 
  288.         for($i = 0; $i < $num_fields$i ++) { 
  289.             $insert .= ($comma . "'" . mysql_escape_string ( $record [$i] ) . "'"); 
  290.             $comma = ","
  291.         } 
  292.         $insert .= ");" . $this->ds; 
  293.         return $insert
  294.     } 
  295.  
  296.     /** 
  297.      * 寫入文件 
  298.      * 
  299.      * @param string $sql 
  300.      * @param string $filename 
  301.      * @param string $dir 
  302.      * @return boolean 
  303.      */ 
  304.     private function _write_file($sql$filename$dir) { 
  305.         $dir = $dir ? $dir : './backup/'
  306.         // 創建目錄 
  307.         if (! is_dir ( $dir )) { 
  308.             mkdir ( $dir, 0777, true ); 
  309.         } 
  310.         $re = true; 
  311.         if (! @$fp = fopen ( $dir . $filename"w+" )) { 
  312.             $re = false; 
  313.             $this->_showMsg("打開sql文件失敗!",true); 
  314.         } 
  315.         if (! @fwrite ( $fp$sql )) { 
  316.             $re = false; 
  317.             $this->_showMsg("寫入sql文件失敗,請文件是否可寫",true); 
  318.         } 
  319.         if (! @fclose ( $fp )) { 
  320.             $re = false; 
  321.             $this->_showMsg("關閉sql文件失??!",true); 
  322.         } 
  323.         return $re
  324.     } 
  325.  
  326.     /* 
  327.      * 
  328.      * -------------------------------上:數據庫導出-----------分割線----------下:數據庫導入-------------------------------- 
  329.      */ 
  330.  
  331.     /** 
  332.      * 導入備份數據 
  333.      * 說明:分卷文件格式20120516211738_all_v1.sql 
  334.      * 參數:文件路徑(必填) 
  335.      * 
  336.      * @param string $sqlfile 
  337.      */ 
  338.     function restore($sqlfile) { 
  339.         // 檢測文件是否存在 
  340.         if (! file_exists ( $sqlfile )) { 
  341.             $this->_showMsg("sql文件不存在!請檢查",true); 
  342.             exit (); 
  343.         } 
  344.         $this->lock ( $this->database ); 
  345.         // 獲取數據庫存儲位置 
  346.         $sqlpath = pathinfo ( $sqlfile ); 
  347.         $this->sqldir = $sqlpath ['dirname']; 
  348.         // 檢測是否包含分卷,將類似20120516211738_all_v1.sql從_v分開,有則說明有分卷 
  349.         $volume = explode ( "_v"$sqlfile ); 
  350.         $volume_path = $volume [0]; 
  351.         $this->_showMsg("請勿刷新及關閉瀏覽器以防止程序被中止,如有不慎!將導致數據庫結構受損"); 
  352.         $this->_showMsg("正在導入備份數據,請稍等!"); 
  353.         if (emptyempty ( $volume [1] )) { 
  354.             $this->_showMsg ( "正在導入sql:<span class='imp'>" . $sqlfile . '</span>'); 
  355.             // 沒有分卷 
  356.             if ($this->_import ( $sqlfile )) { 
  357.                 $this->_showMsg( "數據庫導入成功!"); 
  358.             } else { 
  359.                  $this->_showMsg('數據庫導入失??!',true); 
  360.                 exit (); 
  361.             } 
  362.         } else { 
  363.             // 存在分卷,則獲取當前是第幾分卷,循環執行余下分卷 
  364.             $volume_id = explode ( ".sq"$volume [1] ); 
  365.             // 當前分卷為$volume_id 
  366.             $volume_id = intval ( $volume_id [0] ); 
  367.             while ( $volume_id ) { 
  368.                 $tmpfile = $volume_path . "_v" . $volume_id . ".sql"
  369.                 // 存在其他分卷,繼續執行 
  370.                 if (file_exists ( $tmpfile )) { 
  371.                     // 執行導入方法 
  372.                     $this->msg .= "正在導入分卷 $volume_id :<span style='color:#f00;'>" . $tmpfile . '</span><br />'
  373.                     if ($this->_import ( $tmpfile )) { 
  374.  
  375.                     } else { 
  376.                         $volume_id = $volume_id ? $volume_id :1; 
  377.                         exit ( "導入分卷:<span style='color:#f00;'>" . $tmpfile . '</span>失敗!可能是數據庫結構已損壞!請嘗試從分卷1開始導入' ); 
  378.                     } 
  379.                 } else { 
  380.                     $this->msg .= "此分卷備份全部導入成功!<br />"
  381.                     return
  382.                 } 
  383.                 $volume_id ++; 
  384.             } 
  385.         }if (emptyempty ( $volume [1] )) { 
  386.             $this->_showMsg ( "正在導入sql:<span class='imp'>" . $sqlfile . '</span>'); 
  387.             // 沒有分卷 
  388.             if ($this->_import ( $sqlfile )) { 
  389.                 $this->_showMsg( "數據庫導入成功!"); 
  390.             } else { 
  391.                  $this->_showMsg('數據庫導入失敗!',true); 
  392.                 exit (); 
  393.             } 
  394.         } else { 
  395.             // 存在分卷,則獲取當前是第幾分卷,循環執行余下分卷 
  396.             $volume_id = explode ( ".sq"$volume [1] ); 
  397.             // 當前分卷為$volume_id 
  398.             $volume_id = intval ( $volume_id [0] ); 
  399.             while ( $volume_id ) { 
  400.                 $tmpfile = $volume_path . "_v" . $volume_id . ".sql"
  401.                 // 存在其他分卷,繼續執行 
  402.                 if (file_exists ( $tmpfile )) { 
  403.                     // 執行導入方法 
  404.                     $this->msg .= "正在導入分卷 $volume_id :<span style='color:#f00;'>" . $tmpfile . '</span><br />'
  405.                     if ($this->_import ( $tmpfile )) { 
  406.  
  407.                     } else { 
  408.                         $volume_id = $volume_id ? $volume_id :1; 
  409.                         exit ( "導入分卷:<span style='color:#f00;'>" . $tmpfile . '</span>失?。】赡苁菙祿旖Y構已損壞!請嘗試從分卷1開始導入' ); 
  410.                     } 
  411.                 } else { 
  412.                     $this->msg .= "此分卷備份全部導入成功!<br />"
  413.                     return
  414.                 } 
  415.                 $volume_id ++; 
  416.             } 
  417.         } 
  418.     } 
  419.  
  420.     /** 
  421.      * 將sql導入到數據庫(普通導入) 
  422.      * 
  423.      * @param string $sqlfile 
  424.      * @return boolean 
  425.      */ 
  426.     private function _import($sqlfile) { 
  427.         // sql文件包含的sql語句數組 
  428.         $sqls = array (); 
  429.         $f = fopen ( $sqlfile"rb" ); 
  430.         // 創建表緩沖變量 
  431.         $create_table = ''
  432.         while ( ! feof ( $f ) ) { 
  433.             // 讀取每一行sql 
  434.             $line = fgets ( $f ); 
  435.             // 這一步為了將創建表合成完整的sql語句 
  436.             // 如果結尾沒有包含';'(即為一個完整的sql語句,這里是插入語句),并且不包含'ENGINE='(即創建表的最后一句) 
  437.             if (! preg_match ( '/;/'$line ) || preg_match ( '/ENGINE=/'$line )) { 
  438.                 // 將本次sql語句與創建表sql連接存起來 
  439.                 $create_table .= $line
  440.                 // 如果包含了創建表的最后一句 
  441.                 if (preg_match ( '/ENGINE=/'$create_table)) { 
  442.                     //執行sql語句創建表 
  443.                     $this->_insert_into($create_table); 
  444.                     // 清空當前,準備下一個表的創建 
  445.                     $create_table = ''
  446.                 } 
  447.                 // 跳過本次 
  448.                 continue
  449.             } 
  450.             //執行sql語句 
  451.             $this->_insert_into($line); 
  452.         } 
  453.         fclose ( $f ); 
  454.         return true; 
  455.     } 
  456.  
  457.     //插入單條sql語句 
  458.     private function _insert_into($sql){ 
  459.         if (! mysql_query ( trim ( $sql ) )) { 
  460.             $this->msg .= mysql_error (); 
  461.             return false; 
  462.         } 
  463.     } 
  464.  
  465.     /* 
  466.      * -------------------------------數據庫導入end--------------------------------- 
  467.      */ 
  468.  
  469.     // 關閉數據庫連接 
  470.     private function close() { 
  471.         mysql_close ( $this->db ); 
  472.     } 
  473.  
  474.     // 鎖定數據庫,以免備份或導入時出錯 
  475.     private function lock($tablename$op = "WRITE") { 
  476.         if (mysql_query ( "lock tables " . $tablename . " " . $op )) 
  477.             return true; 
  478.         else 
  479.             return false; 
  480.     } 
  481.  
  482.     // 解鎖 
  483.     private function unlock() { 
  484.         if (mysql_query ( "unlock tables" )) 
  485.             return true; 
  486.         else 
  487.             return false; 
  488.     } 
  489.  
  490.     // 析構 
  491.     function __destruct() { 
  492.         if($this->db){ 
  493.             mysql_query ( "unlock tables"$this->db ); 
  494.             mysql_close ( $this->db ); 
  495.         } 
  496.     } 
  497.  
  498. ?>

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 阳原县| 静乐县| 台东县| 岢岚县| 永平县| 绥德县| 龙里县| 惠来县| 四川省| 大田县| 出国| 沙坪坝区| 高平市| 青河县| 平江县| 黔江区| 田阳县| 内江市| 英山县| 松滋市| 历史| 碌曲县| 舟曲县| 旬邑县| 西林县| 琼海市| 连江县| 潞城市| 永川市| 沙湾县| 黑龙江省| 哈密市| 南安市| 日照市| 漾濞| 镇赉县| 赤峰市| 多伦县| 油尖旺区| 象州县| 大名县|