tar does not retain extended attributes by default. Since SELinux contexts are stored in extended attributes, contexts can be lost when archiving files. Use tar --selinux to create archives that retain contexts. If a Tar archive contains files without extended attributes, or if you want the extended attributes to match the system defaults, run the archive through /sbin/restorecon:
$ tar -xvf archive.tar | /sbin/restorecon -f -
Note: depending on the directory, you may need to be the Linux root user to run the /sbin/restorecon command.
The following example demonstrates creating a Tar archive that retains SELinux contexts:
As the Linux root user, run the touch /var/www/html/file{1,2,3} command to create three files (file1, file2, and file3). These files inherit the httpd_sys_content_t type from the /var/www/html/ directory:
Run the cd /var/www/html/ command to change into the /var/www/html/ directory. Once in this directory, as the Linux root user, run the tar --selinux -cf test.tar file{1,2,3} command to create a Tar archive named test.tar.
As the Linux root user, run the mkdir /test command to create a new directory, and then, run the chmod 777 /test/ command to allow all users full-access to the /test/ directory.
Run the cp /var/www/html/test.tar /test/ command to copy the test.tar file in to the /test/ directory.
Run the cd /test/ command to change into the /test/ directory. Once in this directory, run the tar -xvf test.tar command to extract the Tar archive.
Run the ls -lZ /test/ command to view the SELinux contexts. The httpd_sys_content_t type has been retained, rather than being changed to default_t, which would have happened had the --selinux not been used: