Bueno hoy viendo el post de siquillote, he decidido crear un backup.
Versión v0.2
Características:
- Comprimir a ZIP.
- Sacar directamente en archivo en SQL.
- Mostrar en la misma pantalla.
- Crear instalador
Instalación: (Creamos el savesql.php o otro nombre finalizando en .php )
Editamos las lineas 2,3,4,5.
Esto exactamente
$host = "localhost"; // Tu hosting para MySQL
$user = "root"; // Tu usuario de MySQL
$pass = ""; // Tu password de MySQL
<?php
$host = "localhost";
$user = "root";
$pass = "";
if ($_POST)
{
$bd = $_POST['db'];
$compresion = $_POST['compresion'];
mysql_connect($host,$user,$pass);
mysql_select_db($bd);
$qry_filas = mysql_query("SHOW TABLES FROM ".$bd);
while ($fila = mysql_fetch_array($qry_filas, MYSQL_NUM))
{
$tablas[] = $fila[0];
}
ob_start();
print_r($tablas);
$representacion = ob_get_contents();
ob_end_clean();
preg_match_all('/(\[\d+\] => .*)\n/', $representacion, $matches);
$save_tablas = implode("; ", $matches[1]);
if ($_POST['compresion'] == "ePHP")
{
$dump = "<?php";
}
if ($_POST['compresion'] == "ePHP")
{
$dump .= "// SaveSQL v0.2 - PHP Install Generator
// Copyright 2010 by JaviPilo for PHPeros
// Generated install of '".$bd."'
mysql_connect('".$host."','".$user."','".$pass."');
mysql_select_db('".$bd."');
";
} else {
$dump ="-- SaveSQL v0.2
-- http://ww.phperos.net/
--
-- Servidor: ".$_SERVER['HTTP_HOST']."
-- Tiempo de generación: ".date("d-m-Y")." a las ".date("H:i:s")."
-- Versión del servidor: ".mysql_get_server_info()."
-- Versión de PHP: ".phpversion()."
--
-- Base de datos: `".$bd."`
--
-- --------------------------------------------------------";
}
foreach ($tablas as $tabla) {
$create_table = "";
$respuesta = mysql_query("SHOW CREATE TABLE ".$tabla);
while ($fila = mysql_fetch_array($respuesta, MYSQL_NUM)) {
$create_table .= $fila[1].";";
}
$insert = "";
$inserts = mysql_query("SELECT * FROM ".$tabla);
while ($fila = mysql_fetch_array($inserts, MYSQL_ASSOC))
{
$columnas = array_keys($fila);
foreach ($columnas as $columna) {
if ( gettype($fila[$columna]) == "NULL" ) {
$values[] = "NULL";
} else {
$values[] = "'".mysql_real_escape_string($fila[$columna])."'";
}
}
$insert .= "INSERT INTO `$tabla` VALUES (".implode(", ", $values).");\n";
unset($values);
}
if ($_POST['compresion'] == "ePHP")
{
if ($create_table)
{
$dump .='
mysql_query("'.$create_table.'");
';
}
if ($insert)
{
$dump .='
mysql_query("'.$insert.'");
';
}
} else {
if ($create_table)
{
$dump .="
--
-- Estructura de tabla para la tabla `".$tabla."`
--
".$create_table."
";
}
if ($insert)
{
$dump .= "
--
-- Volcar la base de datos para la tabla `".$tabla."`
--
".$insert."
-- --------------------------------------------------------
";
}
}
}
if ($_POST['compresion'] == "ePHP")
{
$dump .= "
?>";
}
if ($_POST['compresion'] == "ePHP")
{
$nombre = "instalador_".$bd.".php";
} else {
$nombre = "Backup_".$bd."_".date("d_m_Y_H_i_s").".sql";
}
if ( !headers_sent() ) {
header("Pragma: no-cache");
header("Expires: 0");
header("Content-Transfer-Encoding: binary");
switch ($compresion) {
case "zip":
header("Content-Disposition: attachment; filename=".$nombre.".zip");
header("Content-type: application/x-zip");
echo gzencode($dump, 9);
break;
case "PHP":
echo $dump;
break;
case "ePHP":
header("Content-Disposition: attachment; filename=".$nombre);
header("Content-type: application/force-download");
echo $dump;
break;
default:
header("Content-Disposition: attachment; filename=".$nombre);
header("Content-type: application/force-download");
echo $dump;
}
}
} else {
print("<form method='POST'>
<input type='text' name='db'><br>
<select size='1' name='compresion'>
<option selected value='zip'>Comprimir en ZIP</option>
<option value='sql'>Guardar en formato SQL</option>
<option value='PHP'>Mostrar aquí en el PHP</option>
<option value='ePHP'>Crear instalador</option>
</select>
<input type='submit' value='Generar'>
</form>");
print("<p><b><font size='2' face='verdana'>Copyright 2010 by JaviPilo para PHPNacion</font></b></p>");
}
?>