1. configuration file sql.config.php
<? PHP $cfg _dbhost // $cfg _dbuser// $cfg _dbpwd/ / $cfg _dbname // ?>
2. Backup program db_backup.php
<?php Date_default_timezone_set (' Asia/shanghai '); require_once((dirname(__file__).‘ /sql.config.php ')); //setting the 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 page file path, and the SQL file is exported to this folder $tmpFile= (dirname(__file__))." /".$filename; //exporting a database with the mysqldump command exec("Mysqldump-h$cfg _dbhost-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; ?>
3, restore the database restore_mysql.html
<! DOCTYPE html> " Database sql file ":<input id=" Sqlfile "name=" Sqlfile "type=" File "/> <input id=" Submit "name=" submit "type=" submit "value=" restore "/> </form> </body>
4, restore the program db_restore.php
<?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__).‘ /sql.config.php ')); if(isset($_post[' Sqlfile '] ) ) { $file _name=$_post[' Sqlfile '];//the SQL file name to import//$file _name = "2014-06-10_09-24-44-familymart-app.sql"; $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);//setting the time-out to 0 indicates that it has been executed. When PHP is not valid in Safe mode, this may cause the import to time out, 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 database$dbhost");//connecting to a database mysql_select_db($dbname) or die("Cannot open database$dbname");//Open Database Echo"<p> is emptying the database, please wait a moment ....<br>"; $result=mysql_query("SHOW Tables"); while($currow=Mysql_fetch_array($result)) { mysql_query("Drop TABLE IF EXISTS$currow[0] "); Echo"Empty the data table".$currow[0]. " Success <br> "; } Echo"<br> Congratulations on your cleaning MySQL success <br>"; Echo"Performing Import Database Operations <br>"; //mysql command to import a database exec("Mysql-h$cfg _dbhost-U$cfg _dbuser-P$cfg _dbpwd $cfg _dbname< ".$file _name); Echo"<br> Import done! "; Mysql_close(); } ?>
If after restoring the database through the program, found that the data error, may be the database version is not the same, the ' \ n ' character resolution results are different, and the above program restore did not link the database,
This can be restored through the phpMyAdmin import function, if the database file is large, you also need to phpMyAdmin and php.ini related configuration to increase
PHP backup and restore MySQL database