Аватар пользователя Dima

The useful PHP script to simply build MySQL dump (on unix-like systems)

It is so easy to get the DB dump if you have access to PHP but don't have direct access to MySQL admin.
Here is a simple script to do this.

Set, database connection run the script.
Script:

  1. // Set the connection details.
  2. $username = "";
  3. $password = "";
  4. $hostname = "";
  5. $dbname = "";
  6. $port = '';
  7.  
  8. // if mysqldump is on the system path you do not need to specify the full path
  9. $command = "mysqldump --host=$hostname --user=$username ";
  10. if (!empty($password)) {
  11. $command.= "--password=". $password ." ";
  12. }
  13. if (!empty($port)) {
  14. $command.= "--port=". $port ." ";
  15. }
  16. $command.= $dbname;
  17. $dump = shell_exec($command);
  18.  
  19. // Send dump file to the output
  20. header('Content-Description: File Transfer');
  21. header('Content-Type: application/octet-stream');
  22. header('Content-Disposition: attachment; filename='.basename($dbname . "_" .
  23. date("Y-m-d_H-i-s").".sql"));
  24.  
  25. echo $dump;
  26. exit();

Also it can be used in Drupal, with Devel module, on /devel/php page. Simply paste and execute the script.

TIP:
To get DB information, if you dont have an access to settings.php but have access to /devel/php, just execute simple code:
dpm($GLOBALS['databases']);