0

I am trying to take backup on button click using following code, on every click I get empty database. I tried all articles , but result is same..Can any one help me solve this.

 <?php
 ob_start();

 $username = "root"; 
 $password = ""; 
 $hostname = "localhost"; 
 $dbname   = "cars";

 $command = "D:\\..\\wamp64\\bin\\mysqldump  --add-drop-table host=$hostname
--user=$username ";
 if ($password) 
    $command.= "--password=". $password ." "; 
 $command.= $dbname;
 system($command);

 $dump = ob_get_contents(); 
 ob_end_clean();

// send dump file to the output
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($dbname . "_" . 
date("Y-m-d_H-i-s").".sql"));

flush();
echo $dump;
exit();
?>
1
  • you need to dump to a file, then put that in the downland Commented Oct 9, 2016 at 20:43

1 Answer 1

0

I was going to go through your code but thought I may as well just give you one I made earlier from one of my sites as it's identical to what your asking for. I can tell you at a quick glance though that the reason why your DB was emptying on click is because you were dropping the database in your exec command using --add-drop-table:

How to drop and re-populate mysql databases?

This is the code extracted from one of my backend admin page sites to do random MySQL dumps and backups via broswer when needed. It's very simple but extremly effective if secured properly. The form is set using radio buttons for either download or backup and then executes the local mysqldump from cmdline. Backup will output if it was a success by testing to see if the file has actually backed up and download will download the file which is pretty much put together using the example from the PHP site here. I've shaped it a little so you can add it to your own project

   <?php 


$username = "root";
$password = "root";
$host = "localhost";
$dbname = "new";
$path = "/Applications/MAMP/htdocs/html/test123.sql";
$backup = exec('/Applications/MAMP/Library/bin/mysqldump --user=' . $username . ' --password='. $password .' --host=' . $host . ' ' . $dbname . ' > ' . $path . '');

if (isset($_POST['backup'])) {

        if (file_exists($path)) {

            echo "Backup Success";
            echo "<br>";

        } else {

            echo "Backup failed!";
            echo "<br>";
            echo "Backup of " . $dbname . " does not exist in " . $path;

        }

}

if (isset($_POST['download'])) {

    if (file_exists($path)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="'.basename($path).'"');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($path)); //Whoops...Forgot to change variable!
    readfile($path); //Whoops...Forgot to change variable!
    exit;
} else {

    echo "File does not exist!";

}


}

?>


<!DOCTYPE html>
<html>
<head>
<title>Backup Page</title>
</head>
<body>

        <form method="post">

            <input type="radio" name="backup"> Backup <br>
            <input type="radio" name="download"> Download <br>
            <input type="submit" value="MySQL Backup">

        </form>

</body>
</html>
Sign up to request clarification or add additional context in comments.

4 Comments

Hi.. @kitson88 ... Again the problem is same..I am able to download and take backup , and successful msg also gets displayed. But Files are empty..I am using wamp server..thus I changed path for $path & $backup.. Can you please help in that..?
@DhruviMistry Apologise, I've just updated the code so it works, basically I forgot to update the variables on the Content length and readfile() (see comments) so it's working perfect now. Let me know if you have any further issues.
$path = "/Applications/MAMP/htdocs/html/test123.sql"; $backup = exec('/Applications/MAMP/Library/bin/mysqldump --user=' . $username . ' --password='. $password .' --host=' . $host . ' ' . $dbname . ' > ' . $path . ''); I hv confusion in this two points..Can u plz clear that..or share any related links..?
Path: is where the file is stored and $backup runs the function exec which runs the MySQLdump. If you browse directly to the file in path, is the file there? Is so, delete and run again.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.