0

So im new to exec and recently wanted to auutomate mysql Dumping via a php script.

My php file originates from

var/WWW/html/tool/script.php

The folder reads root / WWW-data when i do ls -l

It is doing

exec('mysqldump -user=user --password=pass db > selfDir\dump.sql')

Now what i would expect is the for the script to output to dump.sql inside the scripts Folder. This is not Happening though.

Only once i did touch dump.sql and chmod 777 dump.sql was the dump actually being written.

I dont understand why. How can i alter my exec() to make sure it can CREATE the dump.file instead of only writing to it once it already exists ?

thanks

user431806
  • 396
  • 5
  • 17
  • Under which user is PHP running? What permissions does the folder have? – tkausl Sep 19 '18 at 06:34
  • Are you running your script via CLI or webserver? – brombeer Sep 19 '18 at 06:41
  • @Kerbholz it is a Hetzner webserver The Folder permissions arewritten above, root / WWW-data. The php file in question is being run from localhost .. ? – user431806 Sep 19 '18 at 07:49
  • @tkausl already asked (and you didn't answer), so, what are the folders permissions? Try `chmod g+w selfDir` to make it writable for the group (www-data) – brombeer Sep 19 '18 at 07:54

1 Answers1

2

If you only want to automatize the database backup, it's better calling the mysqldump with any other system service, like cron.

Rely on php exec can generate too many errors.

  • Php limit time execution
  • Execute from request, then the request is killed the exec is killed or need to wait all the mysqldump time
  • More difficult permissions
  • Limitation memory in php and the server

In cron you the backup will be (example from The Java Guy):

0 0 * * * mysqldump -u 'username' -p'password' DBNAME > /home/eric/db_backup/liveDB_`date +\%Y\%m\%d_\%H\%M`.sql

Other link for crontab backup with mysql: Answer K1773R mysql-dump-cronjob

Guillem Perez
  • 266
  • 2
  • 9