Guest User

Untitled

a guest
Feb 23rd, 2012
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.89 KB | None | 0 0
  1. <?php
  2.  
  3.     backup_tables('host','username','password');
  4.  
  5.  
  6.     /* backup the db OR just a table */
  7.     function backup_tables($host,$user,$pass,$name,$tables = '*')
  8.     {
  9.  
  10.         $link = mysql_connect($host,$user,$pass);
  11.         mysql_select_db($name,$link);
  12.  
  13.         //get all of the tables
  14.         if($tables == '*')
  15.         {
  16.             $tables = array();
  17.             $result = mysql_query('SHOW DATABASES');
  18.             while($arr = mysql_fetch_array($result))
  19.             {
  20.                 if($arr[0] == 'information_schema' || $arr[0] == 'mysql')
  21.                     continue;
  22.                 $result2 = mysql_query('SHOW TABLES FROM '.$arr[0]);
  23.                 while($row = mysql_fetch_row($result2))
  24.                 {
  25.                     $tables[$arr[0]][] = $row[0];
  26.                 }
  27.             }
  28.         }
  29.         else
  30.         {
  31.             $tables = is_array($tables) ? $tables : explode(',',$tables);
  32.         }
  33.        
  34.        
  35.         /*$k=0;
  36.         foreach($tables as $database => $tab){
  37.             echo "<p style='font-weight: bold;text-transform: uppercase;'>$database</p><br/>";
  38.             print_r($tab);
  39.             echo '<br/>';
  40.         }*/
  41.        
  42.         //cycle through
  43.         mkdir('backup_'.date('Y-m-d'));
  44.         foreach($tables as $database => $tab){
  45.             $return = '';
  46.             foreach($tab as $table){
  47.                 $result = mysql_query('SELECT * FROM `'.$database.'`.`'.$table.'`');
  48.                 $num_fields = mysql_num_fields($result);
  49.  
  50.                 //$return.= 'DROP TABLE `'.$database.'`.`'.$table.'`;';
  51.                 $row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE `'.$database.'`.`'.$table.'`'));
  52.                 $return.= "\n\n".$row2[1].";\n\n";
  53.  
  54.                 for ($i = 0; $i < $num_fields; $i++)
  55.                 {
  56.                     while($row = mysql_fetch_row($result))
  57.                     {
  58.                         $return.= 'INSERT INTO `'.$database.'`.`'.$table.'` VALUES(';
  59.                         for($j=0; $j<$num_fields; $j++)
  60.                         {
  61.                             $row[$j] = addslashes($row[$j]);
  62.                             $row[$j] = ereg_replace("\n","\\n",$row[$j]);
  63.                             if($row[$j] == '')
  64.                                 $val = "NULL";
  65.                             else
  66.                                 $val = $row[$j];
  67.                             if (isset($row[$j])) { $return.= '"'.$val.'"' ; } else { $return.= 'NULL'; }
  68.                             if ($j<($num_fields-1)) { $return.= ','; }
  69.                         }
  70.                         $return.= ");\n";
  71.                     }
  72.                 }
  73.                 $return.="\n\n\n";
  74.             }
  75.             $handle = fopen('backup_'.date('Y-m-d').'/'.$database.'.sql','w+');
  76.             fwrite($handle,$return);
  77.             fclose($handle);
  78.         }
  79.  
  80.         //save file
  81.     }
  82. ?>
Advertisement
Add Comment
Please, Sign In to add comment