1、实例化DbBak需要告诉它两件事:数据服务器在哪里($connectid)、备份到哪个目录($backupDir):require_once('DbBak.PHP');
require_once('TableBak.PHP');
$connectid=MysqL_connect('localhost','root','123456');
$backupDir='data';
$DbBak=newDbBak($connectid,$backupDir); 2、然后就可以开始备份数据库了,你不仅能够指定备份那个数据库,而且能详细设置只备份那几个表:
2.1如果你想备份mybbs库中的所有表,只要这样: $DbBak->backupDb('mybbs'); 2.2如果你只想备份mybbs库中的board、face、friendlist表,可以用一个一维数组指定: $DbBak->backupDb('mybbs',array('board','face','friendsite')); 2.3如果只想备份一个表,比如board表:
$DbBak->backupDb('mybbs','board');
3,数据恢复:
对于2.1、2.1、2.3三种情况,只要相应的修改下语句,把backupDb换成restoreDb就能实现数据恢复了: $DbBak->restoreDb('mybbs');
sql代码
$DbBak->restoreDb('mybbs','friendsite'));
PHP代码
$DbBak->restoreDb('mybbs','board');
PHP代码
require_once('TableBak.PHP');
classDbBak{
var$_MysqL_link_id;
var$_dataDir;
var$_tableList;
var$TableBak; functionDbBak($MysqL_link_id,$dataDir)
{
((!is_string($dataDir))||strlen($dataDir)==0)&&die('error:$datadirisnotastring');
!is_dir($dataDir)&&mkdir($dataDir);
$this->dataDir=$dataDir;
$this->MysqL_linkid=$MysqL_link_id;
} functionbackupDb($dbName,$tableName=null)
{
((!is_string($dbName))||strlen($dbName)==0)&&die('$dbNamemustbeastringvalue');
//step1:选择数据库:
MysqL_select_db($dbName);
//step2:创建数据库备份目录
$dbDir=$this->_dataDir.DIRECTORY_SEPARATOR.$dbName;
!is_dir($dbDir)&&mkdir($dbDir);
//step3:得到数据库所有表名并开始备份表
$this->TableBak=newTableBak($this->MysqL_link_id,$dbDir);
if(is_null($tableName)){//backupalltableinthedb
$this->_backupAllTable($dbName);
return;
}
if(is_string($tableName)){
(strlen($tableName)==0)&&die('....');
$this->_backupOneTable($dbName,$tableName);
return;
}
if(is_array($tableName)){
foreach($tableNameas$table){
((!is_string($table))||strlen($table)==0)&&die('....');
}
$this->_backupSoMetalbe($dbName,$tableName);
return;
}
} functionrestoreDb($dbName,$tableName=null){
((!is_string($dbName))||strlen($dbName)==0)&&die('$dbNamemustbeastringvalue');
//step1:检查是否存在数据库并连接:
@MysqL_select_db($dbName)||die("thedatabase$dbNamedosenotexists");
//step2:检查是否存在数据库备份目录
$dbDir=$this->_dataDir.DIRECTORY_SEPARATOR.$dbName;
!is_dir($dbDir)&&die("$dbDirnotexists");
//step3:startrestore
$this->TableBak=newTableBak($this->MysqL_link_id,$dbDir);
if(is_null($tableName)){//backupalltableinthedb
$this->_restoreAllTable($dbName);
return;
}
if(is_string($tableName)){
(strlen($tableName)==0)&&die('....');
$this->_restoreOneTable($dbName,$tableName);
return;
}
if(is_array($tableName)){
foreach($tableNameas$table){
((!is_string($table))||strlen($table)==0)&&die('....');
}
$this->_restoreSoMetalbe($dbName,$tableName);
return;
}
} function_getTableList($dbName)
{
$tableList=array();
$result=MysqL_listtables($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($TableNameListas$tableName){
!in_array($tableName,$this->_getTableList($dbName))&&die("指定的表名$tableName在数据库中不存在");
}
foreach($TableNameListas$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("$tableBakFilenotexistsorunwirteable");
}
//step2:startrestore
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("$tableBakFilenotexistsorunwirteable");
//step3:startrestore
$this->_TableBak->restoreTable($tableName,$tableBakFile);
}
function_restoreSoMetalbe($dbName,$TableNameList)
{
//step1:检查是否存在数据表:
foreach($TableNameListas$tableName){
!in_array($tableName,$this->_getTableList($dbName))&&die("指定的表名$tableName在数据库中不存在");
}
//step2:检查是否存在数据表备份文件以及是否可写:
foreach($TableNameListas$tableName){
$tableBakFile=$this->_dataDir.DIRECTORY_SEPARATOR
.$dbName.DIRECTORY_SEPARATOR
.$tableName.DIRECTORY_SEPARATOR
.$tableName.'.sql';
!is_writeable($tableBakFile)&&die("$tableBakFilenotexistsorunwirteable");
}
//step3:startrestore:
foreach($TableNameListas$tableName){
$tableBakFile=$this->_dataDir.DIRECTORY_SEPARATOR
.$dbName.DIRECTORY_SEPARATOR
.$tableName.DIRECTORY_SEPARATOR
.$tableName.'.sql';
$this->_TableBak->restoreTable($tableName,$tableBakFile);
}
}
}
?>
<div class="codetitle"><a style="CURSOR: pointer" data="21427" class="copybut" id="copybut21427" onclick="doCopy('code21427')"> 代码如下: