1

I'm trying to have MySQL backup through mysqldump and the command works in the command-line but it returns 2 from exec().

Here's the code I'm using:

$filename = "/path/to/backup/dbBackUp".date("Y-m-d").".sql";
$backupcmd = "/usr/bin/mysqldump -u USERNAME -pPASSWORD -A --single-transaction >".$filename;
exec($backupcmd, $output, $return);

Vardump of $output is array(0) { } and $return = 2.

Dharman
  • 30,962
  • 25
  • 85
  • 135
KingMalaka
  • 23
  • 5
  • 1
    Is the webserver running this? Does the webserver user have permission for `/path/to/backup/`? – AbraCadaver Oct 25 '19 at 15:59
  • This won't fix your error, but might help you figure out what the issue is. https://stackoverflow.com/a/16665146/1007220 – aynber Oct 25 '19 at 15:59
  • As @AbraCadaver said, apache is running your code (except if you're executing php /path/to/your/script.php instead of loading a web page) and it probably doesn't have permissions to read/write that folder or even to execute mysqldump – dgtal Oct 25 '19 at 16:01
  • So look for the way to give your process serving php the permissions to write that folder. It depends on what system you're on (CentOS, Ubuntu, Windows etc.) – dgtal Oct 25 '19 at 16:03

1 Answers1

1

As it turns out, www-data didn't have write access to folder I was storing the backups to. Thanks for your help.

KingMalaka
  • 23
  • 5