Two Methods of backup/recovery in MySQL database

Source: Internet
Author: User
The following describes two methods for restoring MySQL database backup. Method 1: query ($ SQL) ;}combine the generated temporary backup files $ outfiledate (Y-m-d ).. SQL; if (file_exists ($ dbdir. $ outfile) @ unlink ($ dbdir. $ outfile); $ fprfopen ($ dbdir. $ outfile, a); foreach ($ txtnameas $

The following describes two methods for backing up and recovering MySQL databases. Method 1: query ($ SQL) ;}// combine the generated temporary backup files $ outfile = date (Y-m-d ).. SQL; if (file_exists ($ dbdir. $ outfile) @ unlink ($ dbdir. $ outfile); $ fpr = fopen ($ dbdir. $ outfile, a); foreach ($ txtname as $

The following describes two methods for backing up and recovering MySQL databases.

Method 1:

Query ($ SQL) ;}// combine the generated temporary backup files $ 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 the temporary backup file $ tdata = readfiles ($ dbdir. $ txt); // generate the backup file $ tbl = explode (". ", $ txt); $ str = ""'. $ tbl [0]. "'{{". $ tdata. "}}"; if (fwrite ($ fpr, $ str) {echo $ tbl [0]. "... $ outfile written successfully!

N ";} else {echo $ tbl [0]."... failed to write $ outfile!

N ";}@ unlink ($ dbdir. $ txt) ;}} fclose ($ fpr);} else {// restore 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]) {// clear the TABLE $ SQL = "truncate table '$ tt"'; $ db-> query ($ SQL); // reload data $ SQL = "LOAD DA TA LOW_PRIORITY INFILE '". $ dbdir. $ tt. ". txt ". "'into table' $ tt" '; if ($ db-> query ($ SQL) {fclose ($ fp); echo $ tt. "Table data recovery successful!

N "; unlink ($ dbdir. $ tt.". txt ");} else {echo $ tt." table data recovery failed!

N ";}}// echo $ tdata; // print_r ($ data_ar); // exit ;}} /** Read file Content * parameter $ file is the file name and complete path * returned 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 2:

I want to directly back up the database in the PHP background, so I have never thought of any idea. At the beginning, I was thinking about using php to access the directory where the server installed mysql, for example, in the/usr/local/mysql/data directory, the following files are directly backed up, but a problem occurs:

First, the user who runs php is an apche user. For example, if it is a nobody, it generally has no permission to access the/usr/local/mysql/data directory.

Second, even if you can access it, how can you copy the files in the/usr/local/mysql/data directory? Because mysql does not run access when it is running, the nobody user has the permission to stop mysql services, which is impossible!

The more I thought about the problem, the more I couldn't find a way to see if I could start to operate the database in PHP. So I went to see phpMyadmin and Discuz! So I copied Discuz! The following method is used to back up the database.

Note: More exciting articles, please pay attention to SanlianProgramming TutorialTopic.

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.