Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [Klasa] Backup bazy danych
infoo1
post
Post #1





Grupa: Zarejestrowani
Postów: 117
Pomógł: 0
Dołączył: 12.11.2008

Ostrzeżenie: (0%)
-----


Szukam klasy do backupu bazy danych. Mam takie coś, ale to backupuje same tabele, bez danych:
  1. <?php
  2.      error_reporting(E_ALL);
  3.      ini_set('display_errors',1);
  4.  
  5.      $user        =    'root';
  6.      $pass        =    '';
  7.      $name        =    'a';
  8.      $host        =    'localhost';
  9.  
  10.      if($user  & $pass)
  11.      {
  12.           //dbDump($user, $name, $pass);
  13.      }
  14.  
  15.      function dbconnect($dbuser, $dbuserpass, $dbname)
  16.      {
  17.          global $host;
  18.          $dbhandle = mysql_connect($host, $dbuser, $dbuserpass) or die ("Nie można się połączyć.");
  19.          mysql_select_db ($dbname, $dbhandle) or die ("Nie można wybrać bazy danych.");
  20.          return $dbhandle;
  21.      }
  22.  
  23.      function dbdisconnect($dbhandle){
  24.  
  25.          if(!mysql_close($dbhandle))
  26.          {
  27.              return false;
  28.          }
  29.          else
  30.          {
  31.              return true;
  32.          }
  33.      }
  34.  
  35.      function dbdump($user, $dbname, $pass)
  36.      {
  37.          $path =  "dump_base_" . date('d-m-y_H-i') . ".sql";
  38.          $export = '';   // for dump tables and db
  39.          $data = '';     // for dump tables' data
  40.          $handle = dbconnect($user, $pass, $dbname);
  41.  
  42.          $q0 = "SELECT DATABASE()";
  43.          $res0 = mysql_query($q0,$handle);
  44.          $baza = mysql_fetch_row($res0);
  45.          $baza = $baza[0];
  46.  
  47.          $q1 = "SHOW TABLES";
  48.          $res1 = mysql_query($q1,$handle);
  49.  
  50.          // $export .= "\nCREATE DATABASE `".$baza."`;\n";
  51.          $export .= "USE $baza;";
  52.  
  53.          // foreach table
  54.          while($row = mysql_fetch_row($res1))
  55.          {
  56.              $tabela = $row[0];
  57.  
  58.              $q2 = "SHOW CREATE TABLE ".$tabela;
  59.              $res2 = mysql_query($q2,$handle);
  60.              if($row2 = mysql_fetch_row($res2))
  61.              {
  62.                  $tmp = $row2[1];
  63.                  $export.= $tmp;
  64.                  $export.=";";
  65.              }
  66.  
  67.              $q3 = "SELECT * FROM $tabela";
  68.              $res3 = mysql_query($q3,$handle);
  69.  
  70.              // foreach row in table
  71.              while ($row3 = mysql_fetch_array($res3))
  72.              {
  73.                  $data .= "INSERT INTO `".$tabela."` VALUES (";
  74.                  $ile = mysql_num_fields($res3);
  75.  
  76.                  for($i = 0;$i<$ile;$i++){
  77.  
  78.                      $tmp = preg_replace("/'/", "'", $row3[$i]);
  79.                      $row3[$i] = $tmp;
  80.                      $data .= "'".$row3[$i]."'";
  81.                      if($i < $ile -1)
  82.                      {
  83.                          $data .= ", ";
  84.                      }
  85.                  }
  86.                  $data .= ");\n";
  87.              }
  88.          }
  89.  
  90.          $export .= $data;
  91.          $buf1 = str_replace("ENGINE",  "TYPE", $export);
  92.          $buf2 = preg_replace("/DEFAULT.CHARSET=.*;/",  ";", $buf1);
  93.  
  94.          dbdisconnect($handle);
  95.  
  96.          // save all to file
  97.          $file=fopen($path,'wb');
  98.  
  99.          if(!$file)
  100.          {
  101.              return false;
  102.          }
  103.  
  104.          $result_end = fwrite($file, $buf2);
  105.  
  106.          fclose($file);
  107.  
  108.          echo "<a href=$path>Plik bazy: $path</>";
  109.  
  110.          return $result_end;
  111.      }
  112.  
  113.      dbdump($user, $name, $pass);
  114.  
  115.  ?>


---
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 1)
skowron-line
post
Post #2





Grupa: Zarejestrowani
Postów: 4 340
Pomógł: 542
Dołączył: 15.01.2006
Skąd: Olsztyn/Warszawa

Ostrzeżenie: (0%)
-----


http://letmegooglethatforyou.com/?q=backup+mysql+php+class
http://www.phpclasses.org/browse/package/1560.html


--------------------
I'm so fast that last night I turned off the light switch in my hotel room and was in bed before the room was dark - Muhammad Ali.
Peg jeżeli chcesz uprawiać sex to dzieci muszą wyjść, a jeżeli chcesz żeby był dobry ty też musisz wyjść - Al Bundy.

QueryBuilder, Mootools.net, bbcradio1::MistaJam
http://www.phpbench.com/
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 20.08.2025 - 14:23