Hey all! So I'm trying to be a good programmer and backup my data. I've finally figured out how to backup mysql using mysqldump

(mysqldump --user [username] --password=[password] [database] > sql.dump)

How can I take this command and execute it using a php script? Thanks for your time!

-slight

    As a followup, its also stressing me out that the username and password I'm having to put in are my root, i.e. :

    mysqldump --user root --password [root password] [database] >sql.dump

    I'm probably being a bit paranoid but I don't like writing that down in a script! Any suggestions? Thanks for your time.

    -slight

      Well, [man]system/man would probably work for executing the script. Of course, why not just put a PHP shebang line at the top and call the script from cron(8)?

      As for the "root" password being shown, I'd advise granting permission to another user. If that's impossible for some strange reason ... perhaps a look at some sort of reversible encryption technique?

        Oh, and there is nothing wrong with being "paranoid" like that.

        I used to run my own shop, and now work for someone else; and the security practices aren't nearly as well-thought-out as I would like.

          if you want something very rough and ready..

          $datestamp = date("d-m-Y");      // Current date to append to filename
          
          include('config.php');
          //$rt_dir is defined in config as are the db connection values
          // The name (and optionally path) of the dump file
          $relative_filepath="$dbName-$datestamp-sql.gz";
          $root_filepath= $rt_dir."/foldername/".$relative_filepath;
          
          if (file_exists($relative_filepath)) unlink($relative_filepath);
          
          $command = "mysqldump -u$dbUserLogin -h$dbHost --password=$dbPassword $dbName | gzip > $root_filepath";
          $result = passthru($command);
          
          
          $filename = array_pop(explode("/", $root_filepath));
          
          
          echo '<a href="'.$relative_filepath.'">'.$filename.'</a>';
          
          //unlink($root_filepath);   //delete backup file from the server
          

          the "if file_exists" isn't going to do it's job, but whatever, it's a quick hack
          -also after running it, I immediately delete the script off the server

            Write a Reply...