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.

OrangeRind
  • 4,798
  • 13
  • 45
  • 57
  • Show us the values of all these variables or at least the `$backup_file_location` – RiggsFolly Sep 19 '16 at 23:15
  • We have no idea what the values of your PHP variables are, for example `$mysqldump_location` and `$backup_file_location` and others. I suggest you log the final command in `$command` and see if it's what you expect. – Bill Karwin Sep 19 '16 at 23:16
  • all of the above and make sure php has the permission to write to that directory –  Sep 19 '16 at 23:18
  • Try using shell_exec() instead of exec and echo whatever it returns – Stas Parshin Sep 19 '16 at 23:18
  • @RiggsFolly Added the variable values – OrangeRind Sep 19 '16 at 23:21
  • @BillKarwin the final command in the variable is correct - because I can successfully execute it directly through the terminal. – OrangeRind Sep 19 '16 at 23:22
  • 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()](http://php.net/manual/en/function.exec.php) to capture the output and the exit status of the command. – Bill Karwin Sep 19 '16 at 23:30
  • @StasParshin I tried print_r() on the return value from shell_exec() - it prints absolutely no character. – OrangeRind Sep 19 '16 at 23:31
  • Not the return value from `exec()` function -- the exit status of the command. You have to capture that in the 3rd argument. Please refer to the documentation for http://php.net/manual/en/function.exec.php. – Bill Karwin Sep 19 '16 at 23:32
  • @BillKarwin yeah the 3rd argument shows 1 as the return value. The second output argument shows a blank array. I'm beginning to suspect this is a privileges issue as you suggested. Let me check on that first. – OrangeRind Sep 19 '16 at 23:36
  • 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. – Bill Karwin Sep 19 '16 at 23:39
  • 2
    More tips on handling stderr with `exec()`: http://stackoverflow.com/questions/2320608/php-stderr-after-exec – Bill Karwin Sep 19 '16 at 23:40
  • 1
    @BillKarwin Indeed the problem seemed to be with write privileges. Just tried with a 777. Working fine. Thanks! – OrangeRind Sep 19 '16 at 23:46
  • Possible duplicate of [PHP regular backup of mysql data](http://stackoverflow.com/questions/38916163/php-regular-backup-of-mysql-data) – e4c5 Sep 20 '16 at 06:49

1 Answers1

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.

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828