Re: ayuda respaldo bd
| From: | Ivan Rodriguez | Date: | Thu, 25 Mar 2004 11:39:47 +0000 |
| Subject: | Re: ayuda respaldo bd | ||
| References: | 1 | Groups: | php.general.es |
| Request: | Send a blank email to general-es+get-47102@lists.php.net to get a copy of this message | ||
Buenas, si puedes hacer correr scripts periodicamente ... hazte un script
que realice el backup, yo uso algo como:
class BBDD
{
var $_SERVIDOR;
var $_USUARIO;
var $_PASSWORD;
var $_CONEXION;
var $_SQL;
var $gui;
function BBDD($servidor, $usuario, $password)
{
$this->_SERVIDOR = $servidor;
$this->_USUARIO = $usuario;
$this->_PASSWORD = $password;
$this->gui = array();
}
function DUMP_Estructura($db, $table)
{
$crlf = "\n";
mysql_select_db($db, $this->_CONEXION);
$schema_create .= 'CREATE TABLE ' . $table . ' (' . $crlf;
$local_query = 'SHOW FIELDS FROM ' . $db . '.' . $table;
$result = mysql_query($local_query);
while($row = mysql_fetch_array($result))
{
$schema_create .= ' ' . $row['Field'] . ' ' .
$row['Type'];
if(isset($row['Default']) && $row['Default'] !=
'')
{
$schema_create .= ' DEFAULT \'' . $row['Default'] .
'\'';
}
if($row['Null'] != 'YES')
{
$schema_create .= ' NOT NULL';
}
if($row['Extra'] != '')
{
$schema_create .= ' ' . $row['Extra'];
}
$schema_create .= ',' . $crlf;
}
mysql_free_result($result);
$schema_create = ereg_replace(',' . $crlf . '$', '',
$schema_create);
$local_query = 'SHOW KEYS FROM ' . $db . '.' . $table;
$result = mysql_query($local_query);
while($row = mysql_fetch_array($result))
{
$kname = $row['Key_name'];
$comment = (isset($row['Comment'])) ? $row['Comment'] :
'';
$sub_part = (isset($row['Sub_part'])) ? $row['Sub_part'] :
'';
if ($kname != 'PRIMARY' && $row['Non_unique'] == 0)
{
$kname = "UNIQUE|$kname";
}
if ($comment == 'FULLTEXT')
{
$kname = 'FULLTEXT|$kname';
}
if (!isset($index[$kname]))
{
$index[$kname] = array();
}
if ($sub_part > 1)
{
$index[$kname][] = $row['Column_name'] . '(' . $sub_part .
')';
}
else
{
$index[$kname][] = $row['Column_name'];
}
}
mysql_free_result($result);
while (list($x, $columns) = @each($index))
{
$schema_create .= ',' . $crlf;
if($x == 'PRIMARY')
{
$schema_create .= ' PRIMARY KEY (';
}
elseif(substr($x, 0, 6) == 'UNIQUE')
{
$schema_create .= ' UNIQUE ' . substr($x, 7) . ' (';
}
elseif(substr($x, 0, 8) == 'FULLTEXT')
{
$schema_create .= ' FULLTEXT ' . substr($x, 9) . ' (';
}
else
{
$schema_create .= ' KEY ' . $x . ' (';
}
$schema_create .= implode($columns, ', ') . ')';
}
$schema_create .= $crlf . ')' . $crlf;
return $schema_create;
}
function DUMP_Estructura_Datos($db, $table)
{
$local_query = 'SELECT * FROM ' . $db . '.' . $table;
$result = mysql_query($local_query);
$current_row = 0;
$fields_cnt = mysql_num_fields($result);
$rows_cnt = mysql_num_rows($result);
while ($row = mysql_fetch_row($result))
{
$current_row++;
$table_list = '(';
for ($j = 0; $j < $fields_cnt; $j++)
{
$table_list .= mysql_field_name($result, $j) . ', ';
}
$table_list = substr($table_list, 0, -2);
$table_list .= ')';
if (isset($GLOBALS['extended_ins']) && $current_row > 1)
{
$schema_insert = '(';
}
else
{
$schema_insert .= 'INSERT INTO ' . $table . ' VALUES (';
$is_first_row = FALSE;
}
for ($j = 0; $j < $fields_cnt; $j++)
{
if (!isset($row[$j]))
{
$schema_insert .= ' NULL, ';
}
elseif($row[$j] == '0' || $row[$j] != '')
{
$type = mysql_field_type($result, $j);
if ($type == 'tinyint' || $type == 'smallint' || $type
== 'mediumint' || $type == 'int' ||
$type == 'bigint' ||$type == 'timestamp') {
$schema_insert .= $row[$j] . ', ';
}
else
{
$dummy = '';
$srcstr = $row[$j];
for ($xx = 0; $xx < strlen($srcstr); $xx++)
{
$yy = strlen($dummy);
if ($srcstr[$xx] == '\\') $dummy .= '\\\\';
if ($srcstr[$xx] == '\'') $dummy .=
'\\\'';
if ($srcstr[$xx] == "\x00") $dummy .= '\0';
if ($srcstr[$xx] == "\x0a") $dummy .= '\n';
if ($srcstr[$xx] == "\x0d") $dummy .= '\r';
if ($srcstr[$xx] == "\x1a") $dummy .= '\Z';
if (strlen($dummy) == $yy) $dummy .=
$srcstr[$xx];
}
$schema_insert .= "'" . $dummy . "', ";
}
}
else
{
$schema_insert .= "'', ";
}
}
$schema_insert = ereg_replace(', $', '', $schema_insert);
$schema_insert .= ')' . "\n";
}
return $schema_insert;
}
}
Luego trabaja con el objeto y listo :-)
Espero que te sirvar, yo esto lo use para hacer un phpMyAdmin via grafico
con PHP-GTK, GMySQLAdmin, jeje si alguien lo quiere que me lo diga :-)
Un saludo
Iván Rodriguez Espada
_________________________
ALAPLAYA.COM
http://www.alaplaya.com
epplestun@alaplaya.com
----- Original Message -----
From: "Francisco Rivas Flores" <Kryogen@teleline.es>
To: <php-es@lists.php.net>
Sent: Thursday, March 25, 2004 12:21 PM
Subject: RV: [PHP-ES] ayuda respaldo bd
>
> Y rizando el rizo...
>
> Tenemos un servidor web y otro mysql. En el web tenemos todos los permisos
> menos la ejecución de comandos por consola y en el de mysql solo podemos
> acceder en modo local mediante scripts ejecutados en el servidor web, y no
> tnemos permiso de escritura en disco. Queremos hacer un backup periódico
(el
> servidor web ofrece la ejecución de scripts periodicos).
> ¿Cómo haríamos un backup periódico que se almacenara en nuestro espacio
> dentro del servidor web?
>
> No parece el enunciado de un problema? =)
> Un saludo
>
> F.Rivas
>
> -----Mensaje original-----
> De: Manuel González Noriega [mailto:manuel@simplelogica.net]
> Enviado el: jueves, 25 de marzo de 2004 10:26
> Para: phpes
> Asunto: Re: [PHP-ES] ayuda respaldo bd
>
>
> El jue, 25-03-2004 a las 09:41, Irantzu Leunda Biskarret escribió:
> > Tengo una aplicación hecha en PHP, con BD en MySQL, pues bien, necesito
> > cada cierto tiempo, puede ser una o dos veces a la semana respaldar esa
> > base de datos.
> > Quería saber si hay alguien que haya hecho algo parecido y que me pueda
> > orientar o darme algún ejemplo.
>
> Es bastante facil, se hace por línea de comandos con la utilidad
> mysqldump
>
> mysqldump -h tuhost.com -u tuusuario -ptucontraseña tubasededatos
> opcionalmentetutabla > tufichero.sql
>
>
> Para automatizar la tarea, escribes un script mod_php para ejecutarse a
> través de wget o lynx o bien un script php-cli para ejecución por bash y
> utilizas crontab para que se ejecute cuando tú quieras.
>
>
>
>
> --
> Manuel González Noriega
> Simplelógica, construcción web
> URL: http://simplelogica.net
> EMAIL: simplelogica@simplelogica.net
> TELEFONO: (+34) 985 22 12 65
>
> Logicola es el weblog de Simplelógica
> http://simplelogica.net/logicola/
> </p>That's right. We said Frontpage.</p>
>
> --
> PHP Spanish Localization Talk Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
> --
> PHP Spanish Localization Talk Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>