Ver Mensaje Individual
  #4 (permalink)  
Antiguo 22/07/2019, 13:31
xoceunder
 
Fecha de Ingreso: junio-2012
Ubicación: En el Mundo
Mensajes: 759
Antigüedad: 11 años, 9 meses
Puntos: 10
Respuesta: crear un backup mysql con Illuminate

Cita:
Iniciado por hhs Ver Mensaje
Si no estas usando Laravel: http://phpbu.de/
Si estas usando laravel. https://github.com/spatie/laravel-backup

si estuve mirando pero quiero crear uno mismo para mi sistema

Yo uso esto
Código PHP:
Ver original
  1. //MySQL server and database
  2. $dbhost = 'localhost';
  3. $dbuser = 'my_user';
  4. $dbpass = 'my_pwd';
  5. $dbname = 'database_name';
  6. $tables = '*';
  7.  
  8. //Call the core function
  9. backup_tables($dbhost, $dbuser, $dbpass, $dbname, $tables);
  10.  
  11. //Core function
  12. function backup_tables($host, $user, $pass, $dbname, $tables = '*') {
  13.     $link = mysqli_connect($host,$user,$pass, $dbname);
  14.  
  15.     // Check connection
  16.     {
  17.         echo "Failed to connect to MySQL: " . mysqli_connect_error();
  18.         exit;
  19.     }
  20.  
  21.     mysqli_query($link, "SET NAMES 'utf8'");
  22.  
  23.     //get all of the tables
  24.     if($tables == '*')
  25.     {
  26.         $tables = array();
  27.         $result = mysqli_query($link, 'SHOW TABLES');
  28.         while($row = mysqli_fetch_row($result))
  29.         {
  30.             $tables[] = $row[0];
  31.         }
  32.     }
  33.     else
  34.     {
  35.         $tables = is_array($tables) ? $tables : explode(',',$tables);
  36.     }
  37.  
  38.     $return = '';
  39.     //cycle through
  40.     foreach($tables as $table)
  41.     {
  42.         $result = mysqli_query($link, 'SELECT * FROM '.$table);
  43.         $num_fields = mysqli_num_fields($result);
  44.         $num_rows = mysqli_num_rows($result);
  45.  
  46.         $return.= 'DROP TABLE IF EXISTS '.$table.';';
  47.         $row2 = mysqli_fetch_row(mysqli_query($link, 'SHOW CREATE TABLE '.$table));
  48.         $return.= "\n\n".$row2[1].";\n\n";
  49.         $counter = 1;
  50.  
  51.         //Over tables
  52.         for ($i = 0; $i < $num_fields; $i++)
  53.         {   //Over rows
  54.             while($row = mysqli_fetch_row($result))
  55.             {  
  56.                 if($counter == 1){
  57.                     $return.= 'INSERT INTO '.$table.' VALUES(';
  58.                 } else{
  59.                     $return.= '(';
  60.                 }
  61.  
  62.                 //Over fields
  63.                 for($j=0; $j<$num_fields; $j++)
  64.                 {
  65.                     $row[$j] = addslashes($row[$j]);
  66.                     $row[$j] = str_replace("\n","\\n",$row[$j]);
  67.                     if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
  68.                     if ($j<($num_fields-1)) { $return.= ','; }
  69.                 }
  70.  
  71.                 if($num_rows == $counter){
  72.                     $return.= ");\n";
  73.                 } else{
  74.                     $return.= "),\n";
  75.                 }
  76.                 ++$counter;
  77.             }
  78.         }
  79.         $return.="\n\n\n";
  80.     }
  81.  
  82.     //save file
  83.     $fileName = 'db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql';
  84.     $handle = fopen($fileName,'w+');
  85.     fwrite($handle,$return);
  86.     if(fclose($handle)){
  87.         echo "Done, the file name is: ".$fileName;
  88.         exit;
  89.     }
  90. }

pero ahora ando en otro sistema trabajando