欢迎您访问我爱IT技术网,今天小编为你分享的编程技术是:【一个数据库备份类】,下面是详细的分享!
一个数据库备份类
php代码:
<?php//包含Mysql数据库操作文件
//
require_once("MysqlDB.php");
class DBManagement implements IDBManagement
{
//
//当前数据库中所有的数据表的名字
//
private $TablesName;
//
//默认路径
//
private $DefaultPath;
//
//当前要操作的数据库名
//
private $DatabaseName;
//
//操作数据库的对象
//
private $db;
function __construct($_DatabaseName="",$_DefaultPath="")//
{
require("config.inc.php");
if(!$_DatabaseName) {$this->DatabaseName=$dbName;}
else {$this->DatabaseName=$_DatabaseName;}
if(!$_DefaultPath) {$this->DefaultPath=$defaultPath;}
else {$this->DefaultPath=$_DefaultPath;}
$path=realpath($this->DefaultPath);
$this->DefaultPath=str_replace("\\","/",$path);
//$this->db=new DBFactory();
$this->db=new MysqlDB();
}
protected function GetTablesName()
{
$result=$this->db->Query("show table status");
while($Row=$this->db->NextRecord($result))
{
$this->TablesName[]=$Row["Name"];
}
return $this->TablesName;
}
protected function GetDataFileName()
{
$this->GetTablesName();
$count=count($this->GetTablesName());
for ($i=0;$i<$count;$i++)
{
$DataFilesName[]=$this->DefaultPath."/".$this->TablesName[$i].".txt";
//echo $DataFilesName[$i];
}
return $DataFilesName;
}
protected function SaveTableStructure($text)
{
$fileName=$this->DefaultPath."/Install.sql";
//if(file_exists($fileName))
//{
// unlink($fileName);
//}
//echo $text;
$fp=fopen($fileName,"w+");
fwrite($fp,$text);
}
protected function BackupTableStructure()
{
$i=0;
$sqlText="";
$this->GetTablesName();
$count=count($this->TablesName);
//
//取出所有数据表的结构
//
while($i<$count)
{
$tableName=$this->TablesName[$i];
$result=$this->db->Query("show create table $tableName");
$this->db->NextRecord($result);
//
//取出成生表的SQL语句
//
$sqlText.="DROP TABLE IF EXISTS `".$tableName."`; ";//`
$sqlText.=$this->db->GetField(1)."; ";
$i++;
}
$sqlText=str_replace("\r","",$sqlText);
$sqlText=str_replace("\n","",$sqlText);
//$sqlText=str_replace("'","`",$sqlText);
$this->SaveTableStructure($sqlText);
}
protected function RestoreTableStructure()
{
$fileName=$this->DefaultPath."/Install.sql";
if(!file_exists($fileName)){echo "找不到表结构文件,请确认你以前做过备份!";exit;}
$fp=fopen($fileName,"r");
$sqlText=fread($fp,filesize($fileName));
//$sqlText=str_replace("\r","",$sqlText);
//$sqlText=str_replace("\n","",$sqlText);
$sqlArray=explode("; ",$sqlText);
try
{
$count=count($sqlArray);
//
//数组的最后一个为";",是一个无用的语句,
//
for($i=1;$i<$count;$i++)
{
$sql=$sqlArray[$i-1].";";
$result=$this->db->ExecuteSQL($sql);
if(!mysql_errno())
{
if($i%2==0){echo "数据表".($i/2)."的结构恢复成功!\n";}
}
else
{
if($i%2==0)
{
echo "数据表".($i/2)."的结构恢复失败!\n";
exit();
}
}
}
}
catch(Exception $e)
{
$this->db->ShowError($e->getMessage());
$this->db->Close();
return false;
}
}
function ImportData()
{
$DataFilesName=$this->GetDataFileName();
$count=count($this->TablesName);
//$this->db->ExecuteSQL("set character set gbk");
for ($i=0;$i<$count;$i++)
{
//$DataFilesName[$i]=str_replace("\\","/",$DataFilesName[$i])
//echo $DataFilesName[$i];
$sqlLoadData="load data infile '".$DataFilesName[$i]."' into table ".$this->TablesName[$i];
$sqlCleanData="delete from ".$this->TablesName[$i];
//echo $sql."\n";
try
{
//$this->db->ExecuteSQL("set character set utf8");
//$this->db->ExecuteSQL("SET NAMES 'utf8'");
$this->db->ExecuteSQL($sqlCleanData);
$this->db->ExecuteSQL($sqlLoadData);
return true;
//echo "数据导入成功!";
}
catch (Exception $e)
{
$this->db->ShowError($e->getMessage());
return false;
exit();
}
}
}
function ExportData()
{
$DataFilesName=$this->GetDataFileName();
$count=count($this->TablesName);
try
{
for ($i=0;$i<$count;$i++)
{
$sql="select * from ".$this->TablesName[$i]." into outfile '".$DataFilesName[$i]."'";
if(file_exists($DataFilesName[$i]))
{
unlink($DataFilesName[$i]);
}
//$this->db->ExecuteSQL("SET NAMES 'utf8'");
$this->db->ExecuteSQL($sql);
}
return true;
//echo "数据导出成功!";
}
catch (Exception $e)
{
$this->db->ShowError($e->getMessage());
return false;
exit();
}
}
function BackupDatabase()
{
try
{
$this->BackupTableStructure();
$this->ExportData();
return true;
}
catch (Exception $e)
{
$this->db->ShowError($e->getMessage());
return false;
exit();
}
}
function RestoreDatabase()
{
try
{
$this->RestoreTableStructure();
$this->ImportData();
return true;
}
catch (Exception $e)
{
$this->db->ShowError($e->getMessage());
return false;
exit();
}
}
}
?>
以上所分享的是关于一个数据库备份类,下面是编辑为你推荐的有价值的用户互动:
相关问题:日常使用的数据库备份类型有哪些?
答:1、完全备份这是大多数人常用的方式,它可以备份整个数据库,包含用户表、系统表、索引、视图和存储过程等所有数据库对象。但它需要花费更多的时间和空间,所以,一般推荐一周做一次完全备份。 2、事务日志备份事务日志是一个单独的文件,它记录... >>详细
相关问题:sql server 2000数据库中数据的备份有几种类型?有...
答:备份这一块比较复杂,具体可以看联机丛书 SQL Server 支持的备份类型包括: 1.完整备份,该操作将备份包括部分事务日志在内的整个数据库(这样完整备份便可恢复)。 有关详细信息,请参阅完整备份。 2.在完整数据库备份之间执行的完整差异备份。... >>详细
相关问题:数据库怎么建立备份库?一个数据库最多可以建几个...
答:首先,你所用的数据库是什么数据库?备份库可以建立N多,比如oracle数据库,你可以用exp命令整库导出成dmp文件。如果你是acces数据库,直接复制文件另存到别的地方就是备份了。 >>详细
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
