I need to collect files into an archive in such a way that after unpacking this archive, the packed files and directories have the same owners and the same permissions as before archiving. No matter what to do, the main thing is to collect in the archive, then the owners and permissions remain the same. Maybe some shell script that will collect permissions into a file, archive along with the permissions file, and a second script, which will then unpack and set up the new permissions and owners taken from the file.

Reported as a duplicate by aleksandr barakin linux 6 Jun '17 at 7:16 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

    1 answer 1

    collect in the archive, then the owners and permissions remain the same

    for example, tar , which is very often used for archiving , does just that - it preserves both the ownership of files and the permission bits set (executability / write / read for the owner / group / others).

    demonstration:

    $ touch user.file # создадим файл $ chmod u=rwx,go= user.file # запретим доступ к файлу всем, кроме владельца # а владельцу установим все три бита: rwx $ sudo touch root.file # создадим файл от имени root-а # он будет его владельцем $ tar -czf a.tgz *.file # запакуем эти файлы в архив и сожмём его 

    check that in the archive:

     $ tar -tvf a.tgz -rw-r--r-- root/root 0 2017-06-05 23:22 root.file -rwx------ user/user 0 2017-06-05 23:21 user.file 

    as we see, everything was correctly archived - both the belonging and the access bits.


    but

    if you unpack this archive on behalf of an ordinary user, then:

     $ mkdir dir $ tar -xf a.tgz -C dir $ ls -l dir -rw-r--r-- 1 user user 0 Jun 5 23:22 root.file -rwx------ 1 user user 0 Jun 5 23:21 user.file 

    then, as we see, the bits were restored, but the ownership of the files was not: both belong to the user (who was unarchiving).

    so that file ownership is restored

    You must unpack the archive as root user :

     $ sudo tar -xf a.tgz -C dir $ ls -l dir -rw-r--r-- 1 root root 0 Jun 5 23:22 root.file -rwx------ 1 user user 0 Jun 5 23:21 user.file 

    now everything is “in place”: both ownership and access rights.

    • If tar is compiled without ACL support, then metadata may be lost on the target system. - 0andriy