PHP MySQL Database Backup program

Source: Internet
Author: User
Tags mysql query php mysql split strlen mysql database
The code is as follows Copy Code

?>
<!doctype HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/ Xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<link type= "text/css Tutorial" rel= "stylesheet" href= "Common/control.css" >
<script type= "text/web Effects" language= "JavaScript" src= "Common/admin.otable.js" ></script>
<script type= "Text/javascript" language= "JavaScript" src= "Common/include.func.js" ></script>
<title> Data Management </title>
<style type= "Text/css" >
span {padding-left:8px;}
</style>

<body class= "Contentbody" >
<div class= "Maindiv" >
<span class= "Ctitle" >esweb system data Management </span>
<ul class= "CMenu" >
<li><a href= "sql_backup.php" > Data backup </a></li>
<li><a href= "sql_restore.php" > Data restore </a></li>
<li><a href= "sql_optimize.php" > Data optimization </a>
</ul>

<div class= "Concontent" >
<?php

/*--------------Interface--------------*/if (!$_post[' act ') {/*----------------------* *
$MSGS []= "Server backup directory is $backup";
$MSGS []=] for large data tables, it is highly recommended to use a split-volume backup.
$MSGS []=] only choose to back up to the server to use the split-volume backup function ";
Show_msg ($MSGS);
?>
<div class= "thead" ><span class= "left" > If Backup is required , restore or optimize <font color= "Red" > A large number of data </font&gt, the recommended use of "<a href=" http://www.phome.net/ebak2010/"target=" _blank "> Empire Backup King 2010</a>" </span> <span class= "right" ></span></div>
<form name= "MyForm" method= "post" action= "sql_backup.php" >
<table cellpadding= "" cellspacing= "0" border= "0" width= "100%" class= "CTable" id= "otable" align= "Center" >
<tr>
<th colspan= "2" >esweb system data Backup </th>
</tr>
<tr>
&LT;TD align= "right" width= ">" Select Backup Mode </td>
<td><input type= "Radio" name= "Bfzl" value= "Quanbubiao" checked= "checked"/> Backup all data
<input type= "Radio" name= "Bfzl" value= "Danbiao"/> Backup single sheet data
<select name= "tablename" ><option value= "" > Please select </option>
<?php
$d->query ("Show Table status from $mysqldb");
while ($d->nextrecord ()) {
echo "<option value= '". $d->f (' name '). "' > ". $d->f (' name ')." </option> ";}
?>
</select></td>
</tr>
<tr>
&LT;TD align= "Right" > select target Location </td>
<td><input type= "Radio" name= "Weizhi" value= "Server" checked= "checked"/> Backup to Server
<input type= "Radio" name= "Weizhi" value= "LOCALPC"/> Backup to local </td>
</tr>

<tr>
&LT;TD align= "Right" > Use a split-volume backup </td>
<td><input type= "checkbox" Name= "Fenjuan" value= "yes" checked= "checked"/>
Split-volume backup <input name= "filesize" type= "text" value= "1260" size= "" class= "Text2"/> k <span> (only select Backup to Server, To use the split-volume backup feature) </span></td>
</tr>
<tr>
&LT;TD align= "Right" > procedure Description </td>
<td>1. For large data tables, it is highly recommended that you use a volume backup.
<br>2. You can use the split-volume backup feature only if you choose to back up to the server.
</td>
</tr>
<tr>
&LT;TD align= "center" colspan= "2" ><input type= "submit" id= "Act" name= "act" value= "Backup" class= "button"/> < Input type= "reset" value= "reset" class= "button"/></td>
</tr>
</table>
</form>
<?php/*-------------interface End-------------*/}/*---------------------------------* *
/*----*/else{/*--------------Main program-----------------------------------------* *
if ($_post[' Weizhi ']== "LOCALPC" &&$_post[' Fenjuan ']== ' yes ')
{$msgs []=] only choose to back up to the server to use the split-volume backup function ";
Show_msg ($MSGS); Pageend ();}
if ($_post[' Fenjuan ']== "yes" &&!$_post[' filesize '])
{$MSGS []=] You have selected the volume backup function, but not the volume file size ";
Show_msg ($MSGS); Pageend ();}
if ($_post[' Weizhi ']== "Server" &&!writeable ($backup))
{$MSGS []= backup file directory ' $backup ' not writable, please modify the directory attribute ';
Show_msg ($MSGS); Pageend ();}

/*----------back up all Tables-------------*/if ($_post[' Bfzl ']== "Quanbubiao") {/*----* *
/*----does not */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 completed, generate backup file ' $backup/$filename ';
else $msgs []=] failed to back up all data tables;
Show_msg ($MSGS);
Pageend ();
}
/*-----------------do not roll over */}/*-----------------------* *
/*-----------------*/else{/*-------------------------* *
if (!$_post[' filesize '])
{$msgs []= "Please fill in the backup file volume size"; 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 tables-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 tables-volumes-". $p." -Data backup complete, generate backup file ' $backup/$filename ';}
Show_msg ($MSGS);
/*---------------------The end of the */}/*--------------------------------------* *
/*--------Backup All tables End */}/*---------------------------------------------* *

/*--------Backup sheet------*/elseif ($_post[' Bfzl ']== "Danbiao") {/*------------* *
if (!$_post[' tablename '])
{$msgs []=] Select the datasheet to back up, show_msg ($MSGS); Pageend ();}
/*--------does not */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 */}/*------------------------------------* *
/*----------------*/else{/*--------------------------------------* *
if (!$_post[' filesize '])
{$msgs []= "Please fill in the backup file volume size"; 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);
/*----------The end of the */}/*--------------------------------------------------* *
/*----------Backup sheet End */}/*----------------------------------------------* *

/*---*/}/*-------------main program End------------------------------------------* *

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. " ";
$d->query ("Show create TABLE". $table);
$d->nextrecord ();
$tmp =preg_replace ("//", "", $d->f ("CREATE TABLE"));
$sql. = $tmp. " ";
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. = ")";
return $sql;
}

function Show_msg ($MSGS)
{
$title = "Hint:";
echo "<table width= ' 100% ' border= ' 0 ' cellpadding= ' 0 ' cellspacing= ' 0 ' >";
echo "<tr><td>". $title. " </td></tr> ";
echo "<tr><td><br><ul>";
while (list ($k, $v) =each ($MSGS))
{
echo "<li>". $v. " </li> ";
}
echo "</ul></td></tr></table>";
}

function Pageend ()
{
Exit ();
}
?>
</div><br/><br/>

</div>

</div>
</body>

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 is 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 <br>";
echo "query statement:". $sql. " <br> ";
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 '");

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.