Class Backup { /** * @ Var is used to save configuration parameters. */ Var $ config; /** * @ Var is used to save mysql dump data. */ Var $ dump; /** * @ Var is used for database result data and insert commands. */ Var $ struktur = array (); /** * @ Var compression file name zip */ Var $ datei; /** * Structure functions * Connect to the database * @ Return */ Public function Backup ($ options) { // Read the configuration from the parameter Foreach ($ options AS $ name => $ value) { $ This-> config [$ name] = $ value; } // Connect to the database Mysql_connect ($ this-> config ['mysql'] [0], $ this-> config ['mysql'] [1], $ This-> config ['mysql'] [2]) or die (mysql_error ()); Mysql_select_db ($ this-> config ['mysql'] [3]) or die (mysql_error ()); } /** * Functions used to execute the database backup process * @ Return */ Public function backupDB () { // Backup start Command If (isset ($ _ POST ['backup ']) { // Check whether a data table is selected If (empty ($ _ POST ['table']) { Die ("Select a data table. "); } /** Start backup **/ $ Tables = array (); $ Insert = array (); $ SQL _statement = ''; // Lock the database to be backed up to prevent dirty data reading Foreach ($ _ POST ['table'] AS $ table) { Mysql_query ("lock table $ table WRITE "); // Obtain the database structure $ Res = mysql_query ('Show CREATE Table'. $ TABLE .''); $ Createtable = mysql_result ($ res, 0, 1 ); $ Str = "nn". $ createtable. "nn "; Array_push ($ tables, $ str ); // Query all data rows in a data table $ SQL = 'select * from'. $ table; $ Query = mysql_query ($ SQL) or die (mysql_error ()); $ Feld_anzahl = mysql_num_fields ($ query ); $ SQL _statement = '-- -- Data Table '$ table' -- '; // Start reading data and convert it to the insert command While ($ ds = mysql_fetch_object ($ query )){ $ SQL _statement. = 'Insert INTO '''. $ table .''('; For ($ I = 0; $ I <$ feld_anzahl; $ I ++ ){ If ($ I ==$ feld_anzahl-1 ){ $ SQL _statement. = mysql_field_name ($ query, $ I ); } Else { $ SQL _statement. = mysql_field_name ($ query, $ I ).','; } } $ SQL _statement. = ') VALUES ('; For ($ I = 0; $ I <$ feld_anzahl; $ I ++ ){ $ Name = mysql_field_name ($ query, $ I ); If (empty ($ ds-> $ name )){ $ Ds-> $ name = 'null '; } If ($ I ==$ feld_anzahl-1 ){ $ SQL _statement. = '"'. $ ds-> $ name .'"'; } Else { $ SQL _statement. = '"'. $ ds-> $ name .'",'; } } $ SQL _statement. = "); n "; } // Put the insert data in the array, removing duplicates If (! In_array ($ SQL _statement, $ insert )) { Array_push ($ insert, $ SQL _statement ); Unset ($ SQL _statement ); } Unset ($ SQL _statement ); } // Put the database structure together with the insert command $ This-> struktur = array_combine ($ tables, $ insert ); // Execute the dump function $ This-> createDUMP ($ this-> struktur ); // Generate a zip package $ This-> createZIP (); /** End backup **/ // Send an email to the specified email address. the attachment contains SQL Backup. if you have set it, ^_^ If (isset ($ this-> config ['email ']) &! Empty ($ this-> config ['email ']) { $ This-> sendEmail (); } // Output Echo 'backup complete $ this-> datei. '"> Download backup
'; } } /** * Email sending function * @ Return */ Protected function sendEmail () { // Read the email address Foreach ($ this-> config ['email '] AS $ email) { $ To = $ email; $ From = $ this-> config ['email '] [0]; $ Message_body = "The zip package contained in this email is a database backup "; $ Msep = strtoupper (md5 (uniqid (time ()))); // Set the email header $ Header = "From: $ fromrn ". "MIME-Version: 1.0rn ". "Content-Type: multipart/mixed; boundary =". $ msep. "rnrn ". "-- $ Mseprn ". "Content-Type: text/plainrn ". "Content-Transfer-Encoding: 8 bitrnrn ". $ Message_body. "rn "; // File name $ Dateiname = $ this-> datei; // Size of the compressed package $ Dateigroesse = filesize ($ dateiname ); // Read the compressed package $ F = fopen ($ dateiname, "r "); // Save it to the attachment $ Attached_file = fread ($ f, $ dateigroesse ); // Close the compressed package Fclose ($ f ); // Create an attachment $ Attachment = chunk_split (base64_encode ($ attached_file )); // Set the attachment header $ Header. = "--". $ Msep. "rn ". "Content-Type: application/zip; name = 'backup 'RN ". "Content-Transfer-Encoding: base64rn ". "Content-Disposition: attachment; filename='Backup.zip 'RN ". "Content-Description: Mysql Datenbank Backup im Anhangrnrn ". $ Attachment. "rn "; // Mark the end of the attachment as unknown $ Header. = "-- $ msep --"; // Mail title $ Subject = "database backup "; // Php support must be enabled for sending emails ^ If (mail ($ to, $ subject, '', $ header) = FALSE) { Die ("unable to send mail, please check the email address "); } Echo" Email sent successfully "; } } /** * Create a Compressed database backup package and save it to the specified directory on the server. * @ Return */ Protected function createZIP () { // You must have sufficient folder permissions. Chmod ($ this-> config ['Folder'], 0777 ); // Create a compressed package $ Zip = new ZipArchive (); // Set the compressed package file name $ This-> datei = $ this-> config ['Folder']. $ this-> config ['mysql'] [3]. "_" . Date ("j_F_Y_g_ I _a"). ". zip "; // Check whether the compressed package can be opened If ($ zip-> open ($ this-> datei, ZIPARCHIVE: CREATE )! = TRUE ){ Exit ("cannot open <". $ this-> datei. "> n "); } // Put the dump data in the compressed package $ Zip-> addFromString ("dump. SQL", $ this-> dump ); // Close the compressed package $ Zip-> close (); // Check whether the compressed package is generated If (! File_exists ($ this-> datei )) { Die ("unable to generate compressed package "); } Echo"The database backup package is successfully generated. "; } /** * Mysql dump function * @ Param object $ dump * @ Return */ Protected function createDUMP ($ dump) { $ Date = date ("F j, Y, g: I "); $ Header = < -- SQL Dump -- -- Host: {$ _ SERVER ['http _ host']} -- Erstellungszeit: {$ date} -- -- Datenbank: '{$ this-> config ['mysql'] [3]}' -- ---------------------------------------------------------- HEADER; Foreach ($ dump AS $ name => $ value) { $ SQL. = $ name. $ value; } $ This-> dump = $ header. $ SQL; } /** * Generate interface functions for selecting data tables * @ Return */ Public function outputForm () { // Select All $ Result = mysql_list_tables ($ this-> config ['mysql'] [3]); $ Buffer =' Select the data table to be backed up '; Echo $ buffer; } } ?> |