View Full Version : Send PHP vars to run an SQL dump?

07-30-2006, 12:41 PM
Not sure really what I am asking here... I will try to explain it best I can.

I am sure my not even knowing what exactly I want is why I can figure this out.

I have a php script that, when run, will make a MySQL dump of a database of mine.

What I want is for this script:

$emailaddress = "name@site.com";
$host="XXXXXXX"; // database host
$dbuser="XXXXXXX"; // database user name
$dbpswd="XXXXXX"; // database password
$mysqldb="XXXXXX"; // name of database
$filename = "/path/to/backup" . date("d") . ".sql";
if ( file_exists($filename) ) unlink($filename);
system("mysqldump --user=$dbuser --password=$dbpswd --host=$host $mysqldb > $filename",$result);
$size = filesize($filename);
switch ($size) {
case ($size>=1048576): $size = round($size/1048576) . " MB"; break;
case ($size>=1024); $size = round($size/1024) . " KB"; break;
default: $size = $size . " bytes"; break;
$message = "The database backup for " . $mysqldb . " has been run.\n\n";
$message .= "The return code was: " . $result . "\n\n";
$message .= "The file path is: " . $filename . "\n\n";
$message .= "Size of the backup: " . $size . "\n\n";
$message .= "Server time of the backup: " . date(" F d h:ia") . "\n\n";
mail($emailaddress, "Database Backup Message" , $message, "From: Website <>");

to be on a page... let's call it dump.php

I want to browse to setup.php, once I am there I want to have input boxes.

One for $email
One for $filename (kind of.. see below)
and a submit button.

Now when I put in BLiZZard@BLiZZ.com
and BLiZZ
then click submit,

I want dump.php to get those 2 vars and replace them in the script.

HOWEVER... the $filename can't really change, as it is the path, but the "backup" part can.

So using the above example, this line:

$filename = "/path/to/backup" . date("d") . ".sql";

Will end up looking like this:

$filename = "/path/to/BLiZZ" . date("d") . ".sql";

also $email would change to BLiZZaRD@BLiZZ.com

I am thinking I need $filename line to instead of saying /backup I need a new variable like /$name...

Am I even making sense?

07-30-2006, 02:09 PM
You mean like this:
$emailaddress = $_POST['email'];
$host='XXXXXXX'; // database host
$dbuser='XXXXXXX'; // database user name
$dbpswd='XXXXXX'; // database password
$mysqldb='XXXXXX'; // name of database
$filename = '/path/to/' . shell_escape($_POST['filename']) . date('d') . '.sql';
if ( file_exists($filename) ) unlink($filename);
shell_exec("mysqldump --user=$dbuser --password=$dbpswd --host=$host $mysqldb > $filename",$result);
$size = shell_exec('du -ch \'' . str_replace('\'', '\\\'', $filename) . '\'|grep \'total\'|awk \'{ORS="";print $1}\'');
$message = 'The database backup for ' . $mysqldb . " has been run.\n\n" .
'The return code was: ' . $result . "\n\n" .
'The file path is: ' . $filename . "\n\n" .
'Size of the backup: ' . $size . "\n\n" .
'Server time of the backup: ' . date(' F d h:ia') . "\n\n";
mail($emailaddress, 'Database Backup Message', $message, 'From: Website <>');
?>Takes two POST variables, "filename" and "email."

07-30-2006, 10:51 PM
Might be :) I will give it a go...

btw.. what do these do?:

$size = shell_exec('du -ch konq-op.html|grep \'total\'|awk \'{ORS="";print $1}\'');


' . shell_escape

07-30-2006, 10:59 PM
Eh? I put konq-op.html in that command?
(edits quickly)
You didn't see that, OK? :p

shell_escape() is a function that escapes a string so it can be safely used on the shell. The shell_exec() command simply uses the standard UNIX du(1) command to get the size of the file, since it will select the right units automatically (given the -h switch) which is a lot prettier than your attempt at doing the same thing. :p

07-31-2006, 12:00 PM
I didn't see anything.

Off to try this thing out now... let's see how bad I can screw it up :p

Thanks for the info about shell_escape() :D

07-31-2006, 01:03 PM
Okay I have it workng, so now it is time for the tweaks.

The first one is I get an error, because I have changed the $message. This line:

$message .= "The file name was: " . $name" . date("d") . ".sql . "\n\n";

is what gets the error.

basically what I am after is the email will say:

The file name was : BLiZZ31.sql

Strings get me... spaces and " and . and where to not use them.. GAHHHH!!!

08-02-2006, 12:40 PM
$message .= 'The file name was: '.$name.date('d').'.sql \n\n';

08-02-2006, 07:54 PM
Ahh! Thanks!