Jeg har også lånt koden fra php-myadmin. Du skal bare ændre variablen $DB_NAME til navnet på databasen som skal dumpes, samt variablen $DB_BACKUP_DIR til hvor hvor filen skal gemmes:
-- filen: config.inc : indholder alle mulige config vars --
/* MySQL Settings */
$DB_HOST = "...";
$DB_USER = "...";
$DB_PASSWORD = "...";
$DB_NAME = "...";
/* MySQL Backup settings */
$DB_BACKUP_DIR = "mysql/backup";
$DB_BACKUP = TRUE; // auto backup database! NB: May consume alot of diskspace
$DB_BACKUP_CLEANUP = TRUE; // auto cleanup so there only will be $DB_BACKUP_MAXFILES files at the most!
$DB_BACKUP_MAXFILES = 50; // 0 = unlimited
-- filen: libary.inc : som er min fil med alle mulige brugbare funktioner :) --
/*
* Function to make a database dump to file
* (kan ændres til at tage 1 indgående parameter,
* som er navnet på databasen der skal dumpes.
*/
function db_dump()
{
global $DB_NAME, $DB_BACKUP_DIR, $DB_BACKUP;
if ($DB_BACKUP) {
// Write to file
$filename = date("Y.m.d.H.i.s").".sql";
/* Do the header */
$today = date("l jS \o\f F, H:i:s (@B)");
$white_space = str_repeat(" ", (54 - strlen($today)));
$content = "";
$content .= "#############################################################\n";
$content .= "# #\n";
$content .= "# THIS FILE WAS AUTOMATICALLY GENERATED ON: #\n";
$content .= "# ".$today.$white_space."#\n";
$content .= "# #\n";
$content .= "# DENNE FIL INDEHOLDER ET DATABASE DUMP FRA #\n";
$content .= "# DATABASEN Movie_DB. #\n";
$content .= "# #\n";
$content .= "# POWERED BY - PHP & MYSQL #\n";
$content .= "# #\n";
$content .= "# © RASMUS PEDERSEN (math.rasmus@post.cybercity.dk) #\n";
$content .= "# #\n";
$content .= "#############################################################\n";
$result = mysql_list_tables($DB_NAME);
while ($table = mysql_fetch_row($result)) {
// Get table fields and properties
$table_info = MySQLGetQuery("
SHOW FIELDS FROM ".$table[0]);
foreach ($table_info as $row) {
// Create a printable string
$row_array[] = $row['Field']." ".strtoupper($row['Type']).($row['Null']=="" ? " NOT NULL" : "");
$table_fields[] = $row['Field'];
// Catch the primary key(s)
if (strtolower($row['Key'])=="pri") {
$row_key[] = $row['Field'];
}
}
// Get the table type
$table_type = MySQLGetQuery("
SHOW TABLE STATUS LIKE '".$table[0]."'");
/*
* Get table data
*/
$table_data = mysql_query("
SELECT *
FROM ".$table[0]);
// Format table data
/*
* The following piece of code is has been customiced for
* my needs. It's made by staybyte, and orginates from
* the project phpMyAdmin (
http://www.phpMyAdmin.net)
*/
if ($table_data != FALSE) {
$fields_cnt = mysql_num_fields($table_data);
$rows_cnt = mysql_num_rows($table_data);
// Checks whether the field is an integer or not
for ($j = 0; $j < $fields_cnt; $j++) {
$field_set[$j] = mysql_field_name($table_data, $j);
$type = mysql_field_type($table_data, $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
$search = array("\x00", "\x0a", "\x0d", "\x1a"); //\x08\\x09, not required
$replace = array("\0", "\n", "\r", "\Z");
$current_row = 0;
@set_time_limit($GLOBALS['cfgExecTimeLimit']);
while ($row = mysql_fetch_row($table_data)) {
$current_row++;
for ($j = 0; $j < $fields_cnt; $j++) {
if (!isset($row[$j])) {
$values[] = "NULL";
} else if ($row[$j] == "0" || $row[$j] != "") {
// a number
if ($field_num[$j]) {
$values[] = $row[$j];
}
// a string
else {
$values[] = "'" . str_replace($search, $replace, sql_add_slashes($row[$j])) . "'";
}
} else {
$values[] = "''";
} // end if
} // end for
// Extended inserts case
$insert_line[] = "(".implode(", ", $values).")";
unset($values);
} // end while
} // end if ($result != FALSE)
/* Append table definitions and data dump */
$content .= "\n\n-- Struktur dump for tabellen "
. $table[0]
. "\nDROP TABLE IF EXISTS "
. $table[0]
. ";\nCREATE TABLE "
. $table[0]
. " (\n "
. implode(",\n ", $row_array)
. ",\n PRIMARY KEY ("
. implode(", ", $row_key)
. ")\n) TYPE="
. $table_type[0]['Type']
. ";\n\n-- Data dump for tabllen "
. $table[0]
. "\nINSERT INTO "
. $table[0]
. " ("
. implode(", ", $table_fields)
. ")\nVALUES\n"
. implode(",\n", $insert_line)
. ";\n";
unset($row_array, $row_key, $table_fields, $insert_line);
}
/* Write $content to file */
cleanup();
if (is_dir($DB_BACKUP_DIR)) {
$temp = tempnam($DB_BACKUP_DIR."/", "");
if (!copy($temp, $DB_BACKUP_DIR."/".$filename)) {
;//error_handle("Kunne ikke kopierer filen: <b>$temp</b> til <b>$filename</b>");
}
if (!file_exists($DB_BACKUP_DIR."/".$filename)) {
error_handle("Filen: ".$DB_BACKUP_DIR."/".$filename." findes ikke"); //print("Filen eksister ikke")
} else {
$fp = fopen($DB_BACKUP_DIR."/".$filename, "w+");
if ($fp) {
fwrite ($fp, $content);
fclose($fp);
} // end if
}// end else
} else {
;//error_handle("$DB_BACKUP_DIR er ikke et bibliotek!"); //print("Ikke et dir!")
}
}
} // end function