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...