-
- error_reporting(0);//Eliminate all evil php alarm prompts
- //Set email address
- $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();
- }
- // Display backup Table
- $b->outputForm();
- ?>
Copy code
PHP database backup class implementation code:
-
- /**
- * Back up mysql database
- * edit: bbs.it-home.org
- */
- class Backup
- {
- /**
- * @var is used to save configuration parameters
- */
- var $config;
-
- /**
- * @var is used to save the data of mysql dump
- */
- var $ dump;
-
- /**
- * @var is used for database result data and insert instructions
- */
- var $struktur = array();
-
- /**
- * @var compressed file name zip
- */
- var $datei;
-
- /**
- * Structural function
- * Connect to database
- * @return
- */
- public function Backup ($options)
- {
- //Read configuration from formal parameters
- 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());
- }
-
- /**
- * Function to execute backup database process
- * @return
- */
- public function backupDB()
- {
- //Command to start backup
- if(isset($_POST['backup']))
- {
- // Check whether the 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 reading 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 = "nn".$createtable."nn";
-
- array_push($tables, $str);
-
- // Query all data rows in the 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 into 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";
- }
-
- // insert data Put it in the array and remove duplicates
- if(!in_array($sql_statement, $insert))
- {
- array_push($insert, $sql_statement);
- unset($sql_statement);
- }
-
- unset($sql_statement);
-
- }
-
- // Put the database structure and insert command together
- $this->struktur = array_combine($tables, $insert);
-
- // Execute the dump function
- $this->createDUMP($this ->struktur);
-
- // Generate zip archive
- $this->createZIP();
-
- /**End of backup **/
-
- // Send an email to the specified email address, the attachment contains the sql backup , if you set it ^_^
- if(isset($this->config['email']) && !empty($this->config['email']))
- {
- $this-> ;sendEmail();
- }
-
- // output
- echo '
Backup completedDownload backup
';
- }
- }
-
- /**
- * Send email function
- * @return
- */
- protected function sendEmail()
- {
- // Read 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 email header
- $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";
-
- // File name
- $dateiname = $this->datei;
-
- / / Compressed package size
- $dateigroesse = filesize ($dateiname);
-
- // Read compressed package
- $f = fopen ($dateiname, "r");
- // Save to 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 attachment end unknown
- $header .= "--$msep-- ";
-
- // Email title
- $subject = "Database Backup";
-
- // Sending emails requires enabling PHP support ^^
- if(mail($to, $subject, '', $header) == FALSE)
- {
- die("Unable to send email, please check your email address");
- }
-
- echo "
Email sent successfully ";
- }
- }
-
- /**
- * Create a compressed package of database backup and save it to the specified directory on the server
- * @return
- */
- protected function createZIP()
- {
-
- // Folder permissions must be sufficient
- chmod($this->config['folder'], 0777);
-
- // Create 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";
-
- // See if the compressed package can be opened
- if ($zip->open($this->datei, ZIPARCHIVE ::CREATE)!==TRUE) {
- exit("Cannot open<".$this->datei.">n");
- }
-
- // Put the dumped data into 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 "
Database backup compressed package successfully generated";
- }
-
- /**
- * mysql dump function
- * @param object $dump
- * @return
- */
- protected function createDUMP($dump)
- {
- $date = date("F j, Y, g:i a");
-
- $header = <<
Copy code
|