TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Backing up a Database NOT via PHPMyAdmin (http://www.talkphp.com/absolute-beginners/1957-backing-up-database-not-via-phpmyadmin.html)

Gareth 01-14-2008 09:27 PM

Backing up a Database NOT via PHPMyAdmin
 
Hello.

I would like to be able to back up parts of my database by providing a form / link which will give the admin the choice to download the file in a sql format (like in IPB).

I have researched this a little; and been succesful in being able to backup the database into a location on the server where I define, but I'm not sure how I can change it into being download-able (obviously if it is downloaded I don't want it on the server anymore...).

This is what I have so far (I have commented it):

PHP Code:

<?php
include 'conf_global.php'// File with database settings

$tableName  'a_table'// The table which the admin has chosen to backup (Static for exemplory reasons)
$backupFile 'backup/DATE_TABLE.sql'// Where the sql is places
$query      "SELECT * INTO OUTFILE '$backupFile' FROM $tableName"// Back up table into file
$result mysql_query($query); // Query it

if ($result){
    echo 
'Database Backed Up';    // If succesful
}
else {
    echo 
'Database NOT Backed Up'// If not..
}
?>

Can someone help me please?
Much appreciated.
Gareth

Alan @ CIT 01-14-2008 10:01 PM

Hi Gareth,

Completely un-tested, but something like the following should do the trick

PHP Code:

header('Content-type: text/plain');
header('Content-Disposition: attachment; filename="backup.sql"');
readfile($backupFile); 

That should popup a download dialouge (default name of backup.sql - might want to change that to include the date or something).

Alan.

Village Idiot 01-14-2008 10:18 PM

That should work.

Gareth 01-14-2008 10:39 PM

Thanks! Should this go at the top?

RobertK 01-14-2008 10:58 PM

That needs to be present after the query is known to be successful; there shouldn't be any output up to or past that point. ;-)


All times are GMT. The time now is 02:58 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0