PHP backup/restoration of MySQL database code _ PHP Tutorial

Source: Internet
Author: User
PHP backup to restore the MySQL database code. The following code: 1. back up the database and download it to the local [db_backup.php]. copy the code as follows :? Php sets the SQL file to save the file name $ filenamedate (Y-m-d_H-i-s).-. $ cf the following is the code:

I. back up the database and download it to the local [db_backup.php]

The code is as follows:


// Set the SQL file storage file name
$ Filename = date ("Y-m-d_H-i-s"). "-". $ pai_dbname. ". SQL ";
// Saved file name
Header ("Content-disposition: filename =". $ filename );
Header ("Content-type: application/octetstream ");
Header ("Pragma: no-cache ");
Header ("Expires: 0 ");
// Obtain the path of the file on 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 $ export _dbuser-p $ export _dbpwd -- default-character-set = utf8 $ export _dbname>". $ tmpFile );
$ File = fopen ($ tmpFile, "r"); // open the file
Echo fread ($ file, filesize ($ tmpFile ));
Fclose ($ file );
Exit;
?>


2. restore the database [db_restore.php]

The code is as follows:



// My database information is stored in the config. php file, so load this file. if you do not store it in the file, just comment out this line;
Require_once (dirname (_ FILE _). '/.../../include/config. php '));
If (isset ($ _ POST ['sqlfile'])
{
$ File_name = $ _ POST ['sqlfile']; // name of the SQL file to be imported
$ Dbhost = $ mongo_dbhost; // database host name
$ Dbuser = $ user_dbuser; // database username
$ Dbpass = $ pai_dbpwd; // database password
$ Dbname = $ pai_dbname; // database name

Set_time_limit (0); // Set the timeout value to 0, indicating that the operation is always executed. If php is invalid in safe mode, the import may Time Out. in this case, you need to import data in multiple parts.
$ Fp = @ fopen ($ file_name, "r") or die ("SQL file $ file_name" cannot be opened); // open the file
Mysql_connect ($ dbhost, $ dbuser, $ dbpass) or die ("cannot connect to database $ dbhost"); // Connect to database
Mysql_select_db ($ dbname) or die ("You cannot open the database $ dbname"); // open the database

Echo"

Clearing Database. please wait ....
";
$ Result = mysql_query ("SHOW tables ");
While ($ currow = mysql_fetch_array ($ result ))
{
Mysql_query ("drop table if exists $ currow [0]");
Echo "the data table [". $ currow [0]. "is cleared successfully!
";
}
Echo"
Congratulations! you have cleared MYSQL
";

Echo "importing database...
";
// MySQL command for database import
Exec ("mysql-u $ pai_dbuser-p $ pai_dbpwd $ pai_dbname <". $ file_name );
Echo"
Import complete! ";
Mysql_close ();
}
?>

I. back up the database and download it to the local [db_backup.php] code as follows :? Php // Set the SQL file save file name $ filename = date ("Y-m-d_H-i-s"). "-". $ cf...

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.