Link to home
Start Free TrialLog in
Avatar of rae_rae
rae_raeFlag for United States of America

asked on

Why doesn't mysqldump work in php in a Windows Envinronment?

I have this code....that works correctly...the paths it creates in Windows work too...I can prove this because I copy the command into Windows clipboard and it executes fine in the command prompt....


However...when I run it in PHP, the file created by it is empty.
<?php
require_once '../classes/page.class.php';
 
class dbDump
extends page
{
	public function __construct()
	{
		parent::__construct();
 
		// Instantiate the user object
		$user = new user();
 
		// Make sure the user is logged in properly
		if (!$user->isAuthorized())
		{
			$this->redirect($this->getConfig('login_page'));
		}
 
		// Get requirements and verify they loaded correctly
		if (!stristr($_SERVER['HTTP_REFERER'], 'pages/adminManager.php'))
		{
			$this->append($this->error('Access denied to this location'));
 
			return;
		}
 
		// Get the database credentials
		$db = new db();
		$host = $db->getHost();
		$dbname = $db->getDbName();
		$user = $db->getUserName();
		$pass = $db->getPass();
 
		// Create the dump filename
		$date = date('h:i:s--m-d-Y');
		$backup_path = "../db_dumps/{$host}-{$dbname}-{$date}.sql";
 
		// Attempt the backup for a Linux environment
		$command = "mysqldump -f -C -K --host={$host} --user={$user} --password={$pass} $dbname >{$backup_path}";
		exec($command, $output, $error_code);
 
		// Check for errors
		if ($error_code != 0)
		{
			// Get the path to the mysqldump program
			$command = 'dir /b/s \mysqldump.exe';
			exec($command, $output, $error_code);
 
			// Check for errors
			if ($error_code != 0)
			{
				// Something is wrong but we don't know what
				$this->append($this->error("$command failed with an error code of $error_code"));
 
				return;
			}
 
			// Windows refuses to work with dashes and or colons in the file name
			$date = date('hismdY');
			$base_path = $this->getConfig('base_dir');
			$backup_path = "{$base_path}db_dumps/{$host}{$dbname}{$date}.sql";
 
			// Windows needs back slash paths instead of forward slash paths
			$path_array = explode('/', $backup_path);
			$backup_path = implode('\\', $path_array);
		
			// Attempt the backup for a Windows environment
			$command = "\"{$output[0]}\" -f -C -K --host={$host} --user={$user} --password={$pass} $dbname >\"{$backup_path}\"";
			exec($command, $output, $error_code);
 
			// Check for errrors
			if ($error_code != 0)
			{
				// Something is wrong but we don't know what
				$this->append($this->error("$command failed with an error code of $error_code"));
 
				return;
			}
			if (!file_exists($backup_path))
			{
				// The file that needs to be created cannot be
				$this->append($this->error("The path $backup_path, does not exist"));
 
				return;
			}
			if (sizeof($backup_path) == 0)
			{
				// The file was created but no data was written to it
				$this->append($this->error("The backup file was created but not filled. Try running the command $command from cmd."));
 
				return;
			}
 
		}
 
		header("Location: {$_SERVER['HTTP_REFERER']}");
	}
}
 
$db_dump = new dbDump();
?>

Open in new window

Avatar of Beverley Portlock
Beverley Portlock
Flag of United Kingdom of Great Britain and Northern Ireland image

Does the mysqldump command work on a windows command line? If so then check php.ini and look for "disable_functions". This option stops various commands from being executed and is generally used for stopping rogue scripts. If "Exec" is in the list that may be the source of your problem.
Avatar of rae_rae

ASKER

Sorry for the late response.
Yes, it works and no, there is no disable functions set. "disable_functions =" in my php.ini file.
Check that there is only one php.ini file. Sometimes Windows installations seem to get two of them - one for command line PHP and one for the webserver.

Also consider setting (temporarily) the following in your php.ini and then restart Apache to load the settings.

display_errors = On
error_reporting = E_ALL

There should be stubs to add these to or existing stubs with these commented out. Finally just after your EXEC statement, add an EXIT

                        // Get the path to the mysqldump program
                        $command = 'dir /b/s \mysqldump.exe';
                        exec($command, $output, $error_code);
                       exit;
 
and retest. Hopefully you will get a meaningful error message that will steer us in the right direction. You could also check the value of $command at this point by

echo "command is '$command' ";

If this works OK then move the exit statement to just after the next EXEC and retest. If the EXEC is not working then look at this http://uk.php.net/manual/en/function.exec.php#86438 for using the COM object (solution 3b) to execute statements.
Oops... that bit near the end should have read

echo "output is '$output' ";

Sorry.
Avatar of rae_rae

ASKER

Thanks bportlock,
I followed your directions and found that the correct path to mysqldump is showing. Also, after I actually run the dump command here is my output:

output

Array
(
)

Error
"C:\xampp\mysql\bin\mysqldump.exe" -f -C -K --host=nettekk.db.3821848.hostedresource.com --user=nettekk --password=ch!ckenTak0 nettekk >"C:\xampp\htdocs\xampp\cms\db_dumps\nettekk.db.3821848.hostedresource.comnettekk11035903112009.sql" failed with an error code of 1


The code now also checks that the backup path: C:\xampp\htdocs\xampp\cms\db_dumps\  is_writable() so, I have eliminated that as being the issue.

I even added this to the code:
exec("dir /b/s \mysqldump.exe > {$base_path}db_dumps/test.txt");
and the contents of C:\xampp\htdocs\xampp\cms\db_dumps\test.txt are...
C:\xampp\mysql\bin\mysqldump.exe
Two things:

1 - If that is your actual password showing in the dump above then change it. This is a PUBLIC forum

2 - Error code 1 is very rare and usually relates to problems creating temporary files. At the command line type "SET" and hit ENTER then look for a line like TMP=C:\Temp or TEMP=C:\TEMP. If this line is missing then add it to Windows environment variables and try again.
Avatar of rae_rae

ASKER

Yeah, I kicked myself when I posted the last comment....Thanks I did change my password as soon as I posted. lol.

The output of those vars:
C:\Documents and Settings\rae_rae>echo %tmp% && echo %temp%
C:\DOCUME~1\rae_rae\LOCALS~1\Temp
C:\DOCUME~1\rae_rae\LOCALS~1\Temp

I use these locations pretty regularly for other programs. Does one of these need to point to another location? It seems these variable directives have been changed a lot over the years by Microsoft depending on the OS version. I'm using Windows XP Pro...which is probably most of my problem. When I run this in my Linux distro, I of course, have no issues.
TBH, we've covered all the obvious causes. Time to think of non-obvious stuff. Is safe_mode enabled?
.... q: wasn't it so that php/web pages cannot write files outside what is below the www root, and eventually some preconfigured /tmp (aka TEMP) folders in the apache/iis/web server?
just a thought...
It's a good thought - I'm more used to Linux systems than Windows so there might be IIS issues. With it being a new installation I discounted open_basedir restrictions as well.

Having said that, the target dir seems to be C:\xampp\htdocs\xampp\cms\db_dumps\ which looks like it should be inside the webroot.
Avatar of rae_rae

ASKER

Yes, that's correct bportlock...it is inside the web root. BTW, safe_mode = Off
ASKER CERTIFIED SOLUTION
Avatar of Beverley Portlock
Beverley Portlock
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rae_rae

ASKER

bportlock,
Today is a good day...you helped me solve yet another question...that's two in one day here on EE!! And I have been trying to solve these two questions for around a year now. Thank you thank you!!