Here are two methods for MySQL database backup/restore.
Method One:
Query ($sql); ///Make the generated temporary backup files together $outfile = Date ("y-m-d"). SQL "; if (File_exists ($dbdir. $outfile)) @unlink ($dbdir. $outfile); $FPR = fopen ($dbdir. $outfile, "a"); foreach ($txtname as $txt) {if (File_exists ($dbdir. $txt)) {//Read temporary backup file $tdata = Readfiles ($dbdir. $txt);//generate backup file $TBL = expl Ode (".", $txt); $str = "'". $tbl [0]. " ' {{'. $tdata. '}} '; if (fwrite ($FPR, $str)) {echo $tbl [0]. " ... Write $outfile Successful!
n "; }else{echo $tbl [0]. " ... Write $outfile failed!
n "; } @unlink ($dbdir. $txt); } fclose ($FPR); }else{//recovery Data $tdata = Readfiles ($dbdir. $_post["Sqlfile"]) Preg_match_all ("/" (. *) ' {{(. *)}}/isu ', $tdata, $data _ar); foreach ($data _ar[1] as $k => $tt) {if (Empty ($data _ar[2][$k)) continue; $tfile = $dbdir. $tt. ". TXT "; $fp = fopen ($tfile, "w"); if (fwrite ($fp, $data _ar[2][$k)) {//empty table $sql = "TRUNCATE tables ' $tt '"; $db->query ($sql);//Reload data $sql = "Load Data low _priority INFILE ' ". $dbdir. $tt.". TXT "." ' Into TABLE ' $tt '; if ($db->query ($sql)) {fclose ($FP); Echo $tt. " Table Data Recovery Success!
n "; Unlink ($dbdir. $tt. ". TXT "); }else{echo $tt. " Table Data Recovery failed!
n "; }}}//echo $tdata; Print_r ($data _ar); Exit }/* * Read the file content * parameter $file for file name and full path * return file content * * Function readfiles ($file) {$tdata = ""; $fp = fopen ($file, "R"); if (FileSize ($file) <= 0) return; while ($data = Fread ($fp, FileSize ($file))) {$tdata. = $data;} fclose ($FP); return $tdata; }?>
Method Two:
Want to manage in the PHP background directly can back up the database, so think, have no idea, the beginning is to consider using PHP to access the server to install MySQL directory, such as the/usr/local/mysql/data directory, directly below the corresponding file for backup, but there are problems:
First, running PHP is Apche users, such as nobody, then it is generally not 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 without running access, then nobody users have permission to stop MySQL services, it is impossible!
The more think the more wrong, there is no way to see if you can start from the PHP operating database, so went to see the next phpMyAdmin and discuz! code, OH, so stole the discuz! code, formed the following method of backing up the database.
Note : Please pay attention to the triple programming Tutorials section for more wonderful articles .