Linux Mastery

The Human Knowledge Project


Chapter 12 — Compression, Archives & Backups


Why This Chapter Matters

Linux systems create and store enormous amounts of information.

Documents...

Photographs...

Source code...

Databases...

System logs...

Configuration files...

Over time, this data consumes storage space and becomes increasingly valuable.

Linux provides powerful tools to compress files, package entire directory trees into archives, synchronize data between systems, and create reliable backups.

Understanding these tools is essential for protecting information and recovering from hardware failures, accidental deletion, and other unexpected events.


Learning Objectives

Upon completing this chapter, you will be able to:


Introduction

Imagine preparing to move an entire library.

Before transporting thousands of books, you would probably place them into boxes.

Those boxes might then be packed efficiently into a moving truck.

Linux performs similar operations with digital information.

Sometimes we gather many files into a single archive.

Sometimes we compress those files to reduce their size.

Sometimes we make backup copies to protect them.

Although these ideas are closely related, each serves a different purpose.

Understanding the differences is one of the foundations of good system administration.


1. Archives, Compression, and Backups

Although people often use these terms interchangeably, they describe different operations.

| Operation | Purpose |

|-----------|---------|

| Archive | Combine many files into one container |

| Compression | Reduce file size |

| Backup | Create recoverable copies of important data |

Some Linux tools perform only one of these tasks.

Others combine several operations into a single command.

THKI Memory Aid


Archive
        ↓
Packing Boxes
Compression
        ↓
Removing Empty Space
Backup
        ↓
Making Copies
Snapshot
        ↓
Time Machine

Keep these ideas separate.

Understanding the difference makes every backup tool easier to learn.

THKI Insight

An archive combines files.

Compression reduces file size.

They often work together, but they solve different problems.


2. zip — Create ZIP Archives

The zip command creates compressed ZIP archives.

Example:


zip backup.zip notes.txt

This creates:


backup.zip

containing:


notes.txt

Add Multiple Files


zip archive.zip file1.txt file2.txt file3.txt

Compress an Entire Directory

Use the recursive option:


zip -r project.zip project/

This includes:

ZIP archives are supported by:

making them an excellent choice for sharing files between different operating systems.


3. unzip — Extract ZIP Archives

To extract a ZIP archive:


unzip backup.zip

The contents are extracted into the current directory.

Extract into Another Directory


unzip backup.zip -d restored/

View Archive Contents


unzip -l backup.zip

The option:


-l

means:


list

allowing you to examine the archive without extracting it.


4. tar — The Tape Archive Utility

The tar command is one of the most important archive tools in Linux.

Although originally developed for magnetic tape backups, it remains one of the standard tools for packaging files and directories.

Unlike ZIP, tar normally creates an archive without compressing it.

Create a TAR Archive


tar -cvf archive.tar myfolder/

Understanding the Options

| Option | Meaning |

|--------|---------|

| c | Create archive |

| v | Verbose output |

| f | Archive filename |

The resulting file contains:


archive.tar

which stores the complete directory structure.

Extract a TAR Archive


tar -xvf archive.tar

Important

A plain .tar archive is not compressed.

Compression is usually added afterward using tools such as gzip or xz.

THKI Insight

Think of tar as a shipping box.

It gathers everything together.

Compression simply squeezes the box to make it smaller.


5. gzip — Fast File Compression

The gzip command compresses files to reduce their size.

Example:


gzip bigfile.txt

The original file is replaced with:


bigfile.txt.gz

Decompress with gunzip

To restore the original file:


gunzip bigfile.txt.gz

The .gz file is removed and the original file is recreated.

Why Use gzip?

gzip is popular because it is:

It is especially useful for:


6. Combining tar and gzip

Although tar creates archives, it does not compress them.

Linux commonly combines tar with gzip.

Example:


tar -czvf backup.tar.gz myfolder/

Understanding the Options

| Option | Meaning |

|---------|---------|

| c | Create archive |

| z | Compress with gzip |

| v | Verbose output |

| f | Archive filename |

The resulting archive:


backup.tar.gz

contains the complete directory tree in compressed form.

Extract a .tar.gz Archive


tar -xzvf backup.tar.gz

This restores the archived files and directories.

THKI Insight

Many Linux software packages are distributed as .tar.gz archives because they combine efficient packaging with reliable compression.


7. xz — High Compression

The xz utility produces stronger compression than gzip.

Example:


xz bigfile.txt

Result:


bigfile.txt.xz

Decompress with unxz


unxz bigfile.txt.xz

Create a Compressed TAR Archive


tar -cJvf backup.tar.xz myfolder/

Notice the capital:


J

which tells tar to use xz compression.

Extract


tar -xJvf backup.tar.xz

Comparing Compression Methods

| Tool | Speed | Compression |

|------|-------|-------------|

| gzip | Faster | Moderate |

| xz | Slower | Higher |

Generally:


8. rsync — Synchronization and Backup

The rsync command is one of the most powerful backup utilities available on Linux.

Unlike ordinary copy commands, rsync transfers only the data that has changed.

Basic example:


rsync -av source/ backup/

Common Options

| Option | Meaning |

|---------|---------|

| a | Archive mode |

| v | Verbose output |

Archive mode preserves:

Suppose you have already backed up 10,000 files.

Tomorrow only three files change.

Rather than copying everything again, rsync copies only those three changed files.

This greatly reduces:


9. Remote Synchronization

rsync also works across networks.

Example:


rsync -av project/ user@server:/backup/project/

This synchronizes the local project with a remote server.

Remote synchronization is widely used for:


10. Safe Backup Practices

Before copying files, rsync can perform a simulation.

Example:


rsync -av --dry-run source/ backup/

No files are copied.

Instead, rsync reports what it would do.

Dry-run mode is one of the safest ways to verify a backup command before allowing it to modify files.

Deleting Removed Files

Example:


rsync -av --delete source/ backup/

Files removed from the source are also removed from the backup.

Use this option carefully.

THKI Insight

--dry-run is one of the safest options in Linux.

Experienced administrators often simulate an operation before performing it.

A few seconds of verification can prevent hours of recovery.


11. Why Backups Matter

Storage devices eventually fail.

Users accidentally delete files.

Software bugs corrupt data.

Power failures interrupt writes.

Malware and ransomware can destroy years of work.

Backups provide protection against these events.

Good backups should be:

A backup that cannot be restored is not a backup.


12. The 3-2-1 Rule

One of the most widely recommended backup strategies is called the 3-2-1 Rule.

It recommends keeping:

This approach protects against:


13. Snapshots

A snapshot records the state of a filesystem at a particular moment in time.

Unlike an ordinary backup, a snapshot often records only what has changed.

Snapshots allow rapid recovery after:

Many Linux systems support snapshot technologies such as:

Many desktop Linux distributions include:

Timeshift

Timeshift creates system snapshots that allow you to restore your operating system to an earlier state.

Snapshots are extremely useful.

However, they should not replace regular backups.

Snapshots and backups serve different purposes.


14. Real-World Backup Workflow

A Linux administrator performing a routine backup might follow a workflow such as this:

Step 1 — Create a Compressed Archive


tar -czvf backup.tar.gz Documents/

This creates a compressed archive containing the entire Documents directory.

Step 2 — Copy the Backup


rsync -av backup.tar.gz /mnt/backupdrive/

The archive is copied to another storage device while preserving important file attributes.

Step 3 — Verify the Backup

Before deleting the original data, verify that the backup can be read and restored successfully.

Remember:

A backup is not complete until it has been tested.


15. Backup Strategies

Different situations require different types of backups.

| Backup Type | Description |

|--------------|-------------|

| Full | Copies everything |

| Incremental | Copies only changes since the previous backup |

| Differential | Copies changes since the last full backup |

Each method involves trade-offs between:

Administrators often combine these strategies depending on the importance of the data.


16. Choosing Backup Locations

Backups should not all be stored on the same device.

Common backup destinations include:

Multiple storage locations provide protection against hardware failure, theft, fire, and natural disasters.

THKI Insight

If your only backup is stored on the same drive as the original data, you do not really have a backup—you have two copies with the same point of failure.


17. Safety Note

Compression, archiving, and backup tools are generally safe.

However, incorrect commands can overwrite important data.

Always verify:

Pay particular attention when using:


rsync --delete

A single incorrect destination can remove large amounts of data.

Whenever practical, perform a:


--dry-run

before running an important synchronization.


Chapter Summary

| Tool | Primary Purpose |

|------|-----------------|

| zip | Create ZIP archives |

| unzip | Extract ZIP archives |

| tar | Archive files and directories |

| gzip | Fast compression |

| xz | Higher compression ratio |

| rsync | Synchronize files and backups |

| Snapshots | Restore previous filesystem states |


Key Ideas

Linux provides specialized tools for:

Understanding:

provides the foundation for reliable backup and recovery strategies.

Perhaps the single most important lesson of this chapter is that backups are valuable only if they can be restored successfully.


Practice Exercises

  1. Create several practice files and place them in a directory.
  2. Create a ZIP archive containing those files.
  3. Extract the archive into another directory.
  4. Create a TAR archive of the same directory.
  5. Compress the archive using:
  6. 
    gzip
    
  7. Create a compressed archive using:
  8. 
    tar -czvf
    
  9. Create another archive using:
  10. 
    tar -cJvf
    

Compare the resulting file sizes.

  1. Synchronize two directories using:
  2. 
    rsync -av
    

Modify one file and repeat the synchronization.

Observe which files are copied.

  1. Perform a simulation using:
  2. 
    rsync --dry-run
    

Explain why this option is valuable.

  1. Research the snapshot technology available on your Linux distribution.

If available, examine:

  1. Explain the differences between:
  1. Describe a disaster-recovery plan that follows the 3-2-1 Rule.
  2. Create your own backup workflow using:

Explain what each command contributes to the overall process.


Looking Ahead

Protecting data is only one part of effective Linux administration.

In the next chapter, we will begin exploring shell scripting and automation, where Linux commands become reusable programs capable of performing complex tasks automatically.