But there was a problem:
First, running PHP is Apche users, such as the nobody, then it is generally not authorized access to the/usr/local/mysql/data directory
Second, even if you can access, then how can you copy the files in the/usr/local/mysql/data directory? Because MySQL is running when the access is not run, then nobody users have the right to stop the MySQL service, impossible!
The more think the more wrong, there is no way to see if you can start from the PHP operating database, so I went to see the next phpMyAdmin and discuz! The code, OH, so stole the discuz! Code, which forms the following method of backing up the database. (Thanks to discuz! here.) The developer)
There are two ways to back up a database, one is to back up the structure of the database, one is to back up the structure and all the data, of course, the second method is good, but I do it to consider the possible requirements.
/****** BACKUP DATABASE Structure ******/
/*
Function name: Table2sql ()
function function: Transform the structure of a table into SQL
Function parameters: $table: Table name to be extracted
Return value: Returns the result after extraction, SQL collection
Function Author: heiyeluren
*/
/****** backup database structure and all data ******/
/*
Function name: Data2sql ()
function function: Transform the structure and data of a table into SQL
Function parameters: $table: Table name to be extracted
Return value: Returns the result after extraction, SQL collection
Function Author: heiyeluren
*/
function Data2sql ($table)
{
Global $db;
$tabledump = "DROP TABLE IF EXISTS $table; \ n";
$createtable = $db->query ("Show CREATE TABLE $table");
$create = $db->fetch_row ($createtable);
$tabledump. = $create [1]. "; N\n ";
/****** Concrete Implementation Operation ******/
OK, now that we've written all the code, how do we implement the backup in a specific program, we'll look at the code below.
/* BACKUP DATABASE * *
Note: Our database operation takes the Phplib DB class
Define the data tables, prefixes, and where to save
$tables = Array (' Us_sort ', ' us_download ', ' us_article ', ' Us_guestbook '); Defines the data table to be saved, an array
$prefix = ' Us_ '; The prefix of the. sql file to save
$saveto = ' server '; Where to save, local or server, default is server
$back _mode = ' all '; How do you want to save the entire backup or just the database structure?
$admin = ' Heiyeluren '; Administrator name
$admin _email = ' heiyeluren@163.com '; Administrator Mailbox
Define file name for data save
$local _filename = $prefix. Date (' Ymd_his '). SQL "';
if (! $filename) {$filename = $db _backup_path. $prefix. Date (' Ymd_his_ '). Create_check_code (4). ". SQL"; }
$filename = $prefix. Date (ymd_his). Create_check_ Code (6). ". SQL "; File names saved on the server
Note the following Create_check_code () function, which is a function of generating random code, which can be referenced in detail:
Http://www.jb51.net/article/17423.htm
Get database structure and data content
foreach ($tables as $table)
{
if ($back _mode = = ' All ') {$sqldump. = Data2sql ($table);}
if ($back _mode = = ' table ') {$sqldump. = Table2sql ($table);}
}
If the data content is not empty, start saving
if (Trim ($sqldump))
{
Write opening information
$sqldump =
"#--------------------------------------------------------\ n".
# data table backup \ n.
"#\n".
"# Server: $db->host\n."
"# Database: $db->database\n".
"# Backup number:". CREATE_SESS_ID (). " \ n ". Here's a function to generate the session ID
"# Backup time:". Time_to_date (', 6). " \ n ". Here is the function to get the current time
"#\n".
"# admin: $admin ($admin _email) \ n". Administrator's username and email address
"# $copyright \ n".
"#--------------------------------------------------------\n\n\n."
$sqldump;
Save to Local
if ($saveto = = "local")
{
Ob_end_clean ();
Header (' Content-encoding:none ');
Header (' Content-type: '. Strpos ($HTTP _server_vars[' http_user_agent '], ' msie ')? ' Application/octetstream ': ' Application/octet-stream ');
Header (' Content-disposition: '. Strpos ($HTTP _server_vars[' http_user_agent '], ' msie ')? ' Inline; ': ' Attachment; ').' Filename= "'. $local _filename);
Header (' Content-length: ' strlen ($sqldump));
Header (' Pragma:no-cache ');
Header (' expires:0 ');
Echo $sqldump;
}
Save to local end
Save on server
if ($saveto = = "Server")
{
if ($filename!= "")
{
@ $fp = fopen ($filename, "w+");
if ($FP)
{
@flock ($FP, 3);
if (@!fwrite ($FP, $sqldump))
{
@fclose ($FP);
Exit_msg ("Data files cannot be saved to the server, check the directory properties you have write permissions.") ");
}
Else
{
Exit_msg ("Data is successfully backed up to the server <a href=\" $filename \ "> $filename </a>. ");
}
}
Else
{
Exit_msg ("Unable to open the directory you specified". $filename. ", please determine if the directory exists, or if you have appropriate permissions");
}
}
Else
{
Exit_msg ("You did not enter a backup file name, please return to change.") ");
}
}
Save to server end
}
Else
{
EXIT_MSG ("Data table has no content");
}
/* BACKUP DATABASE End * *
Oh, basically this is the end, and then involves a problem is how to restore the data to the database, I think this is not complicated, but it is best to meet the ability to recover data from the client and from the server.
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.