PHP MySQL Database Backup program/* Provides an instance of the PHP MySQL database backup program, a good way to your database online real-time backup, so as to save the database security, And he is the database backup file that was saved in the Bakdata directory on the date of the. sql file.
PHP Tutorial MySQL Tutorial database Tutorial Backup Program
/*
Provide an example of the PHP MySQL database backup program, a good way to make a real-time online backup of your database, so that the security of the database can be saved, and he is the. sql file saved in the Bakdata directory, the date of the database backup file generated.
*/
?>
Data management
Esweb System Data Management
- Data backup
- Data restore
- Data optimization
/*--------------Interface--------------*/if (!$_post[' act ') {/*----------------------*/
$MSGS []= "Server backup directory is $backup";
$MSGS []=] for larger data tables, it is strongly recommended to use a sub-volume backup ";
$MSGS []=] only choose to backup to the server in order to use the sub-volume backup function ";
Show_msg ($MSGS);
?>
If you need to back up, restore, or optimize large amounts of data, it is recommended to use "Empire Backup King"
/*----*/else{/*--------------Main program-----------------------------------------*/
if ($_post[' Weizhi ']== "LOCALPC" &&$_post[' Fenjuan ']== ' yes ')
{$msgs []=] Only select Backup to server to use the sub-volume backup function ";
Show_msg ($MSGS); Pageend ();}
if ($_post[' Fenjuan ']== "yes" &&!$_post[' filesize '])
{$MSGS []=] You have selected the sub-volume backup function, but did not fill in the volume file size ";
Show_msg ($MSGS); Pageend ();}
if ($_post[' Weizhi ']== "Server" &&!writeable ($backup))
{$MSGS []= "Backup file directory ' $backup ' not writable, please modify directory Properties";
Show_msg ($MSGS); Pageend ();}
/*----------back up all Tables-------------*/if ($_post[' Bfzl ']== "Quanbubiao") {/*----*/
/*----Roll */if (!$_post[' Fenjuan ') {/*--------------------------------*/
if (! $tables = $d->query ("Show Table status from $mysqldb")
{$MSGS []= "Read database structure error"; show_msg ($MSGS); Pageend ();}
$sql = "";
while ($d->nextrecord ($tables))
{
$table = $d->f ("name");
$sql. =make_header ($table);
$d->query ("SELECT * from $table");
$num _fields= $d->nf ();
while ($d->nextrecord ())
{$sql. =make_record ($table, $num _fields);}
}
$filename =date ("Ymd", Time ()). " _all.sql ";
if ($_post[' Weizhi ']== "LOCALPC") down_file ($sql, $filename);
ElseIf ($_post[' Weizhi ']== "server")
{if (Write_file ($sql, $filename))
$MSGS []=] All data table data backup complete, generate backup file ' $backup/$filename ' ";
else $msgs []=] failed to back up all data tables ";
Show_msg ($MSGS);
Pageend ();
}
/*-----------------do not roll over */}/*-----------------------*/
/*-----------------Roll */else{/*-------------------------*/
if (!$_post[' filesize ')
{$msgs []=] Please fill in the size of the backup file, Show_msg ($msgs);p ageend ();}
if (! $tables = $d->query ("Show Table status from $mysqldb")
{$MSGS []= "Read database structure error"; show_msg ($MSGS); Pageend ();}
$sql = ""; $p = 1;
$filename =date ("Ymd", Time ()). " _all ";
while ($d->nextrecord ($tables))
{
$table = $d->f ("name");
$sql. =make_header ($table);
$d->query ("SELECT * from $table");
$num _fields= $d->nf ();
while ($d->nextrecord ())
{$sql. =make_record ($table, $num _fields);
if (strlen ($sql) >=$_post[' filesize ']*1000) {
$filename. = ("_v". $p. ". SQL ");
if (Write_file ($sql, $filename))
$MSGS []=] All data Sheets-volumes-". $p." -Data backup complete, generate backup file ' $backup/$filename ' ";
else $msgs []= "Backup Table-". $_post[' tablename '). " -Failure ";
$p + +;
$filename =date ("Ymd", Time ()). " _all ";
$sql = "";}
}
}
if ($sql! = "") {$filename. = ("_v". $p. ". SQL ");
if (Write_file ($sql, $filename))
$MSGS []=] All data Sheets-volumes-". $p." -Data backup complete, generate backup file ' $backup/$filename ' ";}
Show_msg ($MSGS);
/*---------------------*/}/* end of the volume--------------------------------------*/
/*--------Back up all tables end */}/*---------------------------------------------*/
/*--------Backup single table------*/elseif ($_post[' Bfzl ']== "Danbiao") {/*------------*/
if (!$_post[' tablename ')
{$msgs []=] Select the data table to be backed up, show_msg ($MSGS); Pageend ();}
/*--------Roll */if (!$_post[' Fenjuan ') {/*-------------------------------*/
$sql =make_header ($_post[' tablename ');
$d->query ("SELECT * from". $_post[' tablename ');
$num _fields= $d->nf ();
while ($d->nextrecord ())
{$sql. =make_record ($_post[' tablename '), $num _fields);}
$filename =date ("Ymd", Time ()). " _ ". $_post[' tablename ').". SQL ";
if ($_post[' Weizhi ']== "LOCALPC") down_file ($sql, $filename);
ElseIf ($_post[' Weizhi ']== "server")
{if (Write_file ($sql, $filename))
$MSGS []= "Table-". $_post[' tablename '). " -Data backup complete, generate backup file ' $backup/$filename ' ";
else $msgs []= "Backup Table-". $_post[' tablename '). " -Failure ";
Show_msg ($MSGS);
Pageend ();
}
/*----------------do not roll over */}/*------------------------------------*/
/*----------------Roll */else{/*--------------------------------------*/
if (!$_post[' filesize ')
{$msgs []=] Please fill in the size of the backup file, Show_msg ($msgs);p ageend ();}
$sql =make_header ($_post[' tablename '); $p = 1;
$filename =date ("Ymd", Time ()). " _ ". $_post[' tablename ');
$d->query ("SELECT * from". $_post[' tablename ');
$num _fields= $d->nf ();
while ($d->nextrecord ())
{
$sql. =make_record ($_post[' tablename '), $num _fields);
if (strlen ($sql) >=$_post[' filesize ']*1000) {
$filename. = ("_v". $p. ". SQL ");
if (Write_file ($sql, $filename))
$MSGS []= "Table-". $_post[' tablename '). " -Volume-". $p." -Data backup complete, generate backup file ' $backup/$filename ' ";
else $msgs []= "Backup Table-". $_post[' tablename '). " -Failure ";
$p + +;
$filename =date ("Ymd", Time ()). " _ ". $_post[' tablename ');
$sql = "";}
}
if ($sql! = "") {$filename. = ("_v". $p. ". SQL ");
if (Write_file ($sql, $filename))
$MSGS []= "Table-". $_post[' tablename '). " -Volume-". $p." -Data backup complete, generate backup file ' $backup/$filename ' ";}
Show_msg ($MSGS);
/*----------*/}/* end of the volume--------------------------------------------------*/
/*----------Backup single table End */}/*----------------------------------------------*/
/*---*/}/*-------------the end of the main program------------------------------------------*/
function Write_file ($sql, $filename)
{
$re =true;
Global $backup;
if (!@ $fp =fopen ($backup. " /". $filename," w+ ") {$re =false; echo" failed to open target file ";}
if (! @fwrite ($FP, $sql)) {$re =false; echo "Failed to write file";}
if (! @fclose ($fp)) {$re =false; echo "failed to close target file";}
return $re;
}
function Down_file ($sql, $filename)
{
Ob_end_clean ();
Header ("Content-encoding:none");
Header ("Content-type:". ( Strpos ($_server[' http_user_agent '), ' msie ')? ' Application/octetstream ': ' Application/octet-stream ');
Header ("Content-disposition:". ( Strpos ($_server[' http_user_agent '), ' msie ')? ' Inline; ': ' Attachment; ')." Filename= ". $filename);
Header ("Content-length:". strlen ($sql));
Header ("Pragma:no-cache");
Header ("expires:0");
Echo $sql;
$e =ob_get_contents ();
Ob_end_clean ();
}
function writeable ($dir)
{
if (!is_dir ($dir)) {
@mkdir ($dir, 0777);
}
if (Is_dir ($dir))
{
if ($fp = @fopen ("$dir/test.test", ' W '))
{
@fclose ($FP);
@unlink ("$dir/test.test");
$writeable = 1;
}
else {
$writeable = 0;
}
}
return $writeable;
}
function Make_header ($table)
{Global $d;
$sql = "DROP table if exists". $table. " n ";
$d->query ("Show create TABLE". $table);
$d->nextrecord ();
$tmp =preg_replace ("/n/", "", $d->f ("CREATE TABLE"));
$sql. = $tmp. " n ";
return $sql;
}
function Make_record ($table, $num _fields)
{Global $d;
$comma = "";
$sql. = "INSERT into". $table. "VALUES (";
for ($i = 0; $i < $num _fields; $i + +)
{$sql. = ($comma. "". Mysql_escape_string ($d->record[$i]). "'"); $comma = ",";}
$sql. = ") n";
return $sql;
}
function Show_msg ($MSGS)
{
$title = "Hint:";
echo "
"; Echo"
| ". $title." |
"; Echo"
";
while (list ($k, $v) =each ($MSGS))
{
echo "
- ". $v."
";
}
echo "
|
";
}
function Pageend ()
{
Exit ();
}
?>
Class db{
var $linkid;
var $sqlid;
var $record;
function db ($host = "", $username = "", $password = "", $database = "")
{
if (! $this->linkid) @ $this->linkid = mysql_connect ($host, $username, $password) or Die ("Connection server failed.");
@mysql_select_db ($database, $this->linkid) or Die ("Unable to open database");
return $this->linkid;}
function query ($sql)
{if ($this->sqlid=mysql_query ($sql, $this->linkid)) return $this->sqlid;
else {
$this->err_report ($sql, mysql_error);
return false;}
}
function nr ($sql _id= "")
{if (! $sql _id) $sql _id= $this->sqlid;
Return mysql_num_rows ($sql _id);}
function NF ($sql _id= "")
{if (! $sql _id) $sql _id= $this->sqlid;
Return Mysql_num_fields ($sql _id);}
function NextRecord ($sql _id= "")
{if (! $sql _id) $sql _id= $this->sqlid;
if ($this->record=mysql_fetch_array ($sql _id)) return $this->record;
else return false;
}
function f ($name)
{
if ($this->record[$name]) return $this->record[$name];
else return false;
}
function Close () {mysql_close ($this->linkid);}
function Lock ($tblname, $op = "Write")
{if (mysql_query ("Lock Tables". $tblname. " ". $op)) return true; else return false;}
function unlock ()
{if (mysql_query ("Unlock tables)") return true; else return false;}
function Ar () {
Return @mysql_affected_rows ($this->linkid);
}
function i_id () {
return mysql_insert_id ();
}
function Err_report ($sql, $err)
{
echo "MySQL Query error
";
echo "query statement:" $sql. "
";
echo "error message:" $ERR;
}
/**************************************** class End ***************************/
Global $mysqlhost, $mysqluser, $mysqlpwd, $mysqldb, $backup;
$mysqlhost = $mydbhost; Host Name
$mysqluser = $mydbuser; Login Name
$mysqlpwd = $MYDBPW; Password
$mysqldb = $mydbname; Name of database
$d =new db ($mysqlhost, $mysqluser, $mysqlpwd, $MYSQLDB);
$d->query ("Set names ' UTF8 '");
http://www.bkjia.com/PHPjc/630798.html www.bkjia.com true http://www.bkjia.com/PHPjc/630798.html techarticle PHP MySQL Database backup program/* Provides an example of the PHP MySQL database backup program, a good way to your database online real-time backup, so that the database can be saved ...