Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 422
  • Last Modified:

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

0
rae_rae
Asked:
rae_rae
  • 7
  • 5
1 Solution
 
Beverley PortlockCommented:
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.
0
 
rae_raeAuthor Commented:
Sorry for the late response.
Yes, it works and no, there is no disable functions set. "disable_functions =" in my php.ini file.
0
 
Beverley PortlockCommented:
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.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Beverley PortlockCommented:
Oops... that bit near the end should have read

echo "output is '$output' ";

Sorry.
0
 
rae_raeAuthor Commented:
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
0
 
Beverley PortlockCommented:
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.
0
 
rae_raeAuthor Commented:
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.
0
 
Beverley PortlockCommented:
TBH, we've covered all the obvious causes. Time to think of non-obvious stuff. Is safe_mode enabled?
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
.... 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...
0
 
Beverley PortlockCommented:
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.
0
 
rae_raeAuthor Commented:
Yes, that's correct bportlock...it is inside the web root. BTW, safe_mode = Off
0
 
Beverley PortlockCommented:
Indeed, but what AngelIII was thinking of was the TEMP folder which *is* outside the web root. Having said that, on the few ocassions I've used Windows writing things to TEMP has never been a bother.

Having looked again at the Windows code, I noticed there are some double quotes in there that I did not expect to see. What happens if you remove them? Instead of

       $command = "\"{$output[0]}\" -f -C -K --host={$host} --user={$user} --password={$pass} $dbname >\"{$backup_path}\"";
                 
use

       $command = "{$output[0]} -f -C -K --host={$host} --user={$user} --password={$pass} $dbname > {$backup_path}";
                 
0
 
rae_raeAuthor Commented:
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!!
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now