Google
Web dns.bdat.net

Re: [PHP-ES] Backup de Mysql

Write haof XML files: Vladimir Hernández ( interco@linuxbaja.org)
Fecha: mié 27 abr 2005 - 11:44:58 CEST


On Wed, 27 Apr 2005 11:11:00 +0200, marolijo wrote
> Hola, le quiero preparar a un cliente un script para que guarde
> backups de su MySQL sin darle acceso a MySqlAdmin, saben de alguna
> manera de hacerlo? se lo quiero incluir en el administrador web de
> la base de datos que yo he hecho a medida.
>
> Muchas gracias
>
> Pol Maresma i Oliveras // <mailto: pol@polnetwork.com> pol@polnetwork.com
> <http://www.polnetwork.com/> www.PolNetwork.com // Serveis
> d'Internet www.PolNetwork.com/tempslliure // Articles, opinions,
> imatges... Blog

Yo hago que el administrador que también hice a la medida corra el siguiente
script:

function tables($db_name){
  $table_sql = "SHOW TABLES FROM `".$db_name."`";
  $table_sal = mysql_query($table_sql);
  $cuantas = mysql_num_rows($table_sal);
  for ($i=0;$i<$cuantas;$i++){
    $temp_array = mysql_fetch_row($table_sal);
    $salida[$i] = $temp_array[0];
  }
  return $salida;
}
        
# Load table names and number of columns
$tables = tables($db_name);
   # Generating Backup Data
   $cuantos = count($tables);
   $salida = "";
   for ($i=0;$i<$cuantos;$i++){
     $db = init_db($db_name);
        $back_sql = "SELECT * FROM ".$tables[$i];
        $back_sal = mysql_query($back_sql);
        $rows = mysql_num_rows($back_sal);
        if($rows > 0){
          for ($j=0;$j<$rows;$j++){
            $cols = mysql_num_fields($back_sal);
            $cols_html = "";
            $temp_array = mysql_fetch_row($back_sal);
            $salida .= "INSERT INTO `".$tables[$i]."` VALUES (";
            for ($k=0;$k<$cols;$k++){
              if (is_numeric($temp_array[$k])){$field_value =
round($temp_array[$k]);}else{$field_value = $temp_array[$k];}
          # Esto es para evitar problemas con caracteres reservados
              $cols_html .= "
'".preg_replace("/'/","&#39",(preg_replace("/\"/","\\\"",addcslashes($field_value,"\0..\37!@\177..\377"))));
              if($k < ($cols-1)){$cols_html = $cols_html."',";}else{$cols_html
= $cols_html."'";}
            }
            $salida .= $cols_html.")\n";
          }
        }
   }
   $filename = "db_backup_".date(Y_m_d).".txt";
   $filename = "Content-Disposition: attachment; filename=".$filename;
   header("Content-Type: text/plain");
   header($filename);
   echo $salida;

--
Vladimir Hernández
http://linuxbaja.org
Linux registered user # 374079
-- 
PHP Spanish Localization Talk Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Este archivo fue generado por hypermail 2.1.7 : sáb 18 mar 2006 - 18:49:33 CET