Search code examples
phplaravelbackuptararchlinux

Running tar command from php causes Cannot unlink: Read-only file system


I am trying to implement simple backup feature of some directories (mainly directories in /etc) which is handled by laravel. Basically I store .tar archives containing specific directory files.

This is a command used to create a backup archive of a single directory:

shell_exec("cd {$backupPath} && tar -cf {$dirName}.tar -P {$fullPathToDir}")

This is a command to restore directory from a backup archive:

shell_exec("cd / && sudo tar -xf {$backupPath . $dirName} --recursive-unlink --unlink-first")

For test reasons I let http user run sudo tar, however my initial idea was to create a bash script that will handle that, and add it to sudoers. Running command or shell script gives same errors.

The problem is if I run it through php I get errors like this:

Cannot unlink: Read-only file system

But, if i run it from command line, it works:

su http -s /bin/bash -c "cd / && sudo tar -xf {$backupPath . $dirName} --recursive-unlink --unlink-first"

Running this both on full archlinux system and archlinux docker container gives me same results. I would appreciate any kind of help.


Solution

  • So issue was with systemd unit for php-fpm 7.4, where ProtectSystem was set to true, after commenting it out, everything worked as expected.

    sed -i 's:ProtectSystem=full:#ProtectSystem=full:' /usr/lib/systemd/system/php-fpm7.service