Creating Backups – Part II
Untar a tar file:
- To uncompress tar file (.tar)
-- # tar xvf filename.tar
- To uncompress a gzip tar file (.tgz or .tar.gz)
-- # tar xvzf filename.tar.gz
- To uncompress a bzip2 tar file (.tbz or .tar.bz2)
-- tar xvjf filename.tar.bz2
- Untar in a specified directory:
-- tar -xvf filename.tar -C /pathtodirectory
meaning of the letters when using untar:
- x = extract, this indicated an extraction
- v = verbose (optional) the files with relative locations will be displayed
- z = bzip2 - zipped
- f = from/to file
ref above:
More examples:
http
To restore a tar archive, or a single file from the archive (by default tar will restore the file in the current directory), use the "x" flag:
Ex:tar xvf /backup/etc.tar etc/resolv.conf
To restore /home from home.tar:tar xvf /tmp/home.tar
To extract files from files.tar in the /tmp directory:
cd /tmp ; tar xvf /tmp/files.tar
To create a tarball called /tmp/home.tar.gz of the /home directory and compress it with gzip:tar cvzf /tmp/home.tar.gz /home
To create a tarball called /tmp/home.tar.bz2 of the /home directory and compress it with bzip2:tar cvjf /tmp/home.tar.bz2 /home
EXAM TIP:Archiving and compression are tasks done together to produce smaller archive files
To list both compressed archives and check which of the two is smaller: ll /tmp/home.tar*
To create a tarball called /tmp/extattr.tar.bz2 of the files in /home directory and include their extended attributes as well as SELinux contents, and compress the archive with bzip2:
Tar cvj –selinux –xattrs –f /tmp/extattr.tar.bz2 /home
Add Files or Directories to tar Archive File:
- To add files or directories to existing tar archived file, use the "r" (append) option
-- example: To add xyz.txt and directory "var/logs" to existing name.tar archive file:
---> tar -rvfnameoftar.tar xyz.txt
---> tar -rvf nameoftar.tar /var/logs
--> Same thing applies to "tar.gz" and "tar.bz2" archive files
To Check the Size of the tar, tar.gz and tar.bz2 Archive File:
- tar -czf - nameoftar.tar | wc -c
- tar -czf - nameoftargz.tar.gz | wc -c
- tar -czf - nameoffile.tar.bz2 | wc -c
Using “star” archival tool
The star (standard tar) command is an enhanced version of tar.
It supports SELinux security contexts and extended file attributes.
Options for creating, listing, appending, updating and extracting tarballs are the same as the tar command’s
This utility is not installed by default
Example:
- To create a tarball /tmp/etc.tar containing the entire /etc directory with all extended file attributes and SELinux file contexts, run this command:
-- star cvf /tmp/etc.tar –xattr –H=exustar /etc
- To list and extract from above, run the following command:
-- star tvf /tmp/etc.tar
-- star xvf /tmp/etc.tar
Labs/Exercise:
-Backup Practice
-Archiving and Extracting with Tar