<?php
$dbhost = 'localhost';
$dbuser = 'user';
$dbpass = 'pass';
$dbnames = array('dtb_1', 'dtb_2', 'dtb_3);
$use_gzip = 'yes';
$remove_file = 'no';
$use_email = 'no';
$send_to = 'you@yourdomain.com';
$send_from = 'backup@yourdomain.com';
$subject = "MySQL Database ($dbname) Backup - " . date("j F Y");
$use_ftp = 'no';
$ftp_server = 'ftp.yourdomain.com';
$ftp_user_name = 'user';
$ftp_user_pass = 'xxxxxxxx';
$ftp_path = "/db_backups";
$echo_status = 'yes';
$path = make_dir();
foreach ($dbnames as $dbname)
{
$db = mysql_connect("$dbhost","$dbuser","$dbpass");
mysql_select_db("$dbname",$db);
if ($echo_status == 'yes') {
print "MySQL Database Backup...";
print "Starting Process...";
print "Backup dumpfile in progress...";
}
$result = false;
$result = mysql_query("show tables from $dbname");
$newfile = '';
while (list($table) = mysql_fetch_row($result)) {
$newfile .= get_def($table);
$newfile .= "\n\n";
$newfile .= get_content($table);
$newfile .= "\n\n";
$i++;
if ($echo_status == 'yes') {
print "Dumped table $table...";
}
}
$file_name = $dbname . "-" . date("Ymd-Hi") . ".sql";
$file_path = $path . $file_name;
if ($use_gzip == "yes") {
$file_name .= ".gz";
$file_path .= ".gz";
$zp = gzopen($file_path, "wb9");
gzwrite($zp,$newfile);
gzclose($zp);
if ($echo_status == 'yes') {
print "Gzip-file is created...";
}
} else {
$fp = fopen($file_path, "w");
fwrite($fp, $newfile);
fclose($fp);
if ($echo_status == 'yes') {
print "<br>SQL-file is created...<br>";
}
}
if ($remove_file == "yes") {
unlink($file_name);
if ($echo_status == 'yes') {
print "<br>File is deleted...<br>";
}
}
if ($echo_status == 'yes') {
print "<br>Back up done!<p>";
print "</body></html>";
}
}
mail('mail@mail.com', 'Subject', 'Message');
function make_dir() {
$page = split("/", getenv('SCRIPT_NAME'));
$n = count($page)-1;
$page = $page[$n];
$page = split("\.", $page, 2);
$extension = $page[1];
$page = $page[0];
$script = "$page.$extension";
$base_url = "http://".$_SERVER['SERVER_NAME'];
$directory = $_SERVER['PHP_SELF'];
$url_base = "$base_url$directory";
$url_base = ereg_replace("$script", '', "$_SERVER[PATH_TRANSLATED]");
$path = $url_base;
return $path;
}
function get_def($table) {
$def = "";
$def .= "DROP TABLE IF EXISTS $table;\n";
$def .= "CREATE TABLE $table (\n";
$result = mysql_query("SHOW FIELDS FROM $table") or die("Table $table not existing in database");
while($row = mysql_fetch_array($result)) {
$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_query("SHOW KEYS FROM $table");
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)) {
$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($table) {
$content="";
$result = mysql_query("SELECT * FROM $table");
while($row = mysql_fetch_row($result)) {
$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 .= "'".addslashes($row[$j])."',";
else $insert .= "'',";
}
$insert = ereg_replace(",$","",$insert);
$insert .= ");\n";
$content .= $insert;
}
return $content;
}