Home >Backend Development >PHP Tutorial >PHP backup/restore MySQL database code_PHP tutorial

PHP backup/restore MySQL database code_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:32:06940browse

The following is the code:

1. Back up the database and download it to local [db_backup.php]

Copy the code The code is as follows:

//Set the SQL file save file name
$filename=date("Y-m-d_H-i-s")."-".$cfg_dbname.".sql";
// Saved file name
header("Content-disposition:filename=".$filename);
header("Content-type:application/octetstream"); ​​
header("Pragma :no-cache");
header("Expires:0");
// Get the file path of the current page and export the SQL file to this folder
$tmpFile = (dirname(__FILE__) )."\".$filename;
// Use the MySQLDump command to export the database
exec("mysqldump -u$cfg_dbuser -p$cfg_dbpwd --default-character-set=utf8 $cfg_dbname > ".$ tmpFile);
$file = fopen($tmpFile, "r"); // Open the file
echo fread($file,filesize($tmpFile));
fclose($file);
exit;
?>

2. Restore the database [db_restore.php]
Copy code The code is as follows:


[Database SQL file]:


// My database information is stored in the config.php file, so load this file. If yours is not stored in this file, just comment this line;
require_once ((dirname(__FILE__).'/../../include/config.php'));
if ( isset ( $_POST['sqlFile'] ) )
{
$file_name = $_POST['sqlFile']; //SQL file name to be imported
$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); //Set the timeout to 0, which means it will always be executed. When php is invalid in safe mode, it may cause the import to time out. In this case, it is necessary to import in sections
$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");//Connect to database
mysql_select_db($dbname) or die ("Cannot open Database $dbname");//Open the database

echo "

Clearing the database, please wait....
";
$result = mysql_query("SHOW tables ");
while ($currow=mysql_fetch_array($result))
{
mysql_query("drop TABLE IF EXISTS $currow[0]");
echo "Clear the data table【". $currow[0]."] Success!
";
}
echo "
Congratulations on successfully cleaning MYSQL
";

echo "Executing Import database operation
";
//MySQL command to import database
exec("mysql -u$cfg_dbuser -p$cfg_dbpwd $cfg_dbname < ".$file_name);
echo "< ;br>Import completed! ";
mysql_close();
}
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322874.htmlTechArticleThe following is the code: 1. Back up the database and download it to local [db_backup.php] Copy the code as follows: ?php //Set the SQL file save file name $filename=date("Y-m-d_H-i-s")."-".$cf...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn