1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
<?php
// ------------------------------------------------------------------------- //
// Sauvegarde MYSQL 'structure' et 'data' //
// ------------------------------------------------------------------------- //
// Source: Olivier Fabre // Modifier par T / / // Email: ab@overdrived.com // // Web:http://www.overdrived.com //
// ------------------------------------------------------------------------- //
/// File name : savedb.php /// DUMPING d'une DATABASE - STRUCTURE et DATA /// Script PHP realisé par T
/////////////////// $host = "sql.overdrived.com"; /// NOM DU SERVEUR SQL $user = ""; /// LOGIN $pass = ""; /// PASS $db = "overdrived.com"; /// NOM DE LA DATABASE $tb="0"; ///////////////////
@set_time_limit(600);
@mysql_connect($host,$user,$pass) or die("Impossible de se connecter - Pb sur le 'Hostname' ou sur le 'User' " . "ou sur le 'Password'");
@mysql_select_db("$db") or die("Impossible de se connecter - Pb sur le 'Nom de la Data Base'");
if ($tb) { header("Content-disposition: filename=$tb.sql"); } else { echo'DATADBPHP 4 BY T website:http://www.overdrived.com';} function get_table_def($db, $table, $crlf) { global $drop;
$schema_create = ""; if(!empty($drop)) $schema_create .= "DROP TABLE IF EXISTS $table;$crlf";
$schema_create .= "CREATE TABLE $table ($crlf";
$result = mysql_db_query($db, "SHOW FIELDS FROM $table") or mysql_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_db_query($db, "SHOW KEYS FROM $table") or mysql_die(); while($row = mysql_fetch_array($result)) { $kname=$row['Key_name']; if(($kname != "PRIMARY") && ($row['Non_unique'] == 0)) $kname="UNIQUE|$kname"; if(!isset($index[$kname])) $index[$kname] = array(); $index[$kname][] = $row['Column_name']; }
while(list($x, $columns) = @each($index)) { $schema_create .= ",$crlf"; if($x == "PRIMARY") $schema_create .= " PRIMARY KEY (" . implode($columns, ", ") . ")";
elseif (substr($x,0,6) == "UNIQUE") $schema_create .= " UNIQUE ".substr($x,7)." (".implode($columns,", " ).")"; else $schema_create .= " KEY $x (" . implode($columns, ", ") . ")"; }
$schema_create .= "$crlf)"; return (stripslashes($schema_create)); }
function get_table_content($db, $table, $handler) { $result = mysql_db_query($db, "SELECT * FROM $table") or mysql_die(); $i = 0; while($row = mysql_fetch_row($result)) { $table_list = "(";
for($j=0; $j<mysql_num_fields($result);$j++) $table_list .= mysql_field_name($result,$j).", ";
$table_list = substr($table_list,0,-2); $table_list .= ")";
if(isset($GLOBALS["showcolumns"])) $schema_insert = "INSERT INTO $table $table_list VALUES ("; else $schema_insert = "INSERT INTO $table VALUES (";
for($j=0; $j<mysql_num_fields($result);$j++) { if(!isset($row[$j])) $schema_insert .= " NULL,"; elseif($row[$j] != "") $schema_insert .= " '".addslashes($row[$j])."',"; else $schema_insert .= " '',"; } $schema_insert = ereg_replace(",$", "", $schema_insert); $schema_insert .= ")"; $handler(trim($schema_insert)); $i++; } return (true); }
function my_handler($sql_insert) { global $crlf, $asfile;
echo "$sql_insert;$crlf"; }
$crlf="\n";
$strTableStructure = "Table structure for table";
$strDumpingData = "Dumping data for table";
$tables = mysql_list_tables($db);
$num_tables = @mysql_numrows($tables);
$i = 0;
while($i < $num_tables) { $table = mysql_tablename($tables, $i); if ($tb) { if ($table == $tb) { print $crlf; print "# --------------------------------------------------------$cr lf"; print "#$crlf"; print "# $strTableStructure '$table'$crlf"; print "#$crlf"; print $crlf;
echo get_table_def($db, $table, $crlf).";$crlf$crlf";
print "#$crlf"; print "# $strDumpingData '$table'$crlf"; print "#$crlf"; print $crlf;
get_table_content($db, $table, "my_handler");
exit ; } } else { print $crlf; print "# --------------------------------------------------------$crlf"; print "#$crlf"; print "# $strTableStructure '$table'$crlf"; print "#$crlf"; print $crlf;
echo get_table_def($db, $table, $crlf).";$crlf$crlf";
print "#$crlf"; print "# $strDumpingData '$table'$crlf"; print "#$crlf"; print $crlf;
get_table_content($db, $table, "my_handler"); }
$i++; }
mysql_close();
?> |