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

Using the `tar` command in Red Hat Linux to back up and restore files and directories is straightforward. Here are the steps and examples for creating backups and restoring files:
  1. Backing Up Files and Directories

    The tar command creates a single archive file (commonly with .tar, .tar.gz, or .tar.bz2 extensions) from files and directories.

    Basic Backup Command

          tar -cvf backup.tar /path/to/directory
        
    • -c: Create a new archive.
    • -v: Verbose mode, shows progress.
    • -f: Specifies the name of the archive file.
    • /path/to/directory: The files or directories to back up.

    Compress the Backup

    You can compress the archive to save space:

    • Using gzip:
                tar -czvf backup.tar.gz /path/to/directory
              

      Use the -z option for gzip compression.

    • Using bzip2:
                tar -cjvf backup.tar.bz2 /path/to/directory
              

      Use the -j option for bzip2 compression.

    Example

    Backup /home/user/docs to a compressed file named docs_backup.tar.gz:

          tar -czvf docs_backup.tar.gz /home/user/docs
        
  2. Restoring Files and Directories

    To extract files from a tar archive:

    Extract from a Non-Compressed Archive

          tar -xvf backup.tar
        
    • -x: Extract files.
    • -v: Verbose mode.
    • -f: Specifies the archive file to extract.

    Extract from a Gzip-Compressed Archive

          tar -xzvf backup.tar.gz
        

    Use the -z option for gzip-compressed files.

    Extract from a Bzip2-Compressed Archive

          tar -xjvf backup.tar.bz2
        

    Use the -j option for bzip2-compressed files.

    Extract to a Specific Location

    You can specify a directory where the files should be restored:

          tar -xvf backup.tar -C /path/to/restore
        

    -C: Change to the specified directory before extracting.

    Example

    Restore the contents of docs_backup.tar.gz to /home/user/restored_docs:

          mkdir -p /home/user/restored_docs
          tar -xzvf docs_backup.tar.gz -C /home/user/restored_docs
        
  3. Listing Contents of an Archive

    To view the contents of an archive without extracting it:

          tar -tvf backup.tar
        

    -t: Lists files in the archive.

  4. Incremental Backup

    For large backups, you can use incremental backup options by combining tar with the --listed-incremental option:

          tar --listed-incremental=snapshot.file -cvf incremental_backup.tar /path/to/directory
        
  5. Verifying the Archive

    To verify the integrity of an archive:

          tar -tvf backup.tar
        

Common Tips:
  • Always verify backup archives to ensure their integrity.
  • Use absolute paths cautiously to avoid accidentally overwriting files when restoring.
  • Regularly automate backups using cron jobs. For example:

crontab -e

Add a line like this for daily backups:
0 2 * * * tar -czvf /backup/backup_$(date +\%F).tar.gz /path/to/directory

Purpose of tar command

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. These include
  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


Most important verbose options for creating archives in Red Hat Linux 9

In Red Hat Enterprise Linux (RHEL) 9, creating archives typically involves using tools like `tar`, `gzip`, `bzip2`, or `zip`. These tools support various "verbose options" that allow administrators to view detailed output during the creation of archives. Below are the most important verbose options and their practical usage:
Key Verbose Options for Common Tools
  1. tar (Tape Archive)

    The `tar` command is widely used for creating, extracting, and managing archive files.

    • -v (Verbose): Provides detailed output of the files being archived or extracted. This is the most commonly used verbose option.
                tar -cvf archive.tar /path/to/files
              

      Output: Displays the list of files being archived.

    • --verbose: Equivalent to `-v`, but uses the long-form option syntax.
                tar --create --verbose --file=archive.tar /path/to/files
              
    • Commonly Combined Options:
      • - `-cvf`: Create (`c`) an archive, verbose (`v`), and specify the file name (`f`).
      • - `-tvf`: List (`t`) the contents of an archive with verbose output.
  2. gzip and bzip2

    These are compression utilities commonly used alongside `tar`.

    • -v (Verbose): Shows the compression process and details about the file being compressed or decompressed.
      • gzip:
                      gzip -v file.txt
                    

        Output: Displays the compression ratio and the resulting file name.

      • bzip2:
                      bzip2 -v file.txt
                    
  3. zip

    The `zip` command creates compressed ZIP archives.

    • -v (Verbose): Lists detailed information about the files being added to the archive.
                zip -v archive.zip /path/to/files
              
    • --verbose: Provides detailed output in a long-form syntax.
                zip --verbose archive.zip /path/to/files
              
  4. cpio

    The `cpio` command is another archive tool often used with verbose options.

    • -v (Verbose): Lists the files being archived.
                find . | cpio -ov > archive.cpio
              

Practical Scenarios
  • Backup Operations: Use verbose options to confirm which files are included in an archive during backups.
  • Error Detection: Verbose options make it easier to identify errors or skipped files during the archiving process.
  • Archive Inspection: Use -tvf or similar options to inspect the contents of an archive without extracting it.

Examples
  1. Create a tar.gz Archive with Verbose Output:
          tar -czvf archive.tar.gz /path/to/files
        
  2. Extract a tar.bz2 Archive Verbosely:
          tar -xjvf archive.tar.bz2
        
  3. Inspect Contents of a tar File:
          tar -tvf archive.tar
        

Verbose options are critical for ensuring the success of archive operations and verifying their accuracy.

Linux verbose option

The v option for verbose output is not necessary to create and extract tar archives. However, it is useful as a check to view the set of files acted upon as the archive is generated or extracted. The syntax for the verbose option is the same as that for other tar commands. The default tape drive under Linux is /dev/st0 (first SCSI tape device name). Following paragraph summaries command you need to use control tape drive for backup/restore purpose.
  • Linux Tape Backup Example To backup to multiple tape use the following command (backup /home file system):
    # tar -clpMzvf /dev/st0 /home
    

    To compare tape backup, enter:
    # tar -dlpMzvf /dev/st0 /home
    

    To restore tape in case of data loss or hard disk failure:
    # tar -xlpMzvf /dev/st0 /home 
    

Pre-Installed Linux Laptop
The following series of images illustrate some examples.
1) Creates an archive using SCSI
tar cf /dev/st0 file_or_directory

Creates an archive using SCSI tape drive

2) Creates an archive using gzip compression
tar zcf /dev/st0 file_or_directory

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.

SEMrush Software 4 SEMrush Banner 4