Ec (2); functionbackup ($ bakfilenull, $ tablesarray () & nbsp; {& nbsp; if (empty ($ bakfile) {& nbsp; $ bakfile $ this-& gt; dbname. date (& quot; Ymdhis & quot ;).. SQL; & nbsp;} elseif (is _ script ec (2); script
Function backup ($ bakfile = null, $ tables = array ())
{
If (empty ($ bakfile )){
$ Bakfile = $ this-> dbname. date ("Ymdhis"). '. SQL ';
} Elseif (is_dir ($ bakfile )){
If (preg_match ('// $/', $ bakfile )){
$ Bakfile = $ bakfile. $ this-> dbname. date ("Ymdhis"). '. SQL ';
} Else {
$ Bakfile = $ bakfile. '/'. $ this-> dbname. date ("Ymdhis"). '. SQL ';
}
}
If (! $ Tables ){
$ This-> query ("show tables ");
While ($ row = mysql_fetch_row ($ this-> queryID )){
$ Tables [] = $ row [0];
}
} Else {
Foreach ($ tables as $ k => $ v ){
$ Tables [$ k] = $ this-> tablePrefix. $ v;
}
}
If ($ fp = fopen ($ bakfile, 'wb ')){
If ($ this-> dbcharset ){
Fwrite ($ fp, "set names". $ this-> dbcharset. "; nn ");
}
Foreach ($ tables as $ table ){
$ This-> dumpTable ($ table, $ fp );
Fwrite ($ fp, "n ");
} // Foreach
Fclose ($ fp );
Return true;
} Else {
Return false;
} // If
}
// Export the table using a private Method
Function dumpTable ($ fullTableName, $ fp)
{
// Backup table structure
Fwrite ($ fp, "-- n -- {$ fullTableName} n -- n ");
$ Row = $ this-> findBySql ("show create table '{$ fullTableName }"');
Fwrite ($ fp, $ row ['create table']. "; nn ");
// Back up table store data
$ This-> query ("SELECT * FROM '{$ fullTableName }"');
While ($ row = mysql_fetch_assoc ($ this-> queryID )){
Foreach ($ row as $ k => $ v ){
$ Row [$ k] = "'". $ this-> qstr ($ v )."'";
}
$ SQL = "INSERT INTO '$ fullTableName' VALUES (". join (",", $ row). "); n ";
Fwrite ($ fp, $ SQL );
}
Mysql_free_result ($ this-> queryID );
Fwrite ($ fp, "n ");
}
// Restore database files
Function restore ($ bakfile)
{
If ($ fp = fopen ($ bakfile, 'R ')){
$ SQL = '';
While (! Feof ($ fp )){
$ Line = fgets ($ fp );
If (strpos ($ line ,'--')! = 0)
{
$ SQL. = $ line;
// Pp ($ SQL );
}
If (preg_match ('/; s * $/', $ SQL )){
$ This-> query ($ SQL );
$ SQL = '';
}
}
Fclose ($ fp );
Return true;
} Else {
Return false;
}
}