|
-
Oct 6th, 2002, 08:08 PM
#1
Thread Starter
Frenzied Member
:Edited: Database Backup - EMail an attachment?
I am running on a win 2k pro server, and (unfortuneately) the closest i have to "Cron-Jobs" is to set my browser to launch to a specific URL @ a certain time.
So what i want to know...
Does any one know of a php script, that will connect to my MySQL server (same machine) make a copy of my Db (back slashes to enclose tables, Add Drop Table if it exsists, etc)
to a .sql file, and then email me the SQL file, when the script is called VIA:
http://my-domain.com/private/backup....e=*MyDatabase*
It will be on the same HTTP host etc,, and always send to my email address,,
IF any one has a script that can do this, then COOL , if not, How the heck would i go about it?
Cuz my Friend, (Totally sad for him) lost like 36 hours work on his site, cuz his DB corrupted on him,
and i dont want the same to happen to me :@
Well any hows,
If you know of a php script to do this, i can write a quick application in VB to navigate for it to send all my db's at the same time, and maybe on my other system (cuz i have a MySQL server running on two servers here )
And lol, you would think if their in ur own home, it would be easier to back up,
well u jst dont want to have the hassel huh,,
BTW: if u need my specs:
Apache 1.3.26
MySQL Release: mysql-3.23.31
PHP: 2.0.2 (or might be 2.2 lol - the latest out ne howz )
Thanks
Last edited by wpearsall; Oct 15th, 2002 at 08:55 PM.
Wayne
-
Oct 7th, 2002, 10:09 AM
#2
Download a copy of PHPmyAdmin
It has everything you'll need do create a DB back up. If you want, you could proly pull apart the page that does the backup scripting and change it to suit your needs.
-
Oct 15th, 2002, 08:54 PM
#3
Thread Starter
Frenzied Member
How do i send an attachment in an email?
I got my script to Save My DB's (Lol, hacked at scripts 
: Now, this is probally one of those, "Long - Tedious" things? :
It will be in the app's directory,
(Writes the file to the directory)
- EMAILS the file (Date taken, Time, Attachment)
(Deletes File)
Now, i know how to email, but not a file.
If u can help, be brill 
Tnx
-
Oct 16th, 2002, 09:40 AM
#4
Frenzied Member
-
Oct 17th, 2002, 09:21 PM
#5
Thread Starter
Frenzied Member
: Still cant fig it out , wanna be a little more specific plz?
-
Oct 17th, 2002, 10:07 PM
#6
Thread Starter
Frenzied Member
What I Have.....
This is what i have:
It all works, Except it doesnt attach the file (god knows why).
I put the Full path in when using it, it writes the file, etc, Not emailing it with it tho,,,
PHP Code:
<?php
error_reporting(E_ERROR);
$email = "[email protected]";
$filename = "E:/Servers/Root/*****/backup.sql"; //Name of the file u want to send as an attachment
$dbname = $_REQUEST[dbname];
$dbuname = $_REQUEST[dbuname];
$dbpass = $_REQUEST[dbpass];
$dbhost = "localhost";
if($_REQUEST['my_pass'] == "pass"){
$logged_in = "1";
//echo HEY;
} else {
die("<html>\nOI!, TELL ME THE PROPER \"SECRET WORD\"\n</html>");
exit;
}
if ($logged_in == 1) {
$content = "";
switch($op) {
case "backup":
@set_time_limit(800);
echo "<HTML>\n<head>\n<title>Backing Up $dbname</title>\n";
echo "</head>\n<body>\n";
echo "Starting Backup Process of \"$dbname\" At ".date("H:i:s")."\n";
echo "<script language=\"JavaScript\"> window.status = \"Starting Backup Process of $dbname At:
".date("H:i:s")."\"</script>";
echo "<br>\n";
echo "Collecting Database Data of \"$dbname\" At ".date("H:i:s")."\n";
echo "<script language=\"JavaScript\"> window.status = \"Collecting Database Data of \\\"$dbname\\\" At:
".date("H:i:s")."\"</script>";
echo "<br>\n";
$crlf="\n";
// English Text
$strNoTablesFound = "No tables found in database.";
$strHost = "Host";
$strDatabase = "Database ";
$strTableStructure = "Table structure for table";
$strDumpingData = "Dumping data for table";
$strError = "Error";
$strSQLQuery = "SQL-query";
$strMySQLSaid = "MySQL said: ";
$strBack = "Back";
$strFileName = "Save Database";
$strName = "Database saved";
$strDone = "On";
$strat = "at";
$strby = "by";
$date_jour = date ("m-d-Y");
// doing some DOS-CRLF magic...
$client = getenv("HTTP_USER_AGENT");
if(ereg('[^(]*\((.*)\)[^)]*',$client,$regs))
{
$os = $regs[1];
// this looks better under WinX
if (eregi("Win",$os))
$crlf="\r\n \n";
}
function my_handler($sql_insert)
{
global $crlf;
$content .= "$sql_insert;$crlf";
}
// Get the content of $table as a series of INSERT statements.
// After every row, a custom callback function $handler gets called.
// $handler must accept one parameter ($sql_insert);
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))
{
// set_time_limit(60); // HaRa
$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);
}
// Return $table's CREATE definition
// Returns a string containing the CREATE statement on success
function get_table_def($db, $table, $crlf)
{
$schema_create = "";
//$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 mysql_die($error = "")
{
$content .= "<b> $strError </b><p>";
if(isset($sql_query) && !empty($sql_query))
{
$content .= "$strSQLQuery: <pre>$sql_query</pre><p>";
}
if(empty($error))
$content .= $strMySQLSaid.mysql_error();
else
$content .= $strMySQLSaid.$error;
$content .= "<br><a href=\"javascript:history.go(-1)\">$strBack</a>";
exit;
}
global $dbhost, $dbuname, $dbpass, $dbname;
mysql_pconnect($dbhost, $dbuname, $dbpass);
@mysql_select_db("$dbname") or die ("Unable to select database");
$tables = mysql_list_tables($dbname);
$num_tables = @mysql_numrows($tables);
if($num_tables == 0)
{
$content .= $strNoTablesFound;
}
else
{
$i = 0;
$heure_jour = date ("H:i");
$content .= "# ========================================================$crlf";
$content .= "#$crlf";
$content .= "# $strName : $dbname$crlf";
$content .= "# $strDone $date_jour $strat $heure_jour $strby Database_Backuper.exe !$crlf";
$content .= "#$crlf";
$content .= "# ========================================================$crlf";
$content .= "$crlf";
while($i < $num_tables)
{
$table = mysql_tablename($tables, $i);
$content .= $crlf;
$content .= "# --------------------------------------------------------$crlf";
$content .= "#$crlf";
$content .= "# $strTableStructure '$table'$crlf";
$content .= "#$crlf";
$content .= $crlf;
$content .= get_table_def($dbname, $table, $crlf).";$crlf$crlf";
$content .= "#$crlf";
$content .= "# $strDumpingData '$table'$crlf";
$content .= "#$crlf";
$content .= $crlf;
get_table_content($dbname, $table, "my_handler");
$i++;
}
}
//Header("Location: admin.php?op=adminMain");
echo "Writing Database Data of \"$dbname\" To File At ".date("H:i:s")."\n";
echo "<script language=\"JavaScript\"> window.status = \"Writing Database Data of \\\"$dbname\\\" To File At:
".date("H:i:s")."\"</script>";
echo "<br>\n";
$file = fopen($filename, "w");
fwrite($file, $content);
fclose($file);
$userfile_type = "Text/HTML";
//MIME code
$boundary = "b" . md5(uniqid(time()));
$mime = "Content-type: multipart/mixed;";
$mime .= "boundary = $boundary\r\n\r\n";
$mime .= "This is a mime encoded message.\r\n\r\n";
//First the regular message
$mime_message .= "--$boundary\r\n";
$mime .= "Content-type: $userfile_type\r\n";
$mime .= "Content-Transfer-Encoding: base64\r\n\r\n";
//$mime .= "\r\n\r\n" . chunk_split(base64_encode($message)) . "\r\n";
//Now the attachment
if(!($fp = @fopen($filename, "r"))){
$error = "Can't open file";
echo $error;
exit;
}
$attach = fread($fp, filesize($filename));
$attach = chunk_split(base64_encode($attach));
$mime .= "--$boundary\r\n";
$mime .= "File Name: $filename\r\n";
$mime .= "Content-type: $userfile_type\r\n";
$mime .= "Content-Transfer-Encoding: base64\r\n\r\n";
$mime .= "\r\n\r\n$attach\n";
echo "Sending Database \"$dbname\" To EMail Address \"$email\" At ".date("H:i:s")."\n";
echo "<script language=\"JavaScript\"> window.status = \"Sending Database \\\"$dbname\\\" To EMail Address
\\\"$email\\\" At ".date("H:i:s")."\"</script>";
mail($email, "Database Backup of ".$dbname." At ".date("H:i:s"), "Your database should be attached!", "From:
Auto_EMail@".getenv("SERVER_NAME")."\n", $mime);
echo "<br>\n";
echo "Deleting \"$filename\" At ".date("H:i:s")."";
echo "<script language=\"JavaScript\"> window.status = \"Deleting \\\"$filename\\\" At ".date("H:i:s")."\"
</script>";
echo "<br>\n";
unlink("backup.sql");
echo "<B>Back up process Complete At ".date("H:i:s")."</B>";
echo "<script language=\"JavaScript\"> window.status = \"Backup Process Complete At ".date("H:i:s")."\"
</script>";
break;
}
}
?>
I did set error reporting to E_ALL, but it doesnt show any errors :shrugs:
Probally something REALLY DUMB?
Hopefully, it will work tho ?
(Codes Split from place to place )
Any how'z,
Tnx
-
Oct 18th, 2002, 09:25 AM
#7
Frenzied Member
it doesn't send because you have to many perameters in the mail tag. the $mime goes into the body not the header.
$body = "our database should be attached!" . $mime;
mail($email, "Database Backup of ".$dbname." At ".date("H:i:s"), $body, "from:
Auto_EMail@".getenv("SERVER_NAME")."\n");
-
Oct 18th, 2002, 09:02 PM
#8
Thread Starter
Frenzied Member
THANK YOU !!!!!
FINALLY WORKING -- Well to a point,, but if this cant be solved, NM, since it doesnt mess up as it does in a normal mail body tag...
Message-ID: < [email protected]>
Date: Sat, 19 Oct 2002 02:54:34 +0000
Subject: Database Backup of php_nuke At 02:54:34
To: [email protected]
From: [email protected] | This is Spam | Add to Address Book
Return-Path: [email protected]
Content-Length: 127981
Your database should be attached!
Content-type: multipart/mixed;boundary = baf245c3bf69901d8553ad2825bde06ca
This is a mime encoded message.
Content-type: Application/SQL
--baf245c3bf69901d8553ad2825bde06ca
File Name: backup.sql
# ========================================================
#
# Database saved : banners
# On 10-19-2002 at 02:54 by MySQL-Exporter.exe!
#
# ========================================================
# --------------------------------------------------------
#
# Table structure for table 'banneradmin'
#
CREATE TABLE banneradmin (
id int(11) NOT NULL auto_increment,
adminuser varchar(15) NOT NULL,
adminpass varchar(15) NOT NULL,
PRIMARY KEY (id),
^ Thank it how it gets sent to me, as in-line text, not as an attached file (IE GIF/Zip/EXE).
Is there a way to solve this (Except alter my mail preferences)?
Thanks tho dood Works like a charm 
(BTW, i changed "$userfile_type" to "Application/SQL" cuz that solved the problem of my server sending as a .txt file, with .Gen files (kinda like .txt files, only for a chat program),,,), but didnt here.
Any hows,
TNX!!!
-
Oct 21st, 2002, 09:15 AM
#9
Frenzied Member
ok now try this
PHP Code:
$userfile_type = "application/sql";
//MIME code
$boundary = "b" . md5(uniqid(time()));
$mime_header = "Content-type: multipart/mixed;";
$mime_header .= "boundary = {$boundary}\r\n\r\n";
$mime_header .= "This is a mime encoded message.\r\n\r\n";
//First the regular message
$mime_message .= "--{$boundary}\r\n";
$mime_message .= "Content-type: {$userfile_type}\r\n";
$mime_message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
$mime_message .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$mime_message .= "{$message}\r\n";
//Now the attachment
if(!($fp = @fopen($filename, "rb"))){
$error = "Can't open file";
echo $error;
exit;
}
$attach = fread($fp, filesize($filename));
$attach = chunk_split(base64_encode($attach));
$file_name = basename($filename); // gets basename of file
$mime_message .= "--$boundary\r\n";
$mime_message .= "Content-Type: $userfile_type; name=\"{$file_name}\"\r\n";
$mime_message .= "Content-Transfer-Encoding:base64\r\n ";
$mime_message .= "Content-Disposition: attachment; filename=\"{$file_name}\"\r\n\r\n";
$mime_message .= "\r\n\r\n$attach\n";
$mime_message .= "--{$boundary}--\r\n";
mail($email, "Database Backup of ".$dbname." At ".date("H:i:s"), $mime_message , $mime_header);
in order to send text you also have to have the "Content-Transfer-Encoding: 7bit" and you also forgot the attachment line. so try that an dlet me know how you get on.
look at what I changed and added
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|