|
Posted by Geoff Berrow on 04/30/07 12:35
Message-ID: <FJ6dnWNqjeeUQajbnZ2dnUVZ_tqnnZ2d@comcast.com> from Kevin
Marks contained the following:
>Does anyone have an ultra simple php routine that does this. I really want
>bare minimum, so I can figure it out.
Here's what I use.
<?php
//backup function
function datadump ($table,$dir) {
if(!file_exists($dir.$table."_".date('d-M-Y').".sql")){
$result .= "-- Dump of $table \n";
$result .= "-- Dump DATE : " . date("d-M-Y") ."\n\n";
$query = mysql_query("select * from $table");
$num_fields = @mysql_num_fields($query);
$numrow = mysql_num_rows($query);
while($row=mysql_fetch_array($query)) {
$result .= "INSERT INTO `".$table."` VALUES(";
for($j=0; $j<$num_fields; $j++) {
$row[$j] = addslashes($row[$j]);
$row[$j] = ereg_replace("\n","\\n",$row[$j]);
if (isset($row[$j])) $result .= "'$row[$j]'" ; else $result
..= "''";
if ($j<($num_fields-1)) $result .= ", ";
}
$result .= ");\n";
}
//save as a file
$filename = $dir.$table."_".date('d-M-Y').".sql";
if (!$handle = fopen($filename, 'w')) {
echo "Cannot open file ($filename)";
exit;
}
// Write to our opened file.
if (fwrite($handle, $result) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
fclose($handle);
//return $result . "\n\n\n";
echo "Backup <a
href='".$dir.$table."_".date('d-M-Y').".sql
'>".$table."_".date('d-M-Y').".sql</a> created<br>";
}
else{
echo "<a href='".$dir.$table."_".date('d-M-Y').".sql
'>".$table."_".date('d-M-Y').".sql </a><br>";
}
}
?>
--
Geoff Berrow 0110001001101100010000000110
001101101011011001000110111101100111001011
100110001101101111001011100111010101101011
Navigation:
[Reply to this message]
|