<?php
Class Backup { /** * @var to save configuration parameters */ var $config;
/** * @var data to save MySQL dump */ var $dump;
/** * @var for database result data and insert instructions */ var $struktur = array ();
/** * @var Compressed file name zip */ var $datei;
/** * Structure function * Connect to the database * @return */ Public Function Backup ($options) { Reading configuration from formal parameters foreach ($options as $name => $value) { $this->config[$name] = $value; }
Connecting to a 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 that perform the backup database process * @return */ Public Function backupdb () { command to start Backup if (isset ($_post[' backup ')) { Detects whether a datasheet is selected if (Empty ($_post[' table ')) { Die ("Please select a datasheet.") "); }
/** Start Backup **/ $tables = Array (); $insert = Array (); $sql _statement = ';
Lock the database that needs to be backed up to prevent dirty data from being read foreach ($_post[' table '] as $table) { mysql_query ("LOCK TABLE $table WRITE");
Getting the database structure $res = mysql_query (' Show CREATE TABLE '. $table. '); $createtable = mysql_result ($res, 0, 1); $str = "nn". $createtable. " nn ";
Array_push ($tables, $STR);
Querying all rows of data in a datasheet $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 the 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"; }
Place the insert data in an array to 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 Dump function $this->createdump ($this->struktur);
To generate a ZIP compression package $this->createzip ();
/** Backup End **/
Send a message to the specified mailbox, the attachment contains the SQL backup, if you set the words ^_^ if (isset ($this->config[' email ')) &&!empty ($this->config[' email ')) { $this->sendemail (); }
Output Echo ' $this->datei. ' " > Download Backup </a> <br/> <br/> '; } }
/** * Send mail function * @return */ protected function SendEmail () { Read Mailbox Address foreach ($this->config[' email '] as $email) { $to = $email;
$from = $this->config[' email '][0];
$message _body = "The ZIP compression package contained in this message is a database backup";
$MSEP = Strtoupper (MD5 (UNIQID (time)));
Set up email headers $header = "From: $fromrn". "Mime-version:1.0rn". "Content-type:multipart/mixed; Boundary= ". $msep." Rnrn ". "--$mseprn". "Content-type:text/plainrn". "Content-transfer-encoding:8bitrnrn". $message _body. "RN";
Filename $dateiname = $this->datei;
Compressed package size $dateigroesse = FileSize ($dateiname);
Reading a compressed package $f = fopen ($dateiname, "R"); Save to Attachment $attached _file = fread ($f, $dateigroesse); Turn off compression packs Fclose ($f); Create an attachment $attachment = Chunk_split (Base64_encode ($attached _file));
Set up Attachment headers $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";
Tag Attachment End Unknown $header. = "-$msep-";
Message headers $subject = "Database Backup";
Send mail need to open the corresponding support PHP Oh ^ ^ if (Mail ($to, $subject, ', $header) = = FALSE) { Die ("Unable to send mail, please check email address"); }
echo "<p><small> send success </small></p> message"; } }
/** * Set up a compression package for the database backup and save it to the server specified directory * @return */ protected function Createzip () {
folder permissions are sufficient chmod ($this->config[' folder '], 0777);
Create a compressed package $zip = new Ziparchive (); Set cabinet file name $this->datei = $this->config[' folder '. $this->config[' MySQL '][3]. " _" . Date ("J_f_y_g_i_a"). ". Zip ";
See if the compression pack can be turned on. if ($zip->open ($this->datei, ziparchive::create)!==true) { Exit ("Cannot open <". $this->datei. " >n "); }
Put the dump data in the zip bag. $zip->addfromstring ("Dump.sql", $this->dump); Turn off compression packs $zip->close ();
See if the compression pack has been generated if (!file_exists ($this->datei)) { Die ("Unable to generate a compressed package"); } echo "<p><small> database backup compression package successfully generated </small></p>"; }
/** * MySQL dump function * @param object $dump * @return */ protected function Createdump ($dump) { $date = Date ("F J, Y, g:i a");
$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 the interface function of the selection datasheet * @return */ Public Function Outputform () { Select All $result = mysql_list_tables ($this->config[' MySQL '][3]);
$buffer = ' <fieldset> <legend> Select the data table you want to back up </legend> <form method= "POST" action= "" > <select name= "table[]" multiple= "multiple" size= ">"; while ($row = Mysql_fetch_row ($result)) { $buffer. = ' <option value= '. $row [0]. ' " > '. $row [0]. ' </option> '; } $buffer. = ' </select> <br/><br/> <input type= "Submit" Name= "Backup" value= "Backing Up selected data table"/> </form> </fieldset> ';
Echo $buffer; } }
?> |