Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function backup_bd($host,$utilizador,$password,$nome,$tabelas = '*')
- {
- $link = mysql_connect($host,$utilizador,$password);
- mysql_select_db($nome,$link);
- if ($tabelas == '*')
- {
- $tabelas = array();
- $resultado = mysql_query('SHOW TABLES');
- while($coluna = mysql_fetch_row($resultado))
- {
- $tabelas[] = $coluna[0];
- }
- }
- else
- {
- $tabelas = is_array($tabelas) ? $tabelas: explode(',',$tabelas);
- }
- foreach($tabelas as $tabelas)
- {
- $resultado = mysql_query('SELECT * FROM '.$tabelas);
- $num_campos = mysql_num_fields($resultado);
- $return.= 'DROP TABLE '.$tabelas.';';
- $coluna2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$tabelas));
- $return.= "\n\n".$coluna2[1].";\n\n";
- for ($i = 0; $i < $num_campos; $i++)
- {
- while($coluna = mysql_fetch_row($resultado))
- {
- $return.= 'INSERT INTO '.$tabelas.' VALUES(';
- for($j=0; $j<$num_campos; $j++)
- {
- $coluna[$j] = addslashes($row[$j]);
- $coluna[$j] = ereg_replace("\n","\\n",$coluna[$j]);
- if (isset($coluna[$j])) { $return.= '"'.$coluna[$j].'"' ; } else { $return.= '""'; }
- if ($j<($num_campos-1)) { $return.= ','; }
- }
- //$return.= ");\n";
- }
- echo $return;
- }
- $return.="\n\n\n";
- }
- $ficheiro = fopen('bd-backup-'.time().'-'.(md5(implode(',',$tabelas))).'.sql','w+');
- fwrite($ficheiro,$return);
- fclose($ficheiro);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement