Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.65 KB | None | 0 0
  1. function backup_bd($host,$utilizador,$password,$nome,$tabelas = '*')
  2. {
  3.  
  4.     $link = mysql_connect($host,$utilizador,$password);
  5.     mysql_select_db($nome,$link);
  6.  
  7.     if ($tabelas == '*')
  8.     {
  9.         $tabelas = array();
  10.         $resultado = mysql_query('SHOW TABLES');
  11.         while($coluna = mysql_fetch_row($resultado))
  12.         {
  13.             $tabelas[] = $coluna[0];
  14.         }
  15.     }
  16.     else
  17.     {
  18.         $tabelas = is_array($tabelas) ? $tabelas: explode(',',$tabelas);
  19.     }
  20.  
  21.     foreach($tabelas as $tabelas)
  22.     {
  23.         $resultado = mysql_query('SELECT * FROM '.$tabelas);
  24.         $num_campos = mysql_num_fields($resultado);
  25.  
  26.         $return.= 'DROP TABLE '.$tabelas.';';
  27.         $coluna2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$tabelas));
  28.         $return.= "\n\n".$coluna2[1].";\n\n";
  29.  
  30.         for ($i = 0; $i < $num_campos; $i++)
  31.         {
  32.             while($coluna = mysql_fetch_row($resultado))
  33.             {
  34.                 $return.= 'INSERT INTO '.$tabelas.' VALUES(';
  35.                 for($j=0; $j<$num_campos; $j++)
  36.                 {
  37.                     $coluna[$j] = addslashes($row[$j]);
  38.                     $coluna[$j] = ereg_replace("\n","\\n",$coluna[$j]);
  39.                     if (isset($coluna[$j])) { $return.= '"'.$coluna[$j].'"' ; } else { $return.= '""'; }
  40.                     if ($j<($num_campos-1)) { $return.= ','; }
  41.                 }
  42.                 //$return.= ");\n";
  43.             }
  44.             echo $return;
  45.         }
  46.         $return.="\n\n\n";
  47.     }
  48.    
  49.     $ficheiro = fopen('bd-backup-'.time().'-'.(md5(implode(',',$tabelas))).'.sql','w+');
  50.     fwrite($ficheiro,$return);
  51.     fclose($ficheiro);
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement