3

I am trying to create periodic backups (poor man's cron) of my database using mysqldump with exec() function. I am using XAMPP/PHP7 on macOS.

$command = "$mysqldump_location -u$db_user -h$db_host -p$db_password $db_name > $backup_file_location";
exec($command);

When I run the PHP script, I get no SQL dump in the path mentioned in $backup_file_location but if I execute the same $command string on the terminal directly I get the desired SQL file in the desired location.

I am unable to understand what could be the problem here. Also open to suggestions on better ways to dump the entire DB.

Edit 1:

The value of $mysqldump_location is /Applications/XAMPP/xamppfiles/bin/mysqldump

The value of $backup_file_location is /Applications/XAMPP/xamppfiles/htdocs/app5/data/sqldumps/sql_data.sql

/app5/ is the folder in while I am developing my app.

Edit 2: Possible duplicate suggestion does not apply since the issue here was not on how to dump SQL backups. The key issue here was that the backup using mysqldump was working through terminal, but not through PHP's exec() function.

14
  • Show us the values of all these variables or at least the $backup_file_location Commented Sep 19, 2016 at 23:15
  • 2
    Does your web page execute in XAMPP as a user who does not have write privileges to the sqldumps directory? You can use the optional 2nd and 3rd args to exec() to capture the output and the exit status of the command. Commented Sep 19, 2016 at 23:30
  • 3
    This is one downside of PHP's exec(): it's hard to capture the stderr output, which is what you might need to troubleshoot this error. One workaround is to write a shell script that runs your backup, and exec() your shell script. Then you have more opportunity to handle the stderr output in the shell script, logging it or whatever. Commented Sep 19, 2016 at 23:39
  • 2
    More tips on handling stderr with exec(): stackoverflow.com/questions/2320608/php-stderr-after-exec Commented Sep 19, 2016 at 23:40
  • 1
    @BillKarwin Indeed the problem seemed to be with write privileges. Just tried with a 777. Working fine. Thanks! Commented Sep 19, 2016 at 23:46

1 Answer 1

1

The resolution of the issue, from above comments, was that the PHP request executes in XAMPP as a user that has limited privileges, and the mysqldump process inherits those privileges.

Checking the exit status of the process run by exec() confirmed that mysqldump exited with a nonzero exit status, indicating it failed for some reason.

Opening write privileges to 777 on the directory where the mysqldump process tries to write resolved the error.

It should also be adequate to figure out the specific uid & gid of Apache processes (check the User and Group config values in the Apache config file (e.g. xampp-home/apache/conf/httpd.conf) and make the output directory writeable by that uid or gid.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.