Jump to content

Recommended Posts

This is a MySQL database backup script witch will email you the backup using smtp auth

I would use mysqldump and mail() but my host wont let me

This Script is functional but im not sure it actually should be

apache 2.2.9 php 5.2.6 mysql 5.0.51b

Thanks: Aaron McMurray

<?php
/*
My Database Mailer was writen by Aaron St.Clair McMurray
This file is part of My Database Mailer.

My Database Mailer is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

My Database Mailer is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with My Database Mailer.  If not, see <http://www.gnu.org/licenses/>.
*/
require_once "Mail.php";
require_once('Mail/mime.php');

/*************** Modify database settings ***************/

$Host = "MySQL-HOST";
//$Host = $_POST["Host"] or die("No Host");

$UserName = 'MySQL-User-Name';
//$UserName = $_POST["UserName"] or die("No UserName");

$Password = 'MySQL-Password';
//$Password = $_POST["Password"] or die("No Password");

$Database = 'MySQL-Database';
//$Database = $_POST["Database"] or die("No Database");

$tmpDir = "/tmp/"; //You May Want To Replace /tmp/ With A Directory Writable To The User Running This Script
/*************** Modify email settings ***************/
//$from = $_POST["From"] or die("No From Address");
//$to = $_POST["To"] or die("No To Address");

$from = "Sender <[email protected]>"; //Replace [email protected] With Your Mail Address
$to = "Recipient <[email protected]>"; //Replace [email protected] With Recipients Mail Address
$subject = "$Database - backup";

$ehost = "smtp.example.com"; //Replace smtp.example.com With Your SMTP Server Address
$eusername = "[email protected]"; //Replace [email protected] With Your SMTP User Name
$epassword = "Password"; //Replace Password With Your SMTP Password

/* !!!! Do not modify below this line Unless You Know What You Are Doing !!!! */

$connection = mysql_connect("$Host","$UserName","$Password");
mysql_select_db($Database, $connection);
$sql = 'SHOW TABLES FROM '.$Database;
$result = mysql_query($sql);
$contents = "-- Created By: My Database Mailer\n-- Copyright: Aaron St.Clair McMurray\n-- http://www.ourfreeinfo.com\n\n-- Database: ".$Database."\n-- Created: ".date('M j, Y')." at ".date('h:i A')."\n\n";
while ($tables = mysql_fetch_array($result)) {
    $TableList[] = $tables[0];
}
foreach ($TableList as $table) {
    $row = mysql_fetch_assoc(mysql_query('SHOW CREATE TABLE '.$table));
    $contents .= $row["Create Table"].";\n\n";
    $sql = 'SELECT * FROM '.$table;
    $result = mysql_query($sql);
    $columns = explode(',',$row["Create Table"]);
    $i = 0;
    while ($records = mysql_fetch_array($result)) {
        $contents .= "INSERT INTO ".$table." VALUES (";
        for ($i=0;$i< count($records)/2;$i++) {
            if ($i < count($records)/2-1) {
                if (strstr($columns[$i],"varchar") || strstr($columns[$i],"text")) {
                    $contents .= "'".$records[$i]."',";
                } else {
                    $contents .= $records[$i].",";
                }
            } else {
                if (strstr($columns[$i],"varchar") || strstr($columns[$i],"text")) {
                    $contents .= "'".$records[$i]."'";
                } else {
                    $contents .= $records[$i]."";
                }
            }
        }
        $contents .= ");\n";
        $i++;
    }
    $contents .= "\n";
}
$sqlFile = "$tmpDir"."DB_Backup_".$Database."_".date('Y-m-d').".sql";
$handle = fopen($sqlFile,'w');
fwrite($handle,$contents);

$attachment = "$tmpDir"."DB_Backup_".$Database."_".date('Y-m-d').".tgz";
$createZip = "tar cvzf $attachment $sqlFile";
exec($createZip);


$headers = array ('From' => $from, 'To' => $to, 'Subject' => $subject);


$textMessage = $attachment;
$htmlMessage = "";

$mime = new Mail_Mime("\n");
$mime->setTxtBody($textMessage);
$mime->setHtmlBody($htmlMessage);
$mime->addAttachment($attachment, 'text/plain');
$body = $mime->get();
$hdrs = $mime->headers($headers);
$mail = Mail::factory('smtp',
    array ('host' => $ehost,
    'auth' => true,
    'username' => $eusername,
    'password' => $epassword));
$mail->send($to, $hdrs, $body);

unlink($sqlFile);
unlink($attachment);


if (PEAR::isError($mail)) {
  echo("<p>" . $mail->getMessage() . "</p>");
} else {
  echo("<p>$sqlFile Message successfully sent!</p>");
}

mysql_close($connection);

echo("<p style=\"font-size: xx-small\">
&copy Aaron St.Clair McMurray<br />
<a href=\"http://www.ourfreeinfo.com\" title=\"My Database Mailer\">www.ourfreeinfo.com</a>
</p>");

?>

Link to comment
https://forums.phpfreaks.com/topic/117928-need-help-debugging-script/
Share on other sites

I wrote this script and it worked but i realy dont think it should

Because the web user has no priv to any command that directly affects mysql such as mysqldump, mysqladmin, mysql etc

 

I have tried to log in as web user and run these commands via shell commands and priv is denied

so what makes this work and why

 

I know that sounds redikulous

Your code doesn't use any system commands (mysqldump, mysqladmin etc) , it just queries the database to return the current tables and their table structure. From which it'll populate a .sql file for your database backup. Which in turn emails the backup.

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.