Php class implements full backup of the database, or the function of backing up the specified table in the database:
- Class Backup
- {
- /**
- * @ Var stores the options
- */
- Var $ config;
- /**
- * @ Var stores the final SQL dump
- */
- Var $ dump;
- /**
- * @ Var stores the table structure + inserts for every table
- */
- Var $ struktur = array ();
- /**
- * @ Var zip file name
- */
- Var $ datei;
- /**
- * This function is the constructor and phrase the options
- * And connect to the database
- * @ Return
- */
- Public function Backup ($ options)
- {
- // Write options
- Foreach ($ options AS $ name => $ value)
- {
- $ This-> config [$ name] = $ value;
- }
- // Check mysql connection
- 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 ());
- }
- /**
- * This function starts the backup progress its core function
- * @ Return
- */
- Public function backupDB ()
- {
- // Start backup
- If (isset ($ _ POST ['backup '])
- {
- // Check if tables are selected
- If (empty ($ _ POST ['table'])
- {
- Die ("Please select a table .");
- }
- /** Start backup **/
- $ Tables = array ();
- $ Insert = array ();
- $ SQL _statement = '';
- // Lock tables
- Foreach ($ _ POST ['table'] AS $ table)
- {
- Mysql_query ("lock table $ table WRITE ");
- // Read table structure
- $ Res = mysql_query ('Show CREATE Table'. $ TABLE .'');
- $ Createtable = mysql_result ($ res, 0, 1 );
- $ Str = "\ n". $ createtable. "\ n ";
- Array_push ($ tables, $ str );
- // Read table "inserts"
- $ 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 progress
- 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 "Inserts" into an array if not exists
- If (! In_array ($ SQL _statement, $ insert ))
- {
- Array_push ($ insert, $ SQL _statement );
- Unset ($ SQL _statement );
- }
- Unset ($ SQL _statement );
- }
- // Put table structure and inserts together in one var
- $ This-> struktur = array_combine ($ tables, $ insert );
- // Create full dump
- $ This-> createDUMP ($ this-> struktur );
- // Create zip file
- $ This-> createZIP ();
- /** End backup **/
- // Send an email with the SQL dump
- If (isset ($ this-> config ['email ']) &! Empty ($ this-> config ['email '])
- {
- $ This-> sendEmail ();
- }
- // Output
- Echo 'backup war erfolgreichdatei. '"> Download Backup
-
-
';
- }
- }
- /**
- * This function generate an email with attachment
- * @ Return
- */
- Protected function sendEmail ()
- {
- // Start sending emails
- Foreach ($ this-> config ['email '] AS $ email)
- {
- $ To = $ email;
- $ From = $ this-> config ['email '] [0];
- $ Message_body = "This email contains the database backup as a zip file .";
- $ Msep = strtoupper (md5 (uniqid (time ())));
- // Set email header (only text)
- $ 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;
- // Get filesize of zip file
- $ Dateigroesse = filesize ($ dateiname );
- // Open file to read
- $ F = fopen ($ dateiname, "r ");
- // Save content
- $ Attached_file = fread ($ f, $ dateigroesse );
- // Close file
- Fclose ($ f );
- // Create 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 ".
- $ Attachment. "\ r \ n ";
- // Mark end of attachment
- $ Header. = "-- $ msep --";
- // EMail Subject
- $ Subject = "Database Backup ";
- // Send email to emails ^
- If (mail ($ to, $ subject, '', $ header) = FALSE)
- {
- Die ("The email cocould not be sent. Please check the email address .");
- }
- Echo"
Email was successfully sent. ";
- }
- }
- /**
- * This function create the zip file with the database dump and save it on the ftp server
- * @ Return
- */
- Protected function createZIP ()
- {
- // Set permissions to 777
- Chmod ($ this-> config ['Folder'], 0777 );
- // Create zip file
- $ Zip = new ZipArchive ();
- // Create file name
- $ This-> datei = $ this-> config ['Folder']. $ this-> config ['mysql'] [3]. "_". date ("j_F_Y_g: I _a "). ". zip ";
- // Checking if file cocould be created
- If ($ zip-> open ($ this-> datei, ZIPARCHIVE: CREATE )! = TRUE ){
- Exit ("cannot open <". $ this-> datei. "> \ n ");
- }
- // Add mysql dump to zip file
- $ Zip-> addFromString ("dump. SQL", $ this-> dump );
- // Close file
- $ Zip-> close ();
- // Check whether file has been created
- If (! File_exists ($ this-> datei ))
- {
- Die ("The ZIP file cocould not be created .");
- }
- Echo"
The zip was created. ";
- }
- /**
- * This function create the full SQL dump
- * @ 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;
- }
- /**
- * This function displays the output form to select tables
- * @ Return
- */
- Public function outputForm ()
- {
- // Select all tables from database
- $ Result = mysql_list_tables ($ this-> config ['mysql'] [3]);
- $ Buffer ='
-
- Select some tables
-
-
';
- Echo $ buffer;
- }
- }
- ?>
Backup usage:
-
- // You can add as your email addresses as you like
- $ Options = array ('email '=> array ('email1', 'email2 '),
- 'Folder' => './backup /',
- 'Mysql' => array ('localhost', 'root', '***', 'database '));
- $ B = new Backup ($ options );
- // If submit form start backup
- If (isset ($ _ POST ['backup '])
- {
- // Start backup
- $ B-> backupDB ();
- }
- // Display tables
- $ B-> outputForm ();
- ?>
|