Advanced Search

Results 1 to 8 of 8

Thread: Send PHP vars to run an SQL dump?

  1. #1
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,486
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default Send PHP vars to run an SQL dump?

    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:

    Code:
    <?php
    $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:

    Code:
    $filename = "/path/to/backup" . date("d") . ".sql";
    Will end up looking like this:

    Code:
    $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?
    {CWoT - Riddle } {OSTU - Psycho} {Invasion - Team}
    Follow Me on Twitter: @Negative_Chaos
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    You mean like this:
    Code:
    <?php
    $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."
    Last edited by Twey; 07-30-2006 at 10:56 PM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,486
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default

    Might be I will give it a go...

    btw.. what do these do?:

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

    Code:
    ' . shell_escape
    {CWoT - Riddle } {OSTU - Psycho} {Invasion - Team}
    Follow Me on Twitter: @Negative_Chaos
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Eh? I put konq-op.html in that command?
    (edits quickly)
    You didn't see that, OK?

    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.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,486
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default

    I didn't see anything.

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

    Thanks for the info about shell_escape()
    {CWoT - Riddle } {OSTU - Psycho} {Invasion - Team}
    Follow Me on Twitter: @Negative_Chaos
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

  6. #6
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,486
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default

    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:

    Code:
    $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!!!
    {CWoT - Riddle } {OSTU - Psycho} {Invasion - Team}
    Follow Me on Twitter: @Negative_Chaos
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

  7. #7
    Join Date
    Sep 2005
    Posts
    882
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

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

  8. #8
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,486
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default

    Ahh! Thanks!
    {CWoT - Riddle } {OSTU - Psycho} {Invasion - Team}
    Follow Me on Twitter: @Negative_Chaos
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •