Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2012
482
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.                 $result2 = mysql_query('SHOW TABLES FROM '.$arr[0]);
  21.                 while($row = mysql_fetch_row($result2))
  22.                 {
  23.                     $tables[$arr[0]][] = $row[0];
  24.                 }
  25.             }
  26.         }
  27.         else
  28.         {
  29.             $tables = is_array($tables) ? $tables : explode(',',$tables);
  30.         }
  31.        
  32.         /*A little middle check, to see if all is saved in the right way*/
  33.         /*$k=0;
  34.         foreach($tables as $database => $tab){
  35.             echo "<p style='font-weight: bold;text-transform: uppercase;'>$database</p><br/>";
  36.             print_r($tab);
  37.             echo '<br/>';
  38.         }*/
  39.        
  40.         //cycle through
  41.         mkdir('backup_'.date('Y-m-d'));
  42.         foreach($tables as $database => $tab){
  43.             $return = '';
  44.             foreach($tab as $table){
  45.                 $result = mysql_query('SELECT * FROM `'.$database.'`.`'.$table.'`');
  46.                 $num_fields = mysql_num_fields($result);
  47.  
  48.                 //$return.= 'DROP TABLE `'.$database.'`.`'.$table.'`;';
  49.                 $row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE `'.$database.'`.`'.$table.'`'));
  50.                 $return.= "\n\n".$row2[1].";\n\n";
  51.  
  52.                 for ($i = 0; $i < $num_fields; $i++)
  53.                 {
  54.                     while($row = mysql_fetch_row($result))
  55.                     {
  56.                         $return.= 'INSERT INTO `'.$database.'`.`'.$table.'` VALUES(';
  57.                         for($j=0; $j<$num_fields; $j++)
  58.                         {
  59.                             $row[$j] = addslashes($row[$j]);
  60.                             $row[$j] = ereg_replace("\n","\\n",$row[$j]);
  61.                             if($row[$j] == '')
  62.                                 $val = "NULL";
  63.                             else
  64.                                 $val = $row[$j];
  65.                             if (isset($row[$j])) { $return.= '"'.$val.'"' ; } else { $return.= 'NULL'; }
  66.                             if ($j<($num_fields-1)) { $return.= ','; }
  67.                         }
  68.                         $return.= ");\n";
  69.                     }
  70.                 }
  71.                 $return.="\n\n\n";
  72.             }
  73.             $handle = fopen('backup_'.date('Y-m-d').'/'.$database.'.sql','w+');
  74.             fwrite($handle,$return);
  75.             fclose($handle);
  76.         }
  77.  
  78.         //save file
  79.     }
  80. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement