1, instantiation Dbbak need to tell it two things: where the data server ($connectid), back to which directory ($backupDir):
Require_once (' dbbak.php ');
Require_once (' tablebak.php ');
$connectid = mysql_connect (' localhost ', ' root ', ' 123456 ');
$backupDir = ' data ';
$DbBak = new Dbbak ($connectid, $backupDir);
2, then you can start backing up the database, you can not only specify the backup of the database, but also in detail to set up only those tables:
2.1 If you want to back up all the tables in the Mybbs library, just like this:
$DbBak->backupdb (' Mybbs ');
2.2 If you only want to back up the board, face, and friendlist tables in the Mybbs library, you can specify them with a one-dimensional array:
$DbBak->backupdb (' Mybbs ', Array (' board ', ' face ', ' friendsite '));
2.3 If you want to back up only one table, such as the Board table:
$DbBak->backupdb (' Mybbs ', ' board ');
3, Data recovery:
For 2.1, 2.1, 2.3 three cases, as long as the corresponding modification of the statement, the backupdb replaced with RESTOREDB can achieve data recovery:
$DbBak->restoredb (' Mybbs ');
SQL code
$DbBak->restoredb (' Mybbs ', Array (' board ', ' face ', ' friendsite '));
PHP code
$DbBak->restoredb (' Mybbs ', ' board ');
PHP code
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 was 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 is a string value ');
Step1: Select database:
mysql_select_db ($dbName);
Step2: Creating a Database backup directory
$dbDir = $this->_datadir.directory_separator. $dbName;
!is_dir ($dbDir) && mkdir ($dbDir);
Step3: Get all the database table names and start backing up the table
$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 is a string value ');
Step1: Check for the existence of the database and connect:
@mysql_select_db ($dbName) | | Die ("the database
$dbNameDose NOT EXISTS ");
Step2: Check for database backup directory
$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 ("specified table name
$tableNameDoes not exist in the database ");
$this->_tablebak->backuptable ($tableName);
}
function _backupsometalbe ($dbName, $TableNameList)
{
foreach ($TableNameList as $tableName) {
!in_array ($tableName, $this->_gettablelist ($dbName)) && die ("specified table name
$tableNameDoes not exist in the database ");
}
foreach ($TableNameList as $tableName) {
$this->_tablebak->backuptable ($tableName);
}
}
function _restorealltable ($dbName)
{
Step1: Checks if there are any backup files for all data tables and is writable:
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: Checks for the presence of a data table:
!in_array ($tableName, $this->_gettablelist ($dbName)) && die ("specified table name
$tableNameDoes not exist in the database ");
Step2: Check for the existence of a data table backup file and whether it is writable:
$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: Checks for the presence of a data table:
foreach ($TableNameList as $tableName) {
!in_array ($tableName, $this->_gettablelist ($dbName)) && die ("specified table name
$tableNameDoes not exist in the database ");
}
Step2: Check for the existence of a data table backup file and whether it is writable:
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);
}
}
}
?>
Copy CodeThe code is as follows:
Only Dbbak can call this class
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: Create a backup directory name for the table:
$tableDir = $this->_dbdir.directory_separator. $tableName;
!is_dir ($tableDir) && mkdir ($tableDir);
Step2: Start Backup:
$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)
{
Gets the field type of the table:
$fieldInfo = $this->_getfieldinfo ($tableName);
Step1: Constructs the first half of the INSERT statement and writes the file:
$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
$datafile");
(!fwrite ($handle, $sqltext)) && die ("Can not write data to file
$datafile");
Fclose ($handle);
Step2: Get data and write to file:
To remove a table resource:
Set_time_limit (0);
$sql = "SELECT * from $tableName";
$result = mysql_query ($sql, $this->_mysql_link_id);
Open Data backup file: $tableName. xml
$datafile = $tableDir. Directory_separator. $tableName. SQL ';
(! $handle = fopen ($datafile, ' a ')) && die (the "can not open file
$datafile");
Record the table and write the file:
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);
To close a file:
Fclose ($handle);
return true;
}
}
?>
To back up the Mybbs database:
SQL code
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 ');
Restore the Mybbs database:
Copy CodeThe code is as follows:
Require_once (' dbbak.php ');
Require_once (' tablebak.php ');
$connectid = mysql_connect (' localhost ', ' root ', ' 123456 ');
$backupDir = ' data ';
$DbBak = new Dbbak ($connectid, $backupDir);
$DbBak->restoredb (' Mybbs ');
http://www.bkjia.com/PHPjc/318669.html www.bkjia.com true http://www.bkjia.com/PHPjc/318669.html techarticle 1, instantiation Dbbak need to tell it two things: where the data server ($connectid), back to which directory ($backupDir): require_once (' dbbak.php '); Require_once (' Tablebak.php '); $co ...