php实现mysql数据库备份类
程序员文章站
2022-06-03 13:25:35
1、实例化dbbak需要告诉它两件事:数据服务器在哪里($connectid)、备份到哪个目录($backupdir): require_once('dbbak....
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 <b>$dbname</b> 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("指定的表名<b>$tablename</b>在数据库中不存在");
$this->_tablebak->backuptable($tablename);
}
function _backupsometalbe($dbname,$tablenamelist)
{
foreach ($tablenamelist as $tablename){
!in_array($tablename,$this->_gettablelist($dbname)) && die("指定的表名<b>$tablename</b>在数据库中不存在");
}
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("指定的表名<b>$tablename</b>在数据库中不存在");
//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("指定的表名<b>$tablename</b>在数据库中不存在");
}
//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);
}
}
}
?>
<?php
//只有dbbak才能调用这个类
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:创建表的备份目录名:
$tabledir = $this->_dbdir.directory_separator.$tablename;
!is_dir($tabledir) && mkdir($tabledir);
//step2:开始备份:
$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_query("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){
foreach ($row as $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="insert into $tablename($fields)values \r\n";
$datafile = $tabledir.directory_separator.$tablename.'.sql';
(!$handle = fopen($datafile,'w')) && die("can not open file <b>$datafile</b>");
(!fwrite($handle, $sqltext)) && die("can not write data to file <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("can not open file <b>$datafile</b>");
//逐条取得表记录并写入文件:
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 <b>$datafile</b>");
}
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');
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 <b>$dbname</b> 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("指定的表名<b>$tablename</b>在数据库中不存在");
$this->_tablebak->backuptable($tablename);
}
function _backupsometalbe($dbname,$tablenamelist)
{
foreach ($tablenamelist as $tablename){
!in_array($tablename,$this->_gettablelist($dbname)) && die("指定的表名<b>$tablename</b>在数据库中不存在");
}
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("指定的表名<b>$tablename</b>在数据库中不存在");
//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("指定的表名<b>$tablename</b>在数据库中不存在");
}
//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);
}
}
}
?>
复制代码 代码如下:
<?php
//只有dbbak才能调用这个类
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:创建表的备份目录名:
$tabledir = $this->_dbdir.directory_separator.$tablename;
!is_dir($tabledir) && mkdir($tabledir);
//step2:开始备份:
$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_query("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){
foreach ($row as $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="insert into $tablename($fields)values \r\n";
$datafile = $tabledir.directory_separator.$tablename.'.sql';
(!$handle = fopen($datafile,'w')) && die("can not open file <b>$datafile</b>");
(!fwrite($handle, $sqltext)) && die("can not write data to file <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("can not open file <b>$datafile</b>");
//逐条取得表记录并写入文件:
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 <b>$datafile</b>");
}
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');