There are only two types of backup programs: DbBak and TableBak: This program only backs up and recovers data. It is easy to use and instantiate DbBak, then call the bakupDb and restoreDb methods: 1. to instantiate DbBak, you need to tell it two things: where the data server is ($ connectid) and the directory to which the data server is backed up ($ backupDir ):
Require_once ('dbbak. php ');
Require_once ('tablebak. php ');
$ Connectid = mysql_connect ('localhost', 'root', '123 ');
$ BackupDir = 'data ';
$ DbBak = new DbBak ($ connectid, $ backupDir );
2. Then you can start backing up the database. You can not only specify the database to be backed up, but also set to back up only those tables in detail:
2.1 if you want to back up all the tables in the mybbs database, you just need:
$ DbBak-> backupDb ('mybbs ');
2.2 If you only want to back up the board, face, and friendlist tables in the mybbs library, you can use a one-dimensional array to specify:
$ DbBak-> backupDb ('mybbs ', array ('board', 'face', 'dsdsite '));
2.3 if you only want to back up a table, such as the board table:
$ DbBak-> backupDb ('mybbs ', 'board ');
3. data recovery:
In three cases: 2.1, 2.1, and 2.3, you only need to modify the following statement and replace backupDb with restoreDb to recover the data:
$ DbBak-> restoreDb ('mybbs ');
SQL code
$ DbBak-> restoreDb ('mybbs ', array ('board', 'face', 'dsdsite '));
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 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 ');
// Step 1: select the database:
Mysql_select_db ($ dbName );
// Step 2: Create a database backup directory
$ DbDir = $ this-> _ dataDir. DIRECTORY_SEPARATOR. $ dbName;
! Is_dir ($ dbDir) & mkdir ($ dbDir );
// Step 3: get the names of all tables in the database and start the backup 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 be a string value ');
// Step 1: Check whether a database exists and connect to it:
@ Mysql_select_db ($ dbName) | die ("the database
$ DbNameDose not exists ");
// Step 2: Check whether the Database Backup directory exists
$ DbDir = $ this-> _ dataDir. DIRECTORY_SEPARATOR. $ dbName;
! Is_dir ($ dbDir) & die ("$ dbDir not exists ");
// Step 3: 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
$ TableName") Does 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
$ TableName") Does not exist in the database ");
}
Foreach ($ TableNameList as $ tableName ){
$ This-> _ TableBak-> backupTable ($ tableName );
}
}
Function _ restoreAllTable ($ dbName)
{
// Step 1: Check whether all data table backup files exist and whether they can be written:
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 ");
}
// Step 2: 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)
{
// Step 1: Check whether a data table exists:
! In_array ($ tableName, $ this-> _ getTableList ($ dbName) & die ("specified table name
$ TableName") Does not exist in the database ");
// Step 2: Check whether the data table backup file exists 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 ");
// Step 3: start restore
$ This-> _ TableBak-> restoreTable ($ tableName, $ tableBakFile );
}
Function _ restoreSomeTalbe ($ dbName, $ TableNameList)
{
// Step 1: Check whether a data table exists:
Foreach ($ TableNameList as $ tableName ){
! In_array ($ tableName, $ this-> _ getTableList ($ dbName) & die ("specified table name
$ TableName") Does not exist in the database ");
}
// Step 2: Check whether the data table backup file exists 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 ");
}
// Step 3: start restore:
Foreach ($ TableNameList as $ tableName ){
$ TableBakFile = $ this-> _ dataDir. DIRECTORY_SEPARATOR
. $ DbName. DIRECTORY_SEPARATOR
. $ TableName. DIRECTORY_SEPARATOR
. $ TableName. '. SQL ';
$ This-> _ TableBak-> restoreTable ($ tableName, $ tableBakFile );
}
}
}
?>
The 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)
{
// Step 1: name of the Backup Directory for creating the table:
$ TableDir = $ this-> _ dbDir. DIRECTORY_SEPARATOR. $ tableName;
! Is_dir ($ tableDir) & mkdir ($ tableDir );
// Step 2: 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)
{
// Obtain the table field type:
$ FieldInfo = $ this-> _ getFieldInfo ($ tableName );
// Step 1: Construct the First Half of the INSERT statement and write it to 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 );
// Step 2: Get the data and write it to the file:
// Retrieve table resources:
Set_time_limit (0 );
$ SQL = "select * from $ tableName ";
$ Result = mysql_query ($ SQL, $ this-> _ mysql_link_id );
// Open the data backup file: $ tableName. xml
$ Datafile = $ tableDir. DIRECTORY_SEPARATOR. $ tableName. '. SQL ';
(! $ Handle = fopen ($ datafile, 'A') & die ("can not open file$ Datafile");
// Obtain table records one by one and write them into files:
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 );
// Close the file:
Fclose ($ handle );
Return true;
}
}
?>
Back up the mybbs database:
SQL code
// Example 1 backup:
Require_once ('dbbak. php ');
Require_once ('tablebak. php ');
$ Connectid = mysql_connect ('localhost', 'root', '123 ');
$ BackupDir = 'data ';
$ DbBak = new DbBak ($ connectid, $ backupDir );
$ DbBak-> backupDb ('mybbs ');
Restore the mybbs database:
The Code is as follows:
Require_once ('dbbak. php ');
Require_once ('tablebak. php ');
$ Connectid = mysql_connect ('localhost', 'root', '123 ');
$ BackupDir = 'data ';
$ DbBak = new DbBak ($ connectid, $ backupDir );
$ DbBak-> restoreDb ('mybbs ');