Backup the database by clicking on a link

Asked

Viewed 582 times

1

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

  • 1

    And why do you need Phpmyadmin? It’s a little difficult to understand your question friend, has how to explain better what you need?

  • I’m developing a web application, right, in phpmyadmin I have in the database after selecting it I can click export, it generates a script of it with its contents, I want this function exactly, but on another page, in the same way generating this script and downloading it automatically. Instead of having to log into PHPMYADMIN and click there...

  • Phpmyadmin is not the "local" that is stored the data, it is just a CLIENT manager, ie it is just a software to access the mysql server via PHP-API. If you are creating backup software, the only thing you will need is to use PHP-API, in this case mysqli or pdo. Note that you can use phpmyadmin, but it is written under a GNU license and you should be careful about using the software inappropriately. I really don’t mean to offend you, but you need to learn the least language you can use. :)

  • I am quoting the PMA only as an example. My doubt itself is a way in which I can generate my database script for backup, whether there is already any API that facilitates or ...?

  • When I mentioned the API I am referring to the PHP itself, as the mysqli which is a function or class of php itself, we call the API, because it is part of an "Extension" that can be used or not in php. This is why I said that you should learn the minimum of the language you will use, as you are confusing most of the characteristics of this language.

1 answer

1

Mysql comes with a utility exactly for this, which is the 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;

It is also possible to do the same without generating the file on disk, with the passthru, which is very similar to exec but redirects the output to PHP. Example taken from a reply from Soen:

<?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);
?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.