Page 1 of 1
mysqldump via CMS
Posted: Fri Jan 27, 2006 7:40 am
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?
Posted: Fri Jan 27, 2006 12:21 pm
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
}
Posted: Mon Jan 30, 2006 3:59 am
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?
Posted: Mon Jan 30, 2006 5:03 am
by Skittlewidth
Nevermind. The host has disabled exec and passthru anyway.
Posted: Mon Jan 30, 2006 10:50 am
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?
Posted: Mon Jan 30, 2006 2:40 pm
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.