There is an .sh file in which files are unpacked from an archive uploaded via FTP to the server - all this is part of the backup and restore mechanism.

So, in the .sh file there is a command:

tar -zxvf home.tar.gz -C $HOME 

How to make so that after extracting from the archive:

  • Do folders and files have a specific owner-user?
  • folders and files would save chmod settings?
  • one
    man tar did not try? there are many interesting keys. In general, if you deploy tar as root, by default it uses the owners and rights from the archive - Mike
  • one
    And if the owner should not be the one in the archive, but specifically specified, then man tar says that the key is --owner=NAME - Mike
  • Yes, I initially launch the .sh file without any permissions from under root, but folders and files after unpacking will have a different user and group. - Enshtein
  • Added a key: tar -zxvpf home.tar.gz --owner = www-root -C $ HOME # as a result, the files and folders did not receive the owner: www-root unfortunately. - Enshtein
  • one
    I also did not carefully read man ... --owner adds files with a specific user to the archive. And it extracts either with the same one in the archive (--same-owner or when executed as root), or with the current user ( --no-same-owner ). So, it is worthwhile to perform unarchiving not under the root, but under the user www-data. Those. if the script runs under root then su www-data -c 'tar -xzvf file' - Mike

1 answer 1

folders and files would save chmod settings?

permissions for files / directories are stored by the tar program inside the archive. accordingly, when unpacking the archive, the tar program assigns to each created file / directory exactly those rights that were recorded inside the archive.

Do folders and files have a specific owner-user?

The tar program (at least the implementation from the gnu project) when creating an archive saves information about the owner / group of files / directories not only in numerical form ( uid / gid ), but also in the form of strings. so that the lines are not saved, you can use the option --numeric-owner .

when unpacking, running on behalf of an ordinary user, this information does not have a (significant) effect, because an ordinary user can create files / directories belonging only to him.

when unpacking, running on behalf of the superuser (whose uid is zero; usually the root is assigned to the username), there is no such restriction: the process can create files / directories belonging to the user / group with any uid / gid .

accordingly, if the archive contains lines with the names of the owners / groups, then if there is a user / group with the same name in the system where unpacking is performed, these names will be used.

if there is no corresponding user / group name in this system (or the names were not saved at all due to the aforementioned option --numeric-owner ), then the numerical values ​​stored in the archive will be used. completely different strings with the names of users / groups can be assigned to these numerical values ​​of uid / gid . you, apparently, and faced exactly such a situation.


There is only one way out: before unpacking, you need to create in this system users / groups with the names that are stored inside the archive.

You can see these names by adding the -v option when viewing the contents of the archive (and the view itself is the -t option):

 $ tar -t -v -f архив 

example output:

 -rw-r--r-- user/group 0 2016-11-11 12:41 file 

which shows that the file file belongs to the user user group .