Home > Backend Development > PHP Tutorial > postgresql database php implements mysql database backup class

postgresql database php implements mysql database backup class

WBOY
Release: 2016-07-29 08:37:27
Original
1001 people have browsed it

1. To instantiate DbBak, you need to tell it two things: where the data server is ($connectid) and which directory to back up to ($backupDir):
require_once('DbBak.php');
require_once('TableBak.php');
$connectid = mysql_connect('localhost','root','123456');
$backupDir = 'data';
$DbBak = new DbBak($connectid,$backupDir);
2. Then you can start backing up the database Now, you can not only specify which database to back up, but also set up in detail only which tables to back up:
2.1 If you want to back up all the tables in the mybbs library, just do this:
$DbBak->backupDb('mybbs');
2.2 If you only want to back up the board, face, and friendlist tables in the mybbs library, you can specify it with a one-dimensional array:
$DbBak->backupDb('mybbs',array('board','face','friendsite '));
2.3 If you only want to back up one table, such as the board table:
$DbBak->backupDb('mybbs','board');
3. Data recovery:
For the three cases of 2.1, 2.1 and 2.3 , as long as you modify the statement accordingly and replace backupDb with restoreDb, you can achieve data recovery:
$DbBak->restoreDb('mybbs');
SQL code
$DbBak->restoreDb('mybbs',array( 'board','face','friendsite'));
PHP code
$DbBak->restoreDb('mybbs','board');
PHP code
require_once('TableBak.php');
class DbBak {
var $_mysql_link_id;
var $_dataDir;
var $_tableList;
var $_TableBak;
function DbBak($_mysql_link_id,$dataDir)
{
( (!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)
{
( (!is_string($dbName)) || strlen($dbName)==0 ) && die('$dbName must be a string value');
//step1: Select database:
mysql_select_db($dbName);
//step2: Create database backup directory
$dbDir = $this->_dataDir.DIRECTORY_SEPARATOR.$dbName;
! is_dir( $dbDir) && mkdir($dbDir);
//step3: Get all table names in the database and start backing up the tables
$this->_TableBak = new TableBak($this->_mysql_link_id,$dbDir);
if(is_null ($tableName)){//backup all table in the db
$this->_backupAllTable($dbName);
return;
}
if(is_string($tableName)){
(strlen($tableName)== 0) && die('....');
$this->_backupOneTable($dbName,$tableName);
return;
}
if (is_array($tableName)){
foreach ($tableName as $ table){
( (!is_string($table)) || strlen($table)==0 ) && die('....');
}
$this->_backupSomeTalbe($dbName,$tableName );
return;
}
}    
function restoreDb($dbName,$tableName=null){    
( (!is_string($dbName)) || strlen($dbName)==0 ) && die('$dbName must be a string value');    
//step1:检查是否存在数据库 并连接:    
@mysql_select_db($dbName) || die("the database $dbName dose not exists");    
//step2:检查是否存在数据库备份目录    
$dbDir = $this->_dataDir.DIRECTORY_SEPARATOR.$dbName;    
!is_dir($dbDir) && die("$dbDir not exists");    
//step3:start restore    
$this->_TableBak = new TableBak($this->_mysql_link_id,$dbDir);    
if(is_null($tableName)){//backup all table in the db    
$this->_restoreAllTable($dbName);    
return;    
}    
if(is_string($tableName)){    
(strlen($tableName)==0) && die('....');    
$this->_restoreOneTable($dbName,$tableName);    
return;    
}    
if (is_array($tableName)){    
foreach ($tableName as $table){    
( (!is_string($table)) || strlen($table)==0 ) && die('....');    
}    
$this->_restoreSomeTalbe($dbName,$tableName);    
return;    
}    
}    
function _getTableList($dbName)    
{    
$tableList = array();    
$result=mysql_list_tables($dbName,$this->_mysql_link_id);    
for ($i = 0; $i < mysql_num_rows($result); $i++){    
        array_push($tableList,mysql_tablename($result, $i));    
}    
mysql_free_result($result);    
return $tableList;    
}    
function _backupAllTable($dbName)    
{    
foreach ($this->_getTableList($dbName) as $tableName){    
$this->_TableBak->backupTable($tableName);    
}    
}    
function _backupOneTable($dbName,$tableName)    
{    
!in_array($tableName,$this->_getTableList($dbName)) && die("指定的表名$tableName在数据库中不存在");    
$this->_TableBak->backupTable($tableName);    
}    
function _backupSomeTalbe($dbName,$TableNameList)    
{    
foreach ($TableNameList as $tableName){    
!in_array($tableName,$this->_getTableList($dbName)) && die("指定的表名$tableName在数据库中不存在");    
}    
foreach ($TableNameList as $tableName){    
$this->_TableBak->backupTable($tableName);    
}    
}    
function _restoreAllTable($dbName)    
{    
//step1:检查是否存在所有数据表的备份文件 以及是否可写:    
foreach ($this->_getTableList($dbName) as $tableName){    
$tableBakFile = $this->_dataDir.DIRECTORY_SEPARATOR    
            . $dbName.DIRECTORY_SEPARATOR    
               . $tableName.DIRECTORY_SEPARATOR    
            . $tableName.'.sql';    
!is_writeable ($tableBakFile) && die("$tableBakFile not exists or unwirteable");    
}    
//step2:start restore    
foreach ($this->_getTableList($dbName) as $tableName){    
$tableBakFile = $this->_dataDir.DIRECTORY_SEPARATOR    
               . $dbName.DIRECTORY_SEPARATOR    
               . $tableName.DIRECTORY_SEPARATOR    
               . $tableName.'.sql';    
$this->_TableBak->restoreTable($tableName,$tableBakFile);    
}    
}    
function _restoreOneTable($dbName,$tableName)    
{    
//step1:检查是否存在数据表:    
!in_array($tableName,$this->_getTableList($dbName)) && die("指定的表名$tableName在数据库中不存在");    
//step2:检查是否存在数据表备份文件 以及是否可写:    
$tableBakFile = $this->_dataDir.DIRECTORY_SEPARATOR    
         . $dbName.DIRECTORY_SEPARATOR    
      . $tableName.DIRECTORY_SEPARATOR    
         . $tableName.'.sql';    
!is_writeable ($tableBakFile) && die("$tableBakFile not exists or unwirteable");    
//step3:start restore    
$this->_TableBak->restoreTable($tableName,$tableBakFile);    
}    
function _restoreSomeTalbe($dbName,$TableNameList)    
{    
//step1:检查是否存在数据表:    
foreach ($TableNameList as $tableName){    
!in_array($tableName,$this->_getTableList($dbName)) && die("指定的表名$tableName在数据库中不存在");    
}    
//step2:检查是否存在数据表备份文件 以及是否可写:    
foreach ($TableNameList as $tableName){    
$tableBakFile = $this->_dataDir.DIRECTORY_SEPARATOR    
               . $dbName.DIRECTORY_SEPARATOR    
               . $tableName.DIRECTORY_SEPARATOR    
               . $tableName.'.sql';    
!is_writeable ($tableBakFile) && die("$tableBakFile not exists or unwirteable");    
}    
//step3:start restore:    
foreach ($TableNameList as $tableName){    
$tableBakFile = $this->_dataDir.DIRECTORY_SEPARATOR    
               . $dbName.DIRECTORY_SEPARATOR    
               . $tableName.DIRECTORY_SEPARATOR    
               . $tableName.'.sql';    
$this->_TableBak->restoreTable($tableName,$tableBakFile);    
}    
}    
}    
?>     

复制代码 代码如下:


//Only DbBak can call this class
class TableBak{
var $_mysql_link_id;
var $_dbDir;
//private $_DbManager;
function TableBak($mysql_link_id,$dbDir)
{
$ this->_mysql_link_id = $mysql_link_id;
$this->_dbDir = $dbDir;
}
function backupTable($tableName)
{
//step1: Create the backup directory name of the table:​​
$tableDir = $this- >_dbDir.DIRECTORY_SEPARATOR.$tableName;
!is_dir($tableDir) && mkdir($tableDir);
//step2: Start backup:
$this->_backupTable($tableName,$tableDir);
}
function Restoretable ($ tablename, $ tablebakfile) {
Set_time_limit (0);
$ FileArray = @file ($ tablebakfile) or DIE ("Can Open File $ TableBakfile"); $ Num = Count ($ FileArray);
Mysql_unbuffered_qury ("DELETE FROM $tableName");
$sql = $fileArray[0];
for ($i=1;$i<$num-1;$i++){
mysql_unbuffered_query($sql.$fileArray[$i ]) or (die (mysql_error()));
}
return true;
}
function _getFieldInfo($tableName){
$fieldInfo = array();
$sql="SELECT * FROM $tableName LIMIT 1";
$result = mysql_query($sql,$this->_mysql_link_id);
$num_field=mysql_num_fields($result);
for($i=0;$i<$num_field;$i++){
$field_name= mysql_field_name ($result,$i);
$field_type=mysql_field_type($result,$i);
$fieldInfo[$field_name] = $field_type;
}
mysql_free_result($result);
return $fieldInfo;
}
function _quoteRow($fieldInfo,$row){                                                                                                                                                                                   ; :        $row[$field_name] = "'".mysql_escape_string($field_value)."'";break;                                                                                       $row[$field_name]                                           ;break;         
case "date":      $row[$field_name] = "'".mysql_escape_string($field_value)."'";break; ($field_value)."'";break;              
case "time":       $row[$field_name] = "  ".mysql_escape_string($field_value)."'";break; field_name] = "'".mysql_escape_string($field_value)."'"; break; field_name] = intval($field_value); break;
case "timestamp":$row[$field_name] = intval($field_value); break;
default: $row[$field_name] = intval($field_value); break;
}
}
return $row;
}
function _backupTable($tableName,$tableDir)
{
//Get the field type of the table:
$fieldInfo = $this->_getFieldInfo($tableName);
//step1:Construct INSERT The first half of the statement and written to the file:
$fields = array_keys($fieldInfo);
$fields = implode(',',$fields);
$sqltext="INSERT INTO $tableName($fields)VALUES rn";
$datafile = $tableDir.DIRECTORY_SEPARATOR.$tableName.'.sql';
(!$handle = fopen($datafile,'w')) && die("can not open file $datafile ");
(!fwrite($handle, $sqltext)) && die("can not write data to file $datafile");
fclose($handle);
//step2: Get the data and write it to the file:
//Get the table resources:
set_time_limit(0);
$sql = "select * from $tableName";
$result = mysql_query($sql,$this->_mysql_link_id);​
//Open the data backup file: $tableName.xml
$datafile = $tableDir.DIRECTORY_SEPARATOR.$tableName.'.sql';
(!$handle = fopen($datafile,'a')) && die("can not open file $datafile");
//Get table records one by one and write them into the file:
while ($row = mysql_fetch_assoc($result)) {
$row = $this->_quoteRow ($fieldInfo,$row);                                                                                                                    $record='('                                                                       $record=' write data to file $datafile");
}
mysql_free_result($result);
//Close the file:
fclose($handle);
return true;
}
}
?> ;


Backup mybbs database:

SQL code
//example 1 backup:
require_once('DbBak.php');
require_once('TableBak.php');
$connectid = mysql_connect('localhost' ,'root ','123456');
$backupDir = 'data';
$DbBak = new DbBak($connectid,$backupDir);
$DbBak->backupDb('mybbs');
Restore mybbs database:


Copy the code The code is as follows:

require_once('DbBak.php');

require_once('TableBak.php');
$backupDir = 'data';
$DbBak = new DbBak($connectid,$backupDir);
$DbBak->restoreDb('mybbs');


The above introduces the postgresql database PHP implementation of the mysql database backup class, including the content of the postgresql database. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template