2016-08-08 4 views
6

नोट: मुख्य बात जो मैं जानना चाहता हूं यह है कि PHP का उपयोग करके स्ट्रिंग करने के लिए पूर्णांक और बिट्स को परिवर्तित करना बंद करना है। मैं mysqldump का उपयोग नहीं करना चाहता क्योंकि कई सर्वर गोले तक पहुंच नहीं दे रहे हैं और मैंने इसका परीक्षण किया था। यही कारण है कि, मैं PHPMySQL डेटा बैकअप स्ट्रिंग

मैं अपने datbases https://davidwalsh.name/backup-mysql-database-php से जोड़ने के लिए समस्या है जब मैं फ़ाइल जो मैं बैकअप के रूप में मिलता है खोलने कि संदर्भ के साथ लाइव सर्वर पर कर रहे हैं का बैकअप ले रहा हूँ उपयोग कर रहा हूँ, मैं पूरे देखा डेटा स्ट्रिंग में परिवर्तित किया जाता है। तो, मान लीजिए कि मेरे पास तारीख में शून्य है, इसे 0000-00-00 के रूप में दिनांक बनाने में परिवर्तित हो जाता है, बिट मान 0 में परिवर्तित हो जाता है।

मैंने mysqldump का उपयोग किया और इसमें ऐसे मुद्दे भी हैं जिन्हें मैंने अन्य प्रश्न पर रखा है: mysqldump working on local but not on godaddy server

समारोह मैं उपयोग कर रहा हूँ इस प्रकार है:

function Export_Database($host,$user,$pass,$name, $tables, $backup_name=false) 
    { 
     $mysqli = new mysqli($host,$user,$pass,$name); 
     $mysqli->select_db($name); 
     $mysqli->query("SET NAMES 'utf8'"); 
     foreach($tables as $table) 
     { 
      $result   = $mysqli->query('SELECT * FROM '.$table); 
      $fields_amount = $result->field_count; 
      $rows_num=$mysqli->affected_rows;  
      $res   = $mysqli->query('SHOW CREATE TABLE '.$table); 
      $TableMLine  = $res->fetch_row(); 
      $content  = (!isset($content) ? '' : $content) . "\n\n".$TableMLine[1].";\n\n"; 

      for ($i = 0, $st_counter = 0; $i < $fields_amount; $i++, $st_counter=0) 
      { 
       while($row = $result->fetch_row()) 
       { //when started (and every after 100 command cycle): 
        if ($st_counter%100 == 0 || $st_counter == 0) 
        { 
          $content .= "\nINSERT INTO ".$table." VALUES"; 
        } 
        $content .= "\n("; 
        for($j=0; $j<$fields_amount; $j++) 
        { 
         $row[$j] = str_replace("\n","\\n", addslashes($row[$j])); 
         if (isset($row[$j])) 
         { 
          $content .= '"'.$row[$j].'"' ; 
         } 
         else 
         { 
          $content .= '""'; 
         }  
         if ($j<($fields_amount-1)) 
         { 
           $content.= ','; 
         }  
        } 
        $content .=")"; 
        //every after 100 command cycle [or at last line] ....p.s. but should be inserted 1 cycle eariler 
        if ((($st_counter+1)%100==0 && $st_counter!=0) || $st_counter+1==$rows_num) 
        { 
         $content .= ";"; 
        } 
        else 
        { 
         $content .= ","; 
        } 
        $st_counter=$st_counter+1; 
       } 
      } $content .="\n\n\n"; 
     } 
     $folder = 'DB_Backup/'; 
     if (!is_dir($folder)) 
     mkdir($folder, 0777, true); 
     chmod($folder, 0777); 

     $date = date('m-d-Y-H-i-s', time()); 
     $filename = $folder."db-backup-".$date; 

     $handle = fopen($filename.'.sql','w+'); 
     fwrite($handle,serialize($content)); 
     fclose($handle); 
    } 
+0

मैं mysqldump इस्तेमाल किया के लिए लिंक का उपयोग किया है, लेकिन इसके केवल स्थानीय पर ठीक काम कर रहा। सर्वर शैल @ e4c5 –

+0

तक पहुंच नहीं दे रहे हैं, मैं एक ऐसे प्रोजेक्ट पर काम कर रहा हूं जहां मुझे उपयोगकर्ता को जब भी वह डेटाबेस बैकअप लेना चाहता है, तो उसे एक्सेस करने की आवश्यकता होती है, वह उसे कुछ बटन @ e4c5 –

+0

हाँ पर क्लिक करके अपनी वेबसाइट पर ले जा सकता है .. लेकिन यह सर्वर पर काम नहीं कर रहा है। @ e4c5 –

उत्तर

1

मैं निम्नलिखित तरीके का उपयोग कर हल किया। इधर, $ टेबल है और अपने database.I की तालिकाओं की सरणी संदर्भ http://php.net/manual/en/mysqli-result.fetch-field-direct.php

function Export_Database($host,$user,$pass,$name, $tables, $backup_name=false) 
     { 
      //value greater than 250 are strings 
      $mysql_data_type_hash = array(
       1=>'tinyint', 
       2=>'smallint', 
       3=>'int', 
       4=>'float', 
       5=>'double', 
       7=>'timestamp', 
       8=>'bigint', 
       9=>'mediumint', 
       10=>'date', 
       11=>'time', 
       12=>'datetime', 
       13=>'year', 
       16=>'bit', 
       //252 is currently mapped to all text and blob types (MySQL 5.0.51a) 
       253=>'varchar', 
       254=>'char', 
       246=>'decimal' 
      ); 


      $mysqli = new mysqli($host,$user,$pass,$name); 
      $mysqli->select_db($name); 
      $mysqli->query("SET NAMES 'utf8'"); 
      foreach($tables as $table) 
      { 
       $result   = $mysqli->query('SELECT * FROM '.$table); 
       $fields_amount = $result->field_count; 
       $rows_num=$mysqli->affected_rows;  
       $res   = $mysqli->query('SHOW CREATE TABLE '.$table); 
       $TableMLine  = $res->fetch_row(); 
       $content  = (!isset($content) ? '' : $content) . "\n\n".$TableMLine[1].";\n\n"; 

       for ($i = 0, $st_counter = 0; $i < $fields_amount; $i++, $st_counter=0) 
       { 
        while($row = $result->fetch_row()) 
        { 

       //when started (and every after 100 command cycle): 
         if ($st_counter%100 == 0 || $st_counter == 0) 
         { 
           $content .= "\nINSERT INTO ".$table." VALUES"; 
         } 
         $content .= "\n("; 
         for($j=0; $j<$fields_amount; $j++) 
         { 
          $datatype=$result->fetch_field_direct($j)->type; 
          //echo $mysql_data_type_hash[$datatype]."<br/>"; 
          $row[$j] = str_replace("\n","\\n", addslashes($row[$j])); 
          if (isset($row[$j]) && trim($row[$j])!=null) 
          { 
           if(($datatype>=10 && $datatype<=13) || $datatype>250) 
           { 
            $content .= '"'.$row[$j].'"' ; 
           } 
           else 
           { 
            $content .= $row[$j] ; 
           } 
          } 
          else 
          { 
           $content .= 'NULL'; 
          }  
          if ($j<($fields_amount-1)) 
          { 
            $content.= ','; 
          }  
         } 
         $content .=")"; 
         //every after 100 command cycle [or at last line] ....p.s. but should be inserted 1 cycle eariler 
         if ((($st_counter+1)%100==0 && $st_counter!=0) || $st_counter+1==$rows_num) 
         { 
          $content .= ";"; 
         } 
         else 
         { 
          $content .= ","; 
         } 
         $st_counter=$st_counter+1; 
        } 
       } $content .="\n\n\n"; 
      } 

      $folder = 'DB_Backup/'; 
      if (!is_dir($folder)) 
      mkdir($folder, 0777, true); 
      chmod($folder, 0777); 

      $date = date('m-d-Y-H-i-s', time()); 
      $filename = $folder."db-backup-".$date; 

      //Commenting The Warning Given By Mysql When Taking Database Backup 
      $content=str_replace("Warning","-- Warning",$content); 

      $handle = fopen($filename.'.sql','w+'); 
      fwrite($handle,$content); 
      fclose($handle); 
     } 
संबंधित मुद्दे