Database backup when clicking on a link

1

I need a superuser on my system to export the database by clicking "backup", and force the download of the script from PHP as phpMyAdmin does. It is possible? If yes, how? If not, is there any way I can do something similar?

    
asked by anonymous 24.03.2015 / 03:19

1 answer

1

MySQL comes with a utility just for that, which is mysqldump . Locate this file on the machine and save its path. In PHP you can run this utility with exec , and save the backup to disk. Saving in a location accessible by webserver , just point the browser there. Example:

<?php
$caminhoDoMysqldump = "caminho/do/mysqldump/no/seu/servidor";
$usuario = "usuário do mysql";
$senha = "senha do mysql";
$banco = "seu_banco";
$saida = "/var/www/meuSite/arquivos/backup.sql"; // por exemplo

// Gera o backup e salva em disco
exec("$caminhoDoMysqldump --user=$usuario --password=$senha $banco > $saida");

// Redireciona o browser para o arquivo gerado
header('Location: http://www.meusite.com/arquivos/backup.sql');
exit;

You can also do the same without generating the file on disk, with passthru , which is very similar to exec but redirects the output to PHP. Example taken from a SOen response :

<?php

$DBUSER="user";
$DBPASSWD="password";
$DATABASE="user_db";

$filename = "backup-" . date("d-m-Y") . ".sql.gz";
$mime = "application/x-gzip";

header( "Content-Type: " . $mime );
header( 'Content-Disposition: attachment; filename="' . $filename . '"' );

$cmd = "mysqldump -u $DBUSER --password=$DBPASSWD $DATABASE | gzip --best";   

passthru( $cmd );

exit(0);
?>
    
24.03.2015 / 04:07