- Error_reporting (0); // php alarm prompt for eliminating all evil
- // Set the email address
- $ Options = array ('email '=> array ('email1', 'email2 '),
- 'Folder' => './backup /',
- 'Mysql' => array ('localhost', 'user', 'password', 'DB '));
-
- $ B = new Backup ($ options );
-
- // Submit the backup command
- If (isset ($ _ POST ['backup '])
- {
- // Start backup
- $ B-> backupDB ();
- }
- // Display the backup table
- $ B-> outputForm ();
- ?>
Php database backup implementation code:
- /**
- * Back up a mysql database
- * Edit: bbs.it-home.org
- */
- 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 = "\ n". $ createtable. "\ n ";
-
- 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: $ from \ r \ n ".
- "MIME-Version: 1.0 \ r \ n ".
- "Content-Type: multipart/mixed; boundary =". $ msep. "\ r \ n ".
- "-- $ Msep \ r \ n ".
- "Content-Type: text/plain \ r \ n ".
- "Content-Transfer-Encoding: 8bit \ r \ n ".
- $ Message_body. "\ r \ n ";
-
- // 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. "\ r \ n ".
- "Content-Type: application/zip; name = 'backup '\ r \ n ".
- "Content-Transfer-Encoding: base64 \ r \ n ".
- "Content-Disposition: attachment; filename='Backup.zip '\ r \ n ".
- "Content-Description: Mysql Datenbank Backup im Anhang \ r \ n ".
- $ Attachment. "\ r \ n ";
-
- // 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;
- }
- }
- ?>
|