mysqldump via CMS

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
Skittlewidth
Forum Contributor
Posts: 389
Joined: Wed Nov 06, 2002 9:18 am
Location: Kent, UK

mysqldump via CMS

Post by Skittlewidth »

I'm looking at the MySQL manual and I'm a bit confused.

I want to do a mysqldump of a database, which I'm used to doing from the commandline.
I need the user to be able to click a link in their cms which will trigger the script and either save the .sql file to the server or email it to the administrator. I'm told its not as simple as putting the mysqldump command into a mysql query.

My colleague tells me there are plenty of scripts available that will do this for me, but can't seem to recommend any. Anyone got any ideas or recommendations?
sheila
Forum Commoner
Posts: 98
Joined: Mon Sep 05, 2005 9:52 pm
Location: Texas

Post by sheila »

Code: Select all

$command = "/usr/bin/mysqldump --opt -h$dbhost -u$dbuser --password=$dbpass $dbname >$backup_file";
passthru($command,$error);
if ($error) {
    echo "<p>An error occurred. Unable to create a backup of the database</p>";
} else {
    // code to email the file or display a link to it
}
User avatar
Skittlewidth
Forum Contributor
Posts: 389
Joined: Wed Nov 06, 2002 9:18 am
Location: Kent, UK

Post by Skittlewidth »

I was hoping it might be something like that, but I'm having trouble with passthru()

The database server is different to the hosting server, and I don't know the full path to mysqldump.
The hosting company only gives you the details to connect to the database so I'm not sure how I could find that path out.

I'm getting the following error at the moment:

Code: Select all

<b>Warning</b>:  passthru() has been disabled for security reasons in <b>/home/fhlinux202/i/******/user/htdocs/*****/admin/index.php</b> on line <b>10</b><br />
Any advice anyone?
User avatar
Skittlewidth
Forum Contributor
Posts: 389
Joined: Wed Nov 06, 2002 9:18 am
Location: Kent, UK

Post by Skittlewidth »

Nevermind. The host has disabled exec and passthru anyway.
User avatar
Skittlewidth
Forum Contributor
Posts: 389
Joined: Wed Nov 06, 2002 9:18 am
Location: Kent, UK

Post by Skittlewidth »

I've made a sort of work around to the database problem (a bit longwinded but it should keep the client happy).

My personal database backup routine automatically ftps a copy of the most recent site backup to the clients hosting server and he can then use a link to download the most recent version, almost as if he generated it himself.

When he clicks on the link a force download header triggers the save dialog box etc etc.

I don't think I have the headers quite right though as the download file, although complete comes down as a mess of text rather than formatted as the original file is.
This is what I've put:

Code: Select all

$file = "../backup.sql";
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=\"".basename($file)."\";");
header("Content-Transfer-Encoding: binary");
set_time_limit(0);
@readfile($file) or die("File not found.");
What should I change to download a plain text file with linebreaks preserved?
sheila
Forum Commoner
Posts: 98
Joined: Mon Sep 05, 2005 9:52 pm
Location: Texas

Post by sheila »

This is what works for me

Code: Select all

//-- get full filesystem path to the file
$fullPath =  $backup_dir . '/' . $filename;

//-- translate file name for Internet Explorer.
if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")){
    $filename = preg_replace('/\./', '%2e', $filename, substr_count($filename, '.') - 1);
}

//-- make sure the file exists before sending headers
if(!$fdl = @fopen($fullPath,'r')){
    die("Cannot Open File!");
} else {
    header("Cache-Control: ");// leave blank to avoid IE errors
    header("Pragma: ");// leave blank to avoid IE errors
    header("Content-type: application/octet-stream");
    header("Content-Disposition: attachment; filename=\"".$filename."\"");
    header("Content-length:".(string)(filesize($fullPath)));
    sleep(1);
    fpassthru($fdl);
}
I don't remember where I found this so don't know what the comments about IE errors mean.
Post Reply