<?php
define("SAVE_PATH", "/my/save/path/to/store/backups");
define("DB_TO_BACKUP", "foo");
function logger($text_to_log)
{
$cur_time=date("Y-m-d H:i");
$handle = fopen(SAVE_PATH . "/backup.log", "a");
if($handle)
{
fputs($handle, "$cur_time\t$text_to_log\n");
fclose(handle);
}
}
function get_def($dbname, $table) {
$def = "";
$def .= "DROP TABLE IF EXISTS $table;\n";
$def .= "CREATE TABLE $table (\n";
$result = mysql_db_query($dbname, "SHOW FIELDS FROM $table") or logger(mysql_error());
while($row = mysql_fetch_array($result)) {
if(mysql_error()) logger(mysql_error());
$def .= " $row[Field] $row[Type]";
if ($row["Default"] != "") $def .= " DEFAULT '$row[Default]'";
if ($row["Null"] != "YES") $def .= " NOT NULL";
if ($row[Extra] != "") $def .= " $row[Extra]";
$def .= ",\n";
}
$def = ereg_replace(",\n$","", $def);
$result = mysql_db_query($dbname, "SHOW KEYS FROM $table") or logger(mysql_error());
while($row = mysql_fetch_array($result)) {
if(mysql_error()) logger(mysql_error());
$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)) {
$def .= ",\n";
if($x == "PRIMARY") $def .= " PRIMARY KEY (" . implode($columns, ", ") . ")";
else if (substr($x,0,6) == "UNIQUE") $def .= " UNIQUE ".substr($x,7)." (" . implode($columns, ", ") . ")";
else $def .= " KEY $x (" . implode($columns, ", ") . ")";
}
$def .= "\n);";
return (stripslashes($def));
}
function get_content($dbname, $table) {
$content="";
$result = mysql_db_query($dbname, "SELECT * FROM $table") or logger(mysql_error());
while($row = mysql_fetch_row($result)) {
if(mysql_error()) logger(mysql_error());
$insert = "INSERT INTO $table VALUES (";
for($j=0; $j<mysql_num_fields($result);$j++) {
if(!isset($row[$j])) $insert .= "NULL,";
else if($row[$j] != "") $insert .= "'".$row[$j]."',";
else $insert .= "'',";
}
$insert = ereg_replace(",$","",$insert);
$insert .= ");\n";
$content .= $insert;
}
return $content;
}
function backup($db_name, $send = true)
{
$cur_date=date("Y-m-d");
$cur_time=date("Y-m-d H:i");
$newfile="# Dump created with phpMyBackup on $cur_time\r\n";
$tables = mysql_list_tables($db_name) or logger(mysql_error());
$num_tables = @mysql_num_rows($tables) or logger(mysql_error());
$i = 0;
while($i < $num_tables) {
$table = mysql_tablename($tables, $i) or logger(mysql_error());
$newfile .= "\n# ----------------------------------------------------------\n#\n";
$newfile .= "# structur for table '$table'\n#\n\n";
$newfile .= get_def($db_name,$table);
$newfile .= "\n\n";
$newfile .= "#\n# data for table '$table'\n#\n\n";
$newfile .= get_content($db_name,$table);
$newfile .= "\n\n";
$i++;
}
$filename = SAVE_PATH . "/" . $db_name.".".$cur_date.".sql";
$fp = fopen ("$filename","w");
fwrite ($fp,$newfile);
fclose ($fp);
if($send != false)
{
header("Content-Type: text/sql");
header("Content-Disposition: attachment; filename=$filename");
readfile("$filename");
unlink($filename);
}
else
return($filename);
}
backup(DB_TO_BACKUP, false);
?>