Is it possible to use PHP and a mysql query combined to allow a whole database backup file to be downloaded?

For example, I'm currently backup up my database by using the export tool in phpmyadmin, but what if I wanted to add a button or link on my site which would essentially allow me to do the same thing?

The backup has to appear as an SQL file btw

Recommended Answers

All 2 Replies

Is it possible to use PHP and a mysql query combined to allow a whole database backup file to be downloaded?

For example, I'm currently backup up my database by using the export tool in phpmyadmin, but what if I wanted to add a button or link on my site which would essentially allow me to do the same thing?

The backup has to appear as an SQL file btw

Hello,

If you follow the normal standards for config files for php something like this should work.

Source code : config.php , opendb.php, backitup.php

<?php
// This is an example of config.php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$dbname = 'phpcake';
?>
<?php
// This is an example opendb.php
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die                      ('Error connecting to mysql');
mysql_select_db($dbname);
?>
<?php
// This is an example of backitup.php
include 'config.php';
include 'opendb.php';

$backupFile = $dbname . date("Y-m-d-H-i-s") . '.gz';
$command = "mysqldump --opt -h $dbhost -u $dbuser -p $dbpass $dbname | gzip > $backupFile";
system($command);
include 'closedb.php';
?>
Member Avatar for diafol

system() may be disabled on your webserver for security reasons. Worth a try though.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.