Codice PHP:
<? /* Questo script è preso dal sito planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=772&lngWId=8 a questo indirizzo trovate anche un tutorial e lo stesso codice per utilizzare lo script. Sono state apportate piccolissime modifiche al file per renderlo un po più semplice e sicuro. ---------------------------------------------- Changelog ---------------------------------------------- 5 Nov 2007 : risolto bug caratteri accentati, fixed bug sui tag html 3 Nov 2007 : modificato il metodo get_table_def() per compatibilità mysql5 (Giuseppe Piraino) */ //Sintassi per richiamare il file back.php?password=1 //questo file (backup.php) può essere rinominato in questo caso la sintassi diventa ad esempio //nuovonome.php?password=tuapassword if(!isset($_GET['password']) || $_GET['password']!=1)exit("For open this page you must digit a password!"); $localhost="localhost"; $dbuser='root'; $dbpass=''; $dbname='negozio'; $target="file"; if($target=="file") { header('Content-Type: application/octetstream'); header('Content-Disposition: filename="backup.sql"'); $asfile="download"; } $crlf="\r\n"; $link = mysql_connect($localhost, $dbuser, $dbpass); $database = mysql_select_db($dbname); //$dbname = "newobs"; $dump_buffer=""; $tables = mysql_query("show tables from $dbname"); $num_tables = mysql_num_rows($tables); if($num_tables == 0) { echo "# No Tables Found"; exit; } //MODIFICATI ALCUNI COMMENTI /* $dump_buffer.= "# DatabaseBackup $crlf"; $dump_buffer.= "# Backup Eseguito:$crlf"; $dump_buffer.= "# ".date("d-n-Y, g:i a")."$crlf"; $dump_buffer.= "# Database: $dbname$crlf"; $dump_buffer.= "# Backed up tabelle : $dbname $crlf"; $dump_buffer.= "$crlf#$crlf"; */ $i = 0; while($i < $num_tables) { $table = mysql_tablename($tables, $i); //echo $table . "
"; //$dump_buffer.= "#--------------------------------------------------------$crlf"; //$dump_buffer.= "$crlf#$crlf"; //$dump_buffer.= "# Table structure for table '$table'$crlf"; //$dump_buffer.= "#$crlf$crlf"; $db = $table; $dump_buffer.= get_table_def($table, $crlf,$dbname).";$crlf"; //$dump_buffer.= "$crlf#$crlf"; //$dump_buffer.= "# Dumping data for table '$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; //fixed bug/optimized for mysql>=5.0 (Giuseppe Piraino) ; function get_table_def($table, $crlf,$dbname) { if($table=mysql_query("SHOW CREATE TABLE `$table`")){ list($table_name, $create_table)=mysql_fetch_row($table); return $create_table; } return False; } 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); // 20 Minutes // Checks whether the field is an integer or not 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","è","é","à","ì","í","ò","ù","'"); //MODIFICATO IL REPLACE SEARCH $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]. "'"; //fixed mysql5 - insert of number need quote! (Giuseppe Piraino); } // a string else { //$values[] = "'" . str_replace($search, $replace, addslashes($row[$j])) . "'"; MODIFICA DI QUESTA RIGA PER OVVIARE AGLI SLASH $values[] = "'" . str_replace($search, $replace, $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; //Giuseppe Piraino - 5 Nov 2007 - This code is inconsistent //uncomment only if you have mysql<=4.1 //if(empty($asfile)) // $tmp_buffer.= htmlspecialchars("$sql_insert;$crlf"); //else $tmp_buffer.= "$sql_insert;$crlf"; } /* Giuseppe Piraino - this functions are unused function faqe_db_error() { return mysql_error(); } function faqe_db_insert_id($result) { return mysql_insert_id($result); } */ ?>
e questo è il restore
Codice PHP:
<? //devo solo mette il percorso del file!!! e tagliarlo se troppo grande..... if(!isset($_REQUEST["qwe"])){ ?> <html> <head> <title>Form di Importazione!!!</title> </head> <body> <h1>Carica il file del backup!!!!</h1> <form name="restore" method="post" action="restore.php" enctype="multipart/form-data"> file backup: <input type="file" name="file_sql" id="sql"> <input type="hidden" value="1" name="percorso" id="percorso"> <input type="hidden" value="1" name="qwe"> <input type="submit" value="Carica"> </form> </body> </html> <? } else{ $db_host="localhost"; $db_user='root'; $db_password=''; $db_name='negozio1'; //include("config.php"); $db = mysql_connect($db_host, $db_user, $db_password); if ($db == FALSE) die ("Errore nella connessione. Verificare i parametri nel file config.php"); mysql_select_db($db_name, $db) or die ("Errore nella selezione del database. Verificare i parametri nel file config.php"); $news=fopen("backupNEW.sql","r"); //apre il file $i=0; while (!feof($news)) { $buffer = fgets($news, 4096); $buffer=trim($buffer); if($buffer[strlen($buffer)-1]==";"){$str[$i].=$buffer;$i++;} else $str[$i].=$buffer; } fclose ($news); #chiude il file for ($k=0;$k<count($str);$k++){ if(mysql_query($str[$k])==true)echo $str[$k]."
Eseguita correttamente...
"; else echo $str[$k]."
NON ESEGUITA...
"; //echo $str[$k]."
"; } echo "
"; } ?>