Last active
August 29, 2015 14:19
-
-
Save avuenja/a63836c25167337027f5 to your computer and use it in GitHub Desktop.
Fazer backup database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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