Data Loss Prevention   «Prev  Next»

Lesson 4The tar tape backup command
ObjectiveUse the tar command to back up and restore files and directories.

tar Tape Backup Command to restore Files and Directories

Question: How do I use the tar command to back up and restore files and directories in Red Hat Linux?
The tar command, which stands for "Tape Archive", is a widely used command in Unix-based operating systems like Red Hat Linux for creating, maintaining, modifying, and extracting files from an archive file known as a tarball. This document will demonstrate how to utilize the tar command to back up and restore files and directories.

BACKING UP FILES AND DIRECTORIES

The following is the general format for creating a backup with tar:
tar -cvf archive_name.tar directory_to_backup/

Let's break this command down:
  1. -c: Create a new archive.
  2. -v: Verbose mode, which will list the files being processed.
  3. -f: Use the following archive file.

Here's an example. If you wanted to back up the directory /home/user/Documents, you could use:
tar -cvf Documents_backup.tar /home/user/Documents/

This command would create a tarball named Documents_backup.tar from the directory /home/user/Documents.

COMPRESSING BACKUPS

To save space, you might want to compress your backup. You can do this with the gzip or bzip2 utilities along with tar.
Here's how:
  1. Using gzip:
    tar -cvzf archive_name.tar.gz directory_to_backup/
    

    -z: Compress the archive using gzip.
  2. Using bzip2:
    tar -cvjf archive_name.tar.bz2 directory_to_backup/
    

    -j: Compress the archive using bzip2.

RESTORING FILES AND DIRECTORIES

The general command for extracting a tarball is:
tar -xvf archive_name.tar

-x: Extract files from an archive.
So, if you wanted to restore the files from the Documents_backup.tar archive, you would use:
tar -xvf Documents_backup.tar

This command will extract all files to the current directory. If you want to extract to a specific directory, use the -C option:
tar -xvf Documents_backup.tar -C /path/to/specific/directory/

RESTORING COMPRESSED BACKUPS

If your backups are compressed, you can extract them with these commands:
  1. For gzip compressed tarball:
    tar -xvzf archive_name.tar.gz
    
  2. For bzip2 compressed tarball:
    tar -xvjf archive_name.tar.bz2
    
Please note, you should replace archive_name.tar.gz and archive_name.tar.bz2 with the actual names of your tarballs.
In conclusion, the tar command is a versatile tool for managing backups of files and directories on Red Hat Linux. Remember to replace the example file names and directories with the ones that correspond to your actual use case.
The tar command is used to create archives and extract files and directories from tar archives. You can use tar as an expedient and simple method for storing an entire file system. You can then use the tar file as a backup, or you can transfer and extract the file onto another machine. tar is very useful for generating daily backups of critical files (such as a current project) or for archiving data, such as mail for an entire organization.
tar "flattens" files and directories into sequential archives that can go to tape or disk. Linux supports standard commands for tape backup, including:
  1. c: Create an archive
  2. x: Extract files and file systems from a tar archive
  3. z: Use gzip compression
  4. f: Specify the file system on which to store the archive

The following series of images illustrate some examples.
1) Creates an archive using SCSI
Creates an archive using SCSI tape drive

2) Creates an archive using gzip compression
Creates an archive using gzip compression

3) Extracts from an archive
Extracts from an archive

4) Extracts from a compressed archive
Extracts from a compressed archive

5) Places tar archive onto a file system
Places a tar archive onto a file system

  1. Creates an archive using SCSI tape drive
  2. Creates an archive using gzip compression
  3. Extracts from an archive
  4. Extracts from a compressed archive
  5. Places a tar archive onto a file system

Linux tar Commands
Question: What command do you use to create a compressed tar archive named
/project.tar.gz that contains the files and subdirectories in the /project directory?

Answer:
tar czf /project.tar.gz /project
or
tar zcf /project.tar.gz /project

The next lesson examines the use of the dump command.
Pre-Installed Linux Laptop