MYSQL data backuprecovery simple method

xiaoxiao2021-03-05  23

Method 1:

$ dbdir = "d: / site / dbak /"; // with absolute path $ txtname = array (); if ($ _ post) {if (! is_writable ($ dbdir)) {echo "Sorry! Specified backup directory is not Write! Please modify the permissions "; exit;} // OP is an invisible domain, identify the backup or restore IF ($ _ post [" ip "]) {// backup data // generate the temporary backup file for each table ($ _POST ["TBL_NAME"] AS $ TBL) {$ TXTNAME [] = $ TBL. ". TXT"; $ SQL = "Select * from` $ TBL` INTO OUTFILE '". $ Dbdir.end ($ txtname)." '"; $ Db-> query ($ sql);} // will be included with the generated temporary backup file $ OUTFILE = Date (" ymd ").". 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 a backup file $ TBL = Explode (".", $ Txt); $ str = "` ". $ TBL [0]." ` {{"$ TDATA."}} "; if (fwrite ($ fpr, $ str)) {Echo $ TBL [0]." ... Write $ OUTFILE!
/ n ";} Else {Echo $ TBL [0]. "... Write $ OUTFILE Failed!
/ n";} @unlink ($ dbdir. $ Txt);}} fclose ($ fpr);} else {// Restore Data $ TDATA = ReadFiles ($ DBDIR. $ _ Post ["SQLFILE"]); preg_match_all ("/` (. *) `/} /}, $ TDA TA, $ DATA_AR); Foreach ($ DATA_AR [1] AS $ K => $ TT) {if ($ DATA_AR [2] [$ K])) Continue; $ TFILE = $ dbdir. $ tt. ". TXT "; $ fp = fopen ($ TFILE," W "); if (fwrite ($ fp, $ data_ar [2) [$ k])) {// Empty Table $ SQL =" Truncate Table `$ Tt`" $ DB-> Query ($ SQL); // Re-load data $ SQL = "Load Data Low_Priority Infile '. $ Dbdir. $ Tt.". TXT "' INTO TABLE` $ Tt` "; if ($ dB->

Query ($ SQL)) {Fclose ($ FP); Echo $ TT. "Table Data Recovery is successful!
/ n"; unlink ($ dbdir. $ tt. ");} else {Echo $ TT "Table Data Recovery Failed!
/ n";}}} // Echo $ TDATA; // Print_R ($ DATA_AR); // EXIT;}} / * * Read File Content * Parameters $ File for File Name and full path * Return to file content * / function readfiles ($ file) {$ TDATA = ""; $ fp = fopen ($ file, "r"); if (FileSize ($ file) <= 0) return; while ($ DATA = FREAD ($ FP, FileSize)) {$ tdata. = $ data;} fclose ($ fp); RETURN $ TDATA;}?> Method 2:

I want to be able to back up the database directly in the PHP background management, so I want to think, there is no idea, I'm thinking about using PHP to access the server to install mySQL's directory, such as / usr / local / mysql / data directory, directly put the following The file is backed up, but there is a problem: First, running PHP is a user user, such as Nobody, then it is generally no permission to access the / usr / local / mysql / data directory second, even if you can access, then you How can I copy the files in the / usr / local / mysql / data directory? Because MySQL is not running when running, then Nobody users have permission to stop MySQL service, it is impossible! The more I want to be more and more, there is no way, I can see if I can use the database from the PHP, so I will see PHPMYADMIN and Discuz! Code, huh, so, I have been stealing discuz! The code forms a method of backup databases. (Thank you here for Discuz!) The backup database has two ways. One is the structure of only the database, one is backed up by the structure and all data, of course, the second method is ok, but I It's all made to consider possible needs. / ****** Backup database structure ***** // * function name: table2sql () function function: Conversion of the structure of the table becomes SQL function parameters: $ table: Table name to be extracted Return Value: return the result of the extraction, SQL set of functions: heiyeluren * / function table2sql ($ 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 ";

RETURN $ TABLEDUMP;} / ****** Backup database structure and all data ***** // * Function Name: Data2SQL () function function: Conversion of the structure and data of the table becomes SQL function parameters: $ TABLE : to table extraction return value: return the result of the extraction, SQL aggregate functions of: heiyeluren * / function data2sql ($ table) {global $ db; $ tabledump = "DROP tABLE IF EXISTS $ table; / n"; $ CreateTable = $ DB-> Query ("Show Create Table $ Table"); $ CREATE = $ db-> fetch_row ($ CREATABLE); $ TABLEDUMP. = $ CREATE [1]. "; / n / n"; $ Rows = $ db-> Query ("Select * from $ table"); $ numberfields = $ db-> numfields ($ rows); $ nuMMrows = $ db-> num_rows ($ rows); while ($ row = $ db -> fetch_row ($ row) {$ comMA = ""; $ TABLEDUMP. = "Insert Into $ TABLE VALUES ("; for ($ I = 0; $ I

RETURN $ TABLEDUMP;} / ****** Specific operation ****** / good, we write the code out, then how do we implement a backup in a specific program, let's see below Code. / * Backup Database * //// Note: Let's use the database operation using phplib's DB class // to save the data sheet, prefix, save to where $ TABLES = array ('us_sort', 'us_download', ' US_Article ',' US_GuestBook '); // Defines the data table to be saved, one array $ prefix =' US_ '; // To save the .sql file prefix $ saveto =' server '; // To save to where Is it a local or server, the default is server $ back_mode = 'all'; // To save mode, is all backups or only save database structure $ admin = 'heiyeluren'; // Administrator name $ admin_email = 'heiyeluren @ @ @@ 163.com '; // Administrator Mailbox

// Define the file name of the data saved $ 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"; // Save file name on the server // Note back CREATE_CHECK_CODE () Function, this is a function that generates a random code, detailed reference: // http://blog.9cbs.net/heiyeshuwu/archive/2005/01/26/268446.aspX// 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, save if (TRIM ($ SQLDUMP)) {// Write information $ sqldump = "# -------------------------------------------------------------------------------------- -------------------------------- / n "." # Data Sheet Backup / N "." # / n "# / n" # Server: $ db-> host / n "." # DB-> Database / N "." # Backup number: ". CREATE_SESS_ID ()." / n ". // Here There is a function of generating session ID "# backup time:" .time_to_date ('', 6). "/ N" is to get the function "# / n" of the current time "# / n". "# Administrator: $ 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_swer_vars [' http_user_agent '],' msie ')?')? ' Application / OCTETSTREAM ':' Application / OcTet-Stream '); Header (' Content-Disposition: '. ($ 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 in Server IF ($ Saveto == "Server Ver ") {if ($ filename! =" ") {@ $ fp = fopen ($ FileName," W "); if ($ fp) {@flock ($ fp, 3); if (@! fwrite ($ FP, $ SQLDUMP) {@fclose ($ fp); EXIT_MSG ("Data file cannot be saved to the server, check the directory properties you have written permissions. ");} Else {exit_msg (" Data successfully backed up to server $ filename .

");}} Else {EXIT_MSG (" You cannot open your specified directory ".", Please make sure that the directory exists, or if there is a corresponding permission ");}} else {exit_msg (" You do not enter a backup file Name, return to the modification. ");}} // Save to the server end} else {exit_msg (" Data Table does not have any content ");} / * Backup database end * /

Oh, basically this is over, then a question involved 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.

转载请注明原文地址:https://www.9cbs.com/read-32284.html

New Post(0)