Samuel Baldus
Samuel Baldus

Reputation: 21

PHP MySQL Backup script on IIS Server

Bit of a newbie, so please excuse the lack of terminology. . . . I have a PHP script to backup a MySQL database "dbjobs". I've tried nearly everything I can but can't get it to work. It works if I run the $command directly from the Command Prompt on the server, but everytime I try to run the PHP version, I get an HTTP 500 error.

<?php
$backupFile = "DBJobs_" . date("Y-m-d");
$command = "\"mysqldump.exe\" --opt -hlocalhost -uUser -pPasswword dbjobs > c:/backup.sql";
$result = system($command);

if ($command !== false) {
echo "<p>Backup file created!</p>";
}
else {
  echo "<p>There was a problem!</p>";
}
?>

I have tried the exec() function instead of system() but still does the same. Does anyone know where I am going wrong?

Thanks

Upvotes: 1

Views: 1252

Answers (3)

rubayeet
rubayeet

Reputation: 9400

You have to grant read/execute permissions to the account under which IIS is running on C:\WINDOWS\System32\cmd.exe. Check this out.

Upvotes: 0

Phill Pafford
Phill Pafford

Reputation: 85348

A couple of options to try are:

  • full path to the mysqldump.exe in the $command line of code
  • You might try backticks $command instead of system (backticks key also has the tilda symbol when you shift)
  • Could be a permissions issue???

Upvotes: 0

Joanne C
Joanne C

Reputation: 1115

It's very likely both a path issue (the web server doesn't know where the executable is) and a permission issue. Typically, IIS isn't able execute a shell (cmd.exe) and so isn't going to be able to run the executable, so it would have to have permissions to get a shell and to run the MySQL utility.

Your other alternative is to have the database do it for you with a select statement that writes to an output file.

Upvotes: 1

Related Questions