tar and rsync: Archive and Preserve SELinux Contexts, Extended Attributes, And ACLs

by on November 9, 2012 · 0 comments· LAST UPDATED November 9, 2012

in , , ,

How do I use tar and rsync command that can preserve the ACLs, extended attributes and SELinux contexts under CentOS / RHEL / Fedora Linux server while making backups?

Tutorial details
DifficultyIntermediate (rss)
Root privilegesYes
RequirementsSELinux, tar, rsync,
RHEL/CentOS/Fedora
Estimated completion timeN/A

Both the GNU/tar and rsync command can archive the Extended Attributes such as the following for backup and other purpose:

=> Selinux context

=> Acls

=> xattrs

tar command syntax

The syntax is:

 
tar --selinux --acls --xattrs  -cvf file.tar /var/www
 

Where,

  1. --selinux - Save the SELinux context to the archive called file.tar.
  2. --acls - ASave the ACLs to the archive called file.tar.
  3. --xattrs - Save the user/root xattrs to the archive called file.tar Please that it archive all extended attributes, including SELinux and ACLs.
  4. -c - Create a new archive called file.tar.
  5. -v - Verbose output.
  6. -f file.tar - Archive file name.
  7. /var/www - Create archive called file.tar from directory /var/www

To extract archive, simply type:

 
tar -xvf file.tar
 

You can ignore the attributes using the following syntax for backward compatibility with older Linux based systems:

 
tar --no-acls --no-selinux --no-xattrs -xvf file.tar
 

Where,

  1. --no-acls - Don't extract the ACLs from the archive.
  2. --no-selinux - Don't extract the SELinux context from the archive.
  3. --no-xattrs - Don't extract the user/root xattrs from the archive

rsync command syntax

The syntax is:

 
rsync -av -A -X /source/path /dest/path
rsync -az -A -X /source/path user1@server2:/dest/path
 

Where,

  1. -A : Preserve ACL.
  2. -X : Preserve extended attributes/SELinux.
  3. -a : Archive mode.
  4. -z : Compress file data during the transfer.

To sync /var/www to node2, enter:

 
rsync -aAX /var/www root@node2:/var/www/
 

OR

 
rsync -e ssh -aAX /var/www root@node2:/var/www/
 

You may want to pass the following additional option to backup hard links (-H), see progress (-P), and verbose (-v) output:

 
rsync -e ssh -aAXHPv /var/www root@node2:/var/www/
 

How do I see the SELinux context?

Type the following command:
ls -lZ
ls -lZ /path/to/file

OR
ls -Z

How do I see the extended attributes of filesystem objects?

Use the getfattr command:

 
getfattr -d -m - -R /path/to/dir
getfattr -d -m - /path/to/file
getfattr -d -m security.selinux -R /var/www
 

To list file attributes on a Linux second extended file system (see chattr command for more information):

 
lsattr /path/to/file
 
TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 0 comments… add one now }

Leave a Comment

Tagged as: , , , , ,

Previous Faq:

Next Faq: