A two-way introduction to backup/restore in MySQL database

Source: Internet
Author: User
Tags foreach backup

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 .

Related Article

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.