Skip to content

Instantly share code, notes, and snippets.

@avuenja
Last active August 29, 2015 14:19
Show Gist options
  • Save avuenja/a63836c25167337027f5 to your computer and use it in GitHub Desktop.
Save avuenja/a63836c25167337027f5 to your computer and use it in GitHub Desktop.
Fazer backup database
<?php
function backup_db($host, $user, $pass, $db) {
mysql_connect($host,$user,$pass) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());
$back = fopen("backup.sql","w");
$res = mysql_list_tables($db) or die(mysql_error());
while ($row = mysql_fetch_row($res)) {
$table = $row[0];
$res2 = mysql_query("SHOW CREATE TABLE $table");
while ( $lin = mysql_fetch_row($res2)){
fwrite($back,"-- Criando tabela : $table\n");
$res3 = mysql_query("SELECT * FROM $table");
while($r=mysql_fetch_row($res3)){
$sql="INSERT INTO $table VALUES ('";
$sql .= implode("','",$r);
$sql .= "')\n";
fwrite($back,$sql);
}
}
}
fclose($back);
}
// Altere os dados para sua conexão
backup_db('your localhost', 'your username', 'your password', 'your database');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment