postgresql資料庫 php實作mysql資料庫備份類

WBOY
發布: 2016-07-29 08:37:27
原創
931 人瀏覽過

1.實例化DbBak需要告訴它兩件事:資料伺服器在哪裡($connectid)、備份到哪個目錄($backupDir): 
require_once('DbBak.php');    
require_once('TableBak. php');    
$connectid = mysql_connect('localhost','root','123456');    
$backupDir =$ ');    
$backupDir =$ '5'; backupDir) ;    
2、然後就可以開始備份資料庫了,你不但能夠指定備份那個資料庫,而且能詳細設定只備份那幾個表:
   2.1如果你想備份mybbs庫中的所有表,只要這樣: 
$DbBak->backupDb('mybbs');    
2.2如果你只想備份mybbs庫中的board、face、friendlist表,可以用一個一維數組指定:
$DbBak->$DbBak-> backupDb('mybbs',array('board','face','friendsite'));    
2.3如果只想備份一個表,例如板表:
$DbBak->backupDb('mybbs', 'board');    
3,資料恢復:
對於2.1、2.1、2.3三種情況,只要對應的修改下語句,將backupDb換成restoreDb就能實現資料恢復了:
$DbBak ->restoreDb('mybbs');   
SQL程式碼
$DbBak->restoreDb('mybbs',array('board','SQL程式碼
$DbBak->restoreDb('mybbs',array('board','face','friendsite'));   
PHPface
PHPface
PHP >$DbBak->restoreDb('mybbs','board');   
PHP程式碼
require_once('TableBak.php');     var $_dataDir;    
var $_tableList;    
var $_TableBak;    
function DbBak($_my_link_id 
function DbBak($_my_link_id,$p. is_string($dataDir)) || strlen($ dataDir)==0) && die('error:$datadir is not a string');    
!is_dir($dataDir) && mkdir($dataDir);    
$this->_dataDir = $dataDir;    
$this->_mysql_link_id = $_mysql_link_id;    
}    
function backupDb($dbName,$tableName=null)
function backupDb($dbName,$tableName=null))| )) || strlen($ dbName)==0 ) && die('$dbName must be a string value');    
//step1:選擇資料庫:    
$dbDir = $this->_dataDir.DIRECTORY_SEPARATOR.$dbName;    
!is_dir($dbDir) && mkdir($dbDir);   🎜>$this->_TableBak = new TableBak($this->_mysql_link_id,$dbDir);    
if(is_null($tableName)){// $dbName);    
return;    
}    
if(is_string($tableName)){    
$this->_backupOneTable($dbName,$tableName);    
return;    
}    
if (is_array($tableName){atable> 
( (!is_string($table)) || strlen($table)==0 ) && die('....');    
}    
$this->_backupS$omeTalbeName$or$ );    
return;    
}    
}
function restoreDb($dbName,$tableName=null){    
((!is_string($dbName)) || strlen($dbName)==0 ) && die('$dbName 必須是字串值' );    
//step1:檢查是否有資料庫並連接:    
@mysql_select_db($dbName) || die(「資料庫$dbName劑量不存在」) /step2:檢查是否有資料庫備份目錄    
$dbDir = $this->_dataDir.DIRECTORY_SEPARATOR.$dbName;    
!is_dir($dbDir) &    
!is_dir($dbDir) & /第三個步驟:開始復原    
$this->_TableBak = new TableBak($this->_mysql_link_id,$dbDir);     if(is_null($tableName)))     if(is_null($tableName)){//備份資料庫中的所有表格🎜>$this->_restoreAllTable($dbName);    
回;    
}    
if(is_string($tableName)){    
if(is_string($tableName)){    
if(is_string($tableName)){  ....');    
$this->_restoreOneTable($dbName,$tableName);    
回傳;    
}   forfor tableName as $table){    
( (!is_string($table)) || strlen($table)==0 ) && 死亡('....');    
} >_restoreSomeTalbe($dbName,$tableName);    
回傳;    
}    
}     = array();    
$ result=mysql_list_tables($dbName,$this->_mysql_link_id);    
for ($i = 0; $i }    
mysql_free_result($result);    
回$tableList;    >{    
foreach ($this- >_getTableList($dbName) as $tableName){    
$this->_TableBak->backupTable ($表名);    
}    
{    
!in_array($tableName,$this->_getTableList($dbName)) && die("指定的表名$tableName資料庫中不存在");    
$ this->_TableBak->backupTable($tableName);    
}    
函數 _backupSomeTalbe($dbName,$TableNameList)    
!in_array( $tableName,$this->_getTableList($dbName) )) && die("指定的表名$tableName在資料庫中不存在");    
}    
foreach ($TableNameList as $tableName){    
$this->_TableBak->backupTable($tableName);    
}    
}    
function _restoreAllTable($dbName)    
{    
//step1:檢查是否有所有資料表的備份檔案以及是否可寫入:   tableName){    
$tableBakFile = $this->_dataDir.DIRECTORY_SEPARATOR    
          
           $dbName.DIRECTORY_SEPARATOR    
               。 $tableName.DIRECTORY_SEPARATOR    
            。 $表名.'.sql';    
!is_writeable ($tableBakFile) && die("$tableBakFile 不存在或不可寫");    
}
// step2:start restore
foreach($ this-> _ getTableList($ dbname)as $ tableName){
$> $ tablebakfile = $ tablebakfile = $> _> _ datadir .directory_deirectory_separator >。 $dbName.DIRECTORY_SEPARATOR    
               。 $tableName.DIRECTORY_SEPARATOR    
               。 $表名.'.sql';    
$this->_TableBak->restoreTable($tableName,$tableBakFile);    
}    
}    
function _restoreOneTable($dbName,$tableName)    
{    /片有檢查20008> > _getTableList($dbName)) && die("指定的表名$tableName在資料庫中不存在");    
//step2:檢查是否有資料表備份檔案以及是否可寫入:    
$tableBakFile = $this->_dataDir.DIRECTORY_SEPARATOR    
        $dbName.DIRECTORY_SEPARATOR    
      。 $tableName.DIRECTORY_SEPARATOR    
         。 $表名.'.sql';    
!is_writeable ($tableBakFile) && die("$tableBakFile 不存在或不可寫");    
//第三個步驟:開始復原    
$this->_TableBak->restoreTable($tableName,$tableBakFile);    
}    
function _restoreSomeTalbe($dbName,$TableNameList)    
{     !in_array ($tableName,$this->_getTableList($dbName)) && die("指定的表名$tableName在資料庫中不存在");    
}    
//step2:檢查是否有資料表備份檔案以及是否可寫入:    
foreach ($TableNameList as $ 🎜 >               。 $dbName.DIRECTORY_SEPARATOR    
               。 $tableName.DIRECTORY_SEPARATOR    
               。 $表名.'.sql';    
!is_writeable ($tableBakFile) && die("$tableBakFile 不存在或不可寫");    
}    
//步驟3:開始復原:    
foreach ($TableNameList as $tableName){    
$tableBakFile           。 $dbName.DIRECTORY_SEPARATOR    
               。 $tableName.DIRECTORY_SEPARATOR    
               。 $表名.'.sql';    
$this->_TableBak->restoreTable($tableName,$tableBakFile);    
}          



複製程式碼

程式碼如下:


//只有DbBak   
//private $_DbManager;     
function TableBak($mysql_link_id,$dbDir)     
{     
$this->_mysql_link_id     
}     
function backupTable($ tableName)     
{     
//step1:建立表格中的備份目錄名稱:     
$tableDir = $this->_dbDir.505; ir) && mkdir($ tableDir);     
//step2:開始備份:     
$this->_backupTable($tableName,$tableDir);      }   
{     
set_time_limit(0);     
$fileArray = @file($tableBakFile) or die("can open file $tableBakFile p; sql_unbuffered_query("DELETE FROM $tableName");     
$sql = $fileArray[0];     
for ($i=1;$i}     
return true;      
$fieldInfo = array();     
$sql="SELECT * FROM $tableName LIMIT 1";     
$result = mysql_query($sql 我 
for( $i=0;$i$field_name=mysql_field_name($result,$i);     
$field_ [$field_name] = $field_type;     
}     
mysql_free_result($result);      _quoteRow($fieldInfo,$row){     
foreach ( $row as $field_name=>$field_value){     
$field_value=strval($field_value);     
[$field_name] = "'".mysql_escape_string($field_value)."'";break;         
case "string":    >case "date":     $row[$field_name] = "'".mysql_escape_string($field_value)."'";break;       $field_value)."'";break;       
case "time":     $row[$field_name] = "'".mysql_escape_string($field_value).N'";  $ row[$field_name] = "'".mysql_escape_string($field_value)."'";break;         
case "int":  >case " real":     $row[$field_name] = intval($field_value); break;     
case "timestamp":$row[$field_ $row[$ field_name] = intval($field_value); break;     
}     
}
return $row;     
}     
function _backupTable($tableName,$tableDir)      { fieldInfo = $this->_getFieldInfo( $tableName);     
//step1:建構INSERT語句前半部 並寫入檔案:     
$fields = array_keys($fieldInfo  
$fields = array_keys($fieldInfo);
$sqltext="INSERT INTO $tableName($fields)VALUES rn";     
$datafile = $tableDir.DIRECTORY_SEPARATOR.$tableName.'..open $tableDir.DIRECTORY_SEPARATOR.$tableName.'.]open $tableDir.DIRECTORY 'w')) && die("can not open file $datafile");     
(!fwrite($handle, $sqltext))  b>$datafile");     
fclose($handle);     
//step2:取得資料 並寫入檔案:      //mitm_time ;     
$sql = "select * from $tableName";     
$result = mysql_query($ 
$datafile = $tableDir.DIRECTORY_SEPARATOR.$tableName.'.sql';     
(!$handle = fopen($datafile,'a')) && die("can not fopen($datafile,'a')) && die("can not >");     
//逐筆取得表格記錄並寫入檔案:     
while ($row = mysql_fetch_assoc($result {@).$.com/oquo );      
$record='(' . implode(',',$row) . ");rn";     
(!fwrite($handle,   to file $datafile");     
}     
mysql_free_result($result);     🎜>return true;
}     
}     
?>   

 
 bBak.php' );    
require_once('TableBak.php');    
$connectid = mysql_connect('localhost','root 🎜>$DbBak = new DbBak($connectid,$backupDir);    
$DbBak->backupDb('mybbs');    
恢復程式🎜> 🎜> 程式碼如下:


require_once('DbBak.php');     
require_once('TableBak.php ,'123456');     
$backupDir = 'data';     

$DbBak = new DbBak($connectid,$bDir)d ; 


以上就介紹了postgresql資料庫 php實作mysql資料庫備份類,包含了postgresql資料庫方面的內容,希望對PHP教學有興趣的朋友有幫助。


相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!