Lara Ch Lara Ch - 6 months ago 15
MySQL Question

Export SQL structure and data from PHPMyAdmin inside web application

I have a client that use my web application for debts control. He wants to save and backup all data so if something happened to his laptop, he could repair his data.

I told him to enter to phpmyadmin and click on export, but he think that it is little bit complicated for him and for his employees.

So I need to add a new button to the app so he can click on it and export an sql file that contain all data and all database tables and structure using PHP PDO of a specific database (not all phpmyadmin dbs).

Is it possible ? I searched and couldn't find something like that.

Answer

Add a button and on the back-end execute this MYSQL command line command using exec of php on button click.

mysqldump -u'username' -p'password' database_name > /path/backup.sql

You can download the file directly in browser using the code given below

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