php postgresql mysql_postgresql数据库php实现mysql数据库备份类

1、实例化DbBak需要告诉它两件事:数据服务器在哪里($connectid)、备份到哪个目录($backupDir):

require_once('DbBak.php');

require_once('TableBak.php');

$connectid = mysql_connect('localhost','root','123456');

$backupDir = 'data';

$DbBak = new DbBak($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',array('board','face','friendsite'));

PHP代码

$DbBak->restoreDb('mybbs','board');

PHP代码

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:选择数据库:

mysql_select_db($dbName);

//step2:创建数据库备份目录

$dbDir = $this->_dataDir.DIRECTORY_SEPARATOR.$dbName;

!is_dir($dbDir) && mkdir($dbDir);

//step3:得到数据库所有表名 并开始备份表

$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);

}

}

}

?>

代码如下:

$datafile");

(!fwrite($handle, $sqltext)) && die("can not write data to file $datafile");

fclose($handle);

//step2:取得数据 并写入文件:

//取出表资源:

set_time_limit(0);

$sql = "select * from $tableName";

$result = mysql_query($sql,$this->_mysql_link_id);

//打开数据备份文件:$tableName.xml

$datafile = $tableDir.DIRECTORY_SEPARATOR.$tableName.'.sql';

(!$handle = fopen($datafile,'a')) && die("can not open file $datafile");

//逐条取得表记录并写入文件:

while ($row = mysql_fetch_assoc($result)) {

$row = $this->_quoteRow($fieldInfo,$row);

$record='(' . implode(',',$row) . ");\r\n";

(!fwrite($handle, $record)) && die("can not write data to file $datafile");

}

mysql_free_result($result);

//关闭文件:

fclose($handle);

return true;

}

}

?>

备份mybbs数据库:

SQL代码

//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');

恢复mybbs数据库:

代码如下:

require_once('DbBak.php');

require_once('TableBak.php');

$connectid = mysql_connect('localhost','root','123456');

$backupDir = 'data';

$DbBak = new DbBak($connectid,$backupDir);

$DbBak->restoreDb('mybbs');

以上就介绍了postgresql数据库 php实现mysql数据库备份类,包括了postgresql数据库方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

本条技术文章来源于互联网,如果无意侵犯您的权益请点击此处反馈版权投诉

本文系统来源:php中文网

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值