时间:2021-05-26
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',array('board','face','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_link_id=$_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<b>$dbName</b>dosenotexists");
//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_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("指定的表名<b>$tableName</b>在数据库中不存在");
$this->_TableBak->backupTable($tableName);
}
function_backupSomeTalbe($dbName,$TableNameList)
{
foreach($TableNameListas$tableName){
!in_array($tableName,$this->_getTableList($dbName))&&die("指定的表名<b>$tableName</b>在数据库中不存在");
}
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("指定的表名<b>$tableName</b>在数据库中不存在");
//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("指定的表名<b>$tableName</b>在数据库中不存在");
}
//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);
}
}
}
?>
复制代码 代码如下:
<?php
//只有DbBak才能调用这个类
classTableBak{
var$_mysql_link_id;
var$_dbDir;
//private$_DbManager;
functionTableBak($mysql_link_id,$dbDir)
{
$this->_mysql_link_id=$mysql_link_id;
$this->_dbDir=$dbDir;
}
functionbackupTable($tableName)
{
//step1:创建表的备份目录名:
$tableDir=$this->_dbDir.DIRECTORY_SEPARATOR.$tableName;
!is_dir($tableDir)&&mkdir($tableDir);
//step2:开始备份:
$this->_backupTable($tableName,$tableDir);
}
functionrestoreTable($tableName,$tableBakFile)
{
set_time_limit(0);
$fileArray=@file($tableBakFile)ordie("canopenfile$tableBakFile");
$num=count($fileArray);
mysql_unbuffered_query("DELETEFROM$tableName");
$sql=$fileArray[0];
for($i=1;$i<$num-1;$i++){
mysql_unbuffered_query($sql.$fileArray[$i])or(die(mysql_error()));
}
returntrue;
}
function_getFieldInfo($tableName){
$fieldInfo=array();
$sql="SELECT*FROM$tableNameLIMIT1";
$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){
foreach($rowas$field_name=>$field_value){
$field_value=strval($field_value);
switch($fieldInfo[$field_name]){
case"blob":$row[$field_name]="'".mysql_escape_string($field_value)."'";break;
case"string":$row[$field_name]="'".mysql_escape_string($field_value)."'";break;
case"date":$row[$field_name]="'".mysql_escape_string($field_value)."'";break;
case"datetime":$row[$field_name]="'".mysql_escape_string($field_value)."'";break;
case"time":$row[$field_name]="'".mysql_escape_string($field_value)."'";break;
case"unknown":$row[$field_name]="'".mysql_escape_string($field_value)."'";break;
case"int":$row[$field_name]=intval($field_value);break;
case"real":$row[$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)
{
//取得表的字段类型:
$fieldInfo=$this->_getFieldInfo($tableName);
//step1:构造INSERT语句前半部分并写入文件:
$fields=array_keys($fieldInfo);
$fields=implode(',',$fields);
$sqltext="INSERTINTO$tableName($fields)VALUES\r\n";
$datafile=$tableDir.DIRECTORY_SEPARATOR.$tableName.'.sql';
(!$handle=fopen($datafile,'w'))&&die("cannotopenfile<b>$datafile</b>");
(!fwrite($handle,$sqltext))&&die("cannotwritedatatofile<b>$datafile</b>");
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("cannotopenfile<b>$datafile</b>");
//逐条取得表记录并写入文件:
while($row=mysql_fetch_assoc($result)){
$row=$this->_quoteRow($fieldInfo,$row);
$record='('.implode(',',$row).");\r\n";
(!fwrite($handle,$record))&&die("cannotwritedatatofile<b>$datafile</b>");
}
mysql_free_result($result);
//关闭文件:
fclose($handle);
returntrue;
}
}
?>
备份mybbs数据库:
SQL代码
//example1backup:
require_once('DbBak.php');
require_once('TableBak.php');
$connectid=mysql_connect('localhost','root','123456');
$backupDir='data';
$DbBak=newDbBak($connectid,$backupDir);
$DbBak->backupDb('mybbs');
恢复mybbs数据库:
复制代码 代码如下:
require_once('DbBak.php');
require_once('TableBak.php');
$connectid=mysql_connect('localhost','root','123456');
$backupDir='data';
$DbBak=newDbBak($connectid,$backupDir);
$DbBak->restoreDb('mybbs');
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了php实现MySQL数据库备份与还原类。分享给大家供大家参考。具体分析如下:这是一个非常简单的利用php来备份mysql数据库的类文件,我们只要简
用PHP实现XML备份Mysql数据库收藏以下是在Linux下通过Apache+PHP对Mysql数据库的备份的文件代码:文件一、Listtable.php(文
用php实现的备份MySQL数据库的代码,需要的朋友可以参考下。
MySQL数据库使用命令行备份|MySQL数据库备份命令例如:数据库地址:127.0.0.1数据库用户名:root数据库密码:root数据库名称:szldb备份
以下是在Linux下通过Apache+PHP对Mysql数据库的备份的文件代码:文件一、Listtable.php(文件列出数据库中的所有表格,供选择备份)请选