- error_reporting (0);//Eliminate all the evils of PHP alarm tips
- Set mailbox
- $options = Array (' email ' = = Array (' Email1 ', ' email2 '),
- ' Folder ' = './backup/',
- ' MySQL ' = array (' localhost ', ' user ', ' password ', ' db ');
- $b = new Backup ($options);
- Submit Backup Command
- if (isset ($_post[' backup '))
- {
- Start Backup
- $b->backupdb ();
- }
- Show Backup Table
- $b->outputform ();
- ?>
Copy CodeImplementation code for the PHP database backup class:
- /**
- * Backup MySQL Database
- * edit:bbs.it-home.org
- */
- Class Backup
- {
- /**
- * @var to save configuration parameters
- */
- var $config;
- /**
- * @var to save MySQL dump data
- */
- var $dump;
- /**
- * @var for database result data and insert Directives
- */
- var $struktur = array ();
- /**
- * @var Compressed file name zip
- */
- var $datei;
- /**
- * Structure function
- * Connect to Database
- * @return
- */
- Public Function Backup ($options)
- {
- Reading the configuration from the formal parameter
- 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 to perform backup database processes
- * @return
- */
- Public Function backupdb ()
- {
- command to start Backup
- if (isset ($_post[' backup '))
- {
- Detects if a data table is selected
- if (Empty ($_post[' table ']))
- {
- Die ("Please select a data table.) ");
- }
- /** Start Backup **/
- $tables = Array ();
- $insert = Array ();
- $sql _statement = ";
- Lock the database that needs to be backed up to prevent read dirty data
- foreach ($_post[' table '] as $table)
- {
- mysql_query ("LOCK TABLE $table WRITE");
- Get the database structure
- $res = mysql_query (' SHOW CREATE TABLE '. $table. ');
- $createtable = mysql_result ($res, 0, 1);
- $str = "\ n". $createtable. " \ n ";
- Array_push ($tables, $STR);
- Querying 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 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";
- }
- Put the insert data in the array and go back to the
- 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);
- Generate a ZIP archive package
- $this->createzip ();
- /** Backup End **/
- Send an email to the specified mailbox, the attachment contains the SQL backup, if you set the word ^_^
- if (isset ($this->config[' email ') &&!empty ($this->config[' email '))
- {
- $this->sendemail ();
- }
- Output
- Echo '
Backup done. $this->datei. ' " > Download Backup
';
- }
- }
- /**
- * Send mail function
- * @return
- */
- protected function SendEmail ()
- {
- Read e-mail address
- foreach ($this->config[' email ') as $email)
- {
- $to = $email;
- $from = $this->config[' email '][0];
- $message _body = "The zip archive contained in this message is a database backup";
- $MSEP = Strtoupper (MD5 (Uniqid (Time ()));
- Set up an email header
- $header =
- "From: $from \ r \ n".
- "Mime-version:1.0\r\n".
- "Content-type:multipart/mixed; Boundary= ". $msep." \r\n\r\n ".
- "--$msep \ r \ n".
- "Content-type:text/plain\r\n".
- "Content-transfer-encoding:8bit\r\n\r\n".
- $message _body. "\ r \ n";
- Filename
- $dateiname = $this->datei;
- Compress Package Size
- $dateigroesse = FileSize ($dateiname);
- Reading a compressed package
- $f = fopen ($dateiname, "R");
- Save to Attachment
- $attached _file = fread ($f, $dateigroesse);
- To close a compressed package
- Fclose ($f);
- Create an attachment
- $attachment = Chunk_split (Base64_encode ($attached _file));
- Set 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\r\n".
- $attachment. "\ r \ n";
- Tag Attachment End Unknown
- $header. = "--$msep--";
- Message header
- $subject = "Database Backup";
- Send the message need to open PHP corresponding support Oh ^ ^
- if (Mail ($to, $subject, ', $header) = = FALSE)
- {
- Die ("Unable to send mail, please check email address");
- }
- echo "
Message sent successfully ";
- }
- }
- /**
- * Set up the database backup package and save it to the server specified directory
- * @return
- */
- protected function Createzip ()
- {
- folder permissions are sufficient
- chmod ($this->config[' folder '], 0777);
- Creating a compressed Package
- $zip = new Ziparchive ();
- Set file name for compressed package
- $this->datei = $this->config[' folder ']. $this->config[' MySQL '][3]. " _"
- . Date ("J_f_y_g_i_a"). ". Zip ";
- Let's see if the compression pack opens.
- if ($zip->open ($this->datei, ziparchive::create)!==true) {
- Exit ("Cannot open <". $this->datei. " >\n ");
- }
- Put the dump data in the compression pack.
- $zip->addfromstring ("Dump.sql", $this->dump);
- To close a compressed package
- $zip->close ();
- See if the compression pack is generated
- if (!file_exists ($this->datei))
- {
- Die ("Unable to generate a compressed package");
- }
- echo "
Database backup compression package successfully generated ";
- }
- /**
- * 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;
- }
- /**
- * Interface function to generate the selection data table
- * @return
- */
- Public Function Outputform ()
- {
- Select All
- $result = mysql_list_tables ($this->config[' MySQL '][3]);
- $buffer = '
-
- Select the data table you want to back up
';
- Echo $buffer;
- }
- }
- ?>
Copy Code |