Jump to content


Photo

Link De Backup Do Banco De Dados


  • Faça o login para participar
3 replies to this topic

#1 Fernando Wobeto

Fernando Wobeto

    webmaster

  • Usuários
  • 341 posts
  • Sexo:Masculino

Posted 28/09/2004, 11:28

Pessoal,

tem como eu criar tipo um link na administração do meu site que quando clicasse abrisse meu banco de dados em forma de texto para eu salvar e fazer backup?

Tipo como no phpmyadmin quando vc pede para exportar em texto e ele mostra todo o bd com a estrutura e os dados.

Obrigado (y)
|--------------------------------------------------|
| MSN: fernando_wobeto@hotmail.com
| E-MAIL: fernandowobeto@gmail.com
| Desenvolvedor Web
|--------------------------------------------------|

#2 mascker

mascker
  • Visitantes

Posted 28/09/2004, 12:05

Ois

este foi-me dado por alguem aqui do site

nã me lembro o nome ;)

<?php
//
//AQUI METES OS FICHEIROS DE CONEXÃO À BASE DE DADOS
//

$target="file";
if($target=="file")
{
header('Content-Type: application/octetstream');
header('Content-Disposition: filename="basededados.sql"');
$asfile="download";
}

$crlf="\r\n";

$dbname = NOME_DA_BASE_DE_DADOS_AQUI;

$dump_buffer="";

$tables = mysql_query("show tables from $dbname");
$num_tables = mysql_num_rows($tables);

if($num_tables == 0)
{
echo "# No Tables Found";
exit;
}
$dump_buffer.= "# Nome do BD: $dbname.sql $crlf";
$dump_buffer.= "# ".date("j F, Y, g:i a")."$crlf";
$dump_buffer.= "# Copyright © Seu_nome $crlf";
$dump_buffer.= "# $crlf";
$i = 0;
while($i < $num_tables)
{
$table = mysql_tablename($tables, $i);
$dump_buffer.= "# --------------------------------------------------------$crlf";
$dump_buffer.= "# $crlf";
$dump_buffer.= "$crlf#$crlf";
$dump_buffer.= "# Estrutura da tabela '$table'$crlf";
$dump_buffer.= "#$crlf$crlf";
$db = $table;
$dump_buffer.= get_table_def($table, $crlf,$dbname).";$crlf";
$dump_buffer.= "$crlf#$crlf";
$dump_buffer.= "# Dados extraidos da tabela '$table'$crlf";
$dump_buffer.= "#$crlf$crlf";
$tmp_buffer="";
get_table_content($dbname, $table, 0, 0, 'my_handler', $dbname);
$dump_buffer.=$tmp_buffer;

$i++;
$dump_buffer.= "$crlf";
}
echo $dump_buffer;
exit;

function get_table_def($table, $crlf,$dbname)
{

$schema_create = "DROP TABLE IF EXISTS $table;$crlf";
$db = $table;

$schema_create .= "CREATE TABLE $table ($crlf";

$result = mysql_query("SHOW FIELDS FROM " .$dbname."."
. $table) or die();
while($row = mysql_fetch_array($result))
{
$schema_create .= " $row[Field] $row[Type]";

if(isset($row["Default"]) && (!empty($row["Default"]) || $row["Default"] == "0"))
$schema_create .= " DEFAULT '$row[Default]'";
if($row["Null"] != "YES")
$schema_create .= " NOT NULL";
if($row["Extra"] != "")
$schema_create .= " $row[Extra]";
$schema_create .= ",$crlf";
}
$schema_create = ereg_replace(",".$crlf."$", "", $schema_create);
$result = mysql_query("SHOW KEYS FROM " .$dbname."." .
$table) or die();
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'];
}

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)";
if(get_magic_quotes_gpc()) {
return (stripslashes($schema_create));
} else {
return ($schema_create);
}
}
function get_table_content($db, $table, $limit_from = 0, $limit_to = 0,$handler)
{
// Defines the offsets to use
if ($limit_from > 0) {
$limit_from--;
} else {
$limit_from = 0;
}
if ($limit_to > 0 && $limit_from >= 0) {
$add_query = " LIMIT $limit_from, $limit_to";
} else {
$add_query = '';
}

get_table_content_fast($db, $table, $add_query,$handler);

}

function get_table_content_fast($db, $table, $add_query = '',$handler)
{
$result = mysql_query('SELECT * FROM ' . $db . '.' . $table . $add_query) or die();
if ($result != false) {

@set_time_limit(1200);

for ($j = 0; $j < mysql_num_fields($result); $j++) {
$field_set[$j] = mysql_field_name($result, $j);
$type = mysql_field_type($result, $j);
if ($type == 'tinyint' || $type == 'smallint' || $type == 'mediumint' || $type == 'int' ||
$type == 'bigint' ||$type == 'timestamp') {
$field_num[$j] = true;
} else {
$field_num[$j] = false;
}
} // end for

// Get the scheme
if (isset($GLOBALS['showcolumns'])) {
$fields = implode(', ', $field_set);
$schema_insert = "INSERT INTO $table ($fields) VALUES (";
} else {
$schema_insert = "INSERT INTO $table VALUES (";
}

$field_count = mysql_num_fields($result);

$search = array("\x0a","\x0d","\x1a"); //\x08\\x09, not required
$replace = array("\\n","\\r","\Z");


while ($row = mysql_fetch_row($result)) {
for ($j = 0; $j < $field_count; $j++) {
if (!isset($row[$j])) {
$values[] = 'NULL';
} else if (!empty($row[$j])) {
// a number
if ($field_num[$j]) {
$values[] = $row[$j];
}
// a string
else {
$values[] = "'" . str_replace($search, $replace, addslashes($row[$j])) . "'";
}
} else {
$values[] = "''";
} // end if
} // end for

$insert_line = $schema_insert . implode(',', $values) . ')';
unset($values);

// Call the handler
$handler($insert_line);
} // end while
} // end if ($result != false)

return true;
}


function my_handler($sql_insert)
{
global $crlf, $asfile;
global $tmp_buffer;

if(empty($asfile))
$tmp_buffer.= htmlspecialchars("$sql_insert;$crlf");
else
$tmp_buffer.= "$sql_insert;$crlf";
}



function faqe_db_error()
{
return mysql_error();
}



function faqe_db_insert_id($result)
{
return mysql_insert_id($result);
}


?>



#3 jaraujo

jaraujo

    12 Horas

  • Banidos
  • PipPipPip
  • 162 posts
  • Sexo:Não informado

Posted 28/09/2004, 18:48

mascker :blink:

Veja que aqui eu tinha: Postado em 5 Aug 2004, 13:31

http://forum.wmonlin...=83021&hl=mysql

O problema é que o pessoal fica respondendo mexendo no script em vez de te passar o link correto(y)

Crie uma pasta com o nome "copia"

Coloca nela as limas "copia.php" e "index.html" :

copia.php


<?php
$target="file";
if($target=="file")
{
header('Content-Type: application/octetstream');
header('Content-Disposition: filename="nome_da_base_de_dados.sql"');
$asfile="download";
}

$crlf="\r\n";

$link = mysql_connect("localhost", "root", "");
$database = mysql_select_db("nome_da_base_de_dados"); 

$dbname = "nome_da_base_de_dados";

$dump_buffer="";

$tables = mysql_query("show tables from $dbname");
$num_tables = mysql_num_rows($tables);

if($num_tables == 0)
{
echo "# No Tables Found";
exit;
}
$dump_buffer.= "# Nome do BD: $dbname.sql $crlf";
$dump_buffer.= "# ".date("j F, Y, g:i a")."$crlf";
$dump_buffer.= "# Copyright © Seu_nome $crlf";
$dump_buffer.= "# $crlf";
$i = 0;
while($i < $num_tables)
{
$table = mysql_tablename($tables, $i);
$dump_buffer.= "# --------------------------------------------------------$crlf";
$dump_buffer.= "# $crlf";
$dump_buffer.= "$crlf#$crlf";
$dump_buffer.= "# Estrutura da tabela '$table'$crlf";
$dump_buffer.= "#$crlf$crlf";
$db = $table;
$dump_buffer.= get_table_def($table, $crlf,$dbname).";$crlf";
$dump_buffer.= "$crlf#$crlf";
$dump_buffer.= "# Dados extraidos da tabela '$table'$crlf";
$dump_buffer.= "#$crlf$crlf";
$tmp_buffer="";
get_table_content($dbname, $table, 0, 0, 'my_handler', $dbname);
$dump_buffer.=$tmp_buffer;

$i++;
$dump_buffer.= "$crlf";
}
echo $dump_buffer;
exit;

function get_table_def($table, $crlf,$dbname)
{

$schema_create = "DROP TABLE IF EXISTS $table;$crlf";
$db = $table;

$schema_create .= "CREATE TABLE $table ($crlf";

$result = mysql_query("SHOW FIELDS FROM " .$dbname."."
. $table) or die();
while($row = mysql_fetch_array($result))
{
$schema_create .= " $row[Field] $row[Type]";

if(isset($row["Default"]) && (!empty($row["Default"]) || $row["Default"] == "0"))
$schema_create .= " DEFAULT '$row[Default]'";
if($row["Null"] != "YES")
$schema_create .= " NOT NULL";
if($row["Extra"] != "")
$schema_create .= " $row[Extra]";
$schema_create .= ",$crlf";
}
$schema_create = ereg_replace(",".$crlf."$", "", $schema_create);
$result = mysql_query("SHOW KEYS FROM " .$dbname."." .
$table) or die();
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'];
}

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)";
if(get_magic_quotes_gpc()) {
return (stripslashes($schema_create));
} else {
return ($schema_create);
}
}
function get_table_content($db, $table, $limit_from = 0, $limit_to = 0,$handler)
{
// Defines the offsets to use
if ($limit_from > 0) {
$limit_from--;
} else {
$limit_from = 0;
}
if ($limit_to > 0 && $limit_from >= 0) {
$add_query = " LIMIT $limit_from, $limit_to";
} else {
$add_query = '';
}

get_table_content_fast($db, $table, $add_query,$handler);

}

function get_table_content_fast($db, $table, $add_query = '',$handler)
{
$result = mysql_query('SELECT * FROM ' . $db . '.' . $table . $add_query) or die();
if ($result != false) {

@set_time_limit(1200); 

for ($j = 0; $j < mysql_num_fields($result); $j++) {
$field_set[$j] = mysql_field_name($result, $j);
$type = mysql_field_type($result, $j);
if ($type == 'tinyint' || $type == 'smallint' || $type == 'mediumint' || $type == 'int' ||
$type == 'bigint' ||$type == 'timestamp') {
$field_num[$j] = true;
} else {
$field_num[$j] = false;
}
} // end for

// Get the scheme
if (isset($GLOBALS['showcolumns'])) {
$fields = implode(', ', $field_set);
$schema_insert = "INSERT INTO $table ($fields) VALUES (";
} else {
$schema_insert = "INSERT INTO $table VALUES (";
}

$field_count = mysql_num_fields($result);

$search = array("\x0a","\x0d","\x1a"); //\x08\\x09, not required
$replace = array("\\n","\\r","\Z");


while ($row = mysql_fetch_row($result)) {
for ($j = 0; $j < $field_count; $j++) {
if (!isset($row[$j])) {
$values[] = 'NULL';
} else if (!empty($row[$j])) {
// a number
if ($field_num[$j]) {
$values[] = $row[$j];
}
// a string
else {
$values[] = "'" . str_replace($search, $replace, addslashes($row[$j])) . "'";
}
} else {
$values[] = "''";
} // end if
} // end for

$insert_line = $schema_insert . implode(',', $values) . ')';
unset($values);

// Call the handler
$handler($insert_line);
} // end while
} // end if ($result != false)

return true;
}


function my_handler($sql_insert)
{
global $crlf, $asfile;
global $tmp_buffer;

if(empty($asfile))
$tmp_buffer.= htmlspecialchars("$sql_insert;$crlf");
else
$tmp_buffer.= "$sql_insert;$crlf";
}



function faqe_db_error()
{
return mysql_error();
}



function faqe_db_insert_id($result)
{
return mysql_insert_id($result);
}


?>


index.html


<html>
<head>
<title>Backup da base de dados: nome_da_base_de_dados.sql</title>

</head>
<body>

<table border="0" align="center" cellpadding="2" cellspacing="1" width="25%">
<tr bgcolor="CCCCCC">
<td align="center"><font face="verdana" size="1"><strong>Backup da base de dados: nome_da_base_de_dados.sql</strong></font>
</tr>
<tr>
<td align="center" >
<input type="button" value="Backup" name="nome_da_base_de_dados.sql" onclick="javascript:window.open('copia.php','_self')">
</tr>
</table>

</body>
</html>



Responde depois se fucionou, certo! (y)

Edição feita por: jaraujo, 28/09/2004, 18:53.


#4 danilodepolli

danilodepolli

    Doutor

  • Usuários
  • 987 posts
  • Sexo:Não informado

Posted 21/01/2005, 23:02

Tem como fazer com que esse arquivo com os dados do bd seja salvo dentro do diretorio do servidor?
Danilo Depolli
danilo@camphost.com.br
CAMPHOST - 1º MES GRATIS EM QUALQUER PLANO! HOSPEDAGEM WEB
QUER PATROCINIO FREE? ENTRE EM CONTATO
ICQ: 7695319
MSN: danilodepolli@bol.com.br




1 user(s) are reading this topic

0 membro(s), 1 visitante(s) e 0 membros anônimo(s)

IPB Skin By Virteq