PHP backup/restore MySQL database code _php tips

Source: Internet
Author: User
Tags safe mode save file import database
Here's the code:

First, back up the database and download to the local "db_backup.php"
Copy Code code as follows:

<?php
Set up SQL file Save file name
$filename =date ("Y-m-d_h-i-s"). " -". $cfg _dbname.". SQL ";
The file name that you saved
Header ("Content-disposition:filename=". $filename);
Header ("Content-type:application/octetstream");
Header ("Pragma:no-cache");
Header ("expires:0");
Gets the current paging file path, and the SQL file is exported to this folder
$tmpFile = (dirname (__file__)). " \ ". $filename;
Exporting a database with the mysqldump command
EXEC ("mysqldump-u$cfg_dbuser-p$cfg_dbpwd--default-character-set=utf8 $cfg _dbname >". $tmpFile);
$file = fopen ($tmpFile, "R"); Open File
Echo fread ($file, FileSize ($tmpFile));
Fclose ($file);
Exit
?>

Second, restore the database "db_restore.php"
Copy Code code as follows:

<form id= "Form1" Name= "Form1" method= "Post" action= "" >
"Database SQL file": <input id= "Sqlfile" name= "Sqlfile" type= "file"/>
<input id= "Submit" name= "submit" type= "submit" value= "Restore"/>
</form>
<?php
My database information is stored in the config.php file, so load this file, if you are not stored in the file, note this line can be;
Require_once (DirName (__file__). /.. /.. /include/config.php '));
if (Isset ($_post[' sqlfile '))
{
$file _name = $_post[' Sqlfile ']; The SQL file name to import
$dbhost = $cfg _dbhost; Database host Name
$dbuser = $cfg _dbuser; Database user Name
$dbpass = $cfg _dbpwd; Database Password
$dbname = $cfg _dbname; Database name

Set_time_limit (0); The timeout is set to 0, which indicates that it has been executed. When PHP is invalid in safe Mode mode, this may cause the import timeout, which requires a segmented import
$fp = @fopen ($file _name, "R") or Die ("Cannot open SQL file $file _name");/Open File
Mysql_connect ($dbhost, $dbuser, $dbpass) or Die ("Cannot connect to the database $dbhost");/Connection Database
mysql_select_db ($dbname) or Die ("Cannot open database $dbname");/Open Database

echo "<p> is emptying the database, please wait for ....<br>";
$result = mysql_query ("Show Tables");
while ($currow =mysql_fetch_array ($result))
{
mysql_query ("Drop TABLE IF EXISTS $currow [0]");
echo "clears the datasheet". $currow [0]. " Success <br> ";
}
echo "<br> congratulate you on cleaning MySQL success <br>";

echo "Performing Import Database Operations <br>";
MySQL command to import Database
EXEC ("Mysql-u$cfg_dbuser-p$cfg_dbpwd $cfg _dbname <". $file _name);
echo <br> Import complete! ";
Mysql_close ();
}
?>
Related Article

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.