LPIC-1: Archiving and compression

Discover in Part 6 of the LPIC-1 series how to archive and compress efficiently using tar, gzip and others. Practical backups, pipe integration, and strategies for admins.

Reading time: 25 min

In the first five parts of our LPIC-1 series, we covered the fundamentals of the Linux command line, filesystem navigation, editing file contents, text processing with shell commands, and finally streams, pipes, and redirections.

Now we turn to an essential aspect of data management: archiving and compression. These techniques build seamlessly on the concepts of streams and pipes covered in the last article and enable you to efficiently bundle, compress, and transport data – whether for backups, data transfers, or storage optimization.

Important note: As emphasized in previous articles, this series does not replace an official exam preparation course for the LPIC-1 certification (LPI 101-500). It serves as a practice-oriented, didactically prepared supplement for your self-study and is designed to help you better understand and apply the sometimes complex content.

Why archiving and compression are so central

In Linux administration, archiving and compression are indispensable tools for efficient LPIC-1 data handling. Archiving allows you to bundle multiple files and directories into a single file, while compression reduces storage space and minimizes transfer times. Particularly in an era of large data volumes – from log files to configurations to complete system backups – these methods help conserve resources and ensure system stability. Through integration with streams and pipes covered in the last part, you can seamlessly integrate these processes into automated workflows, such as archiving command output directly or forwarding compressed data through pipes.

Placement within the LPIC-1 certification

Understanding archiving and compression is centrally important for the LPIC-1 certification and appears in multiple examination objectives:

  • 103.5: Use archives and compression
  • 103.4: Streams, pipes, and redirections (integration with archiving)
  • 104.5: Basic backup strategies (practical application)

These topics account for approximately 10–15% of the points in the LPIC-1 Exam 101 and simultaneously form the foundation for many other examination tasks, such as data management in real-world scenarios.

What to expect in this article

In the upcoming sections, we will cover the following core topics:

  • Fundamentals of archiving and compression
  • tar as the standard archiving tool with detailed options and examples
  • Compression tools such as gzip, bzip2, and xz compared, with practical applications
  • Combined archiving and compression (e.g., tar.gz)
  • Practical backup strategies, including automation and troubleshooting
  • Consolidating exercises for reinforcement

Each section will not only explain the technical aspects but also highlight practical application scenarios from real-world system administration.

Practical relevance for everyday administration

The concepts covered in this article are part of every Linux administrator's daily toolkit. Typical application scenarios include:

  • Regular backups of configuration files and logs to prevent data loss
  • Compressing data for efficient transfer via email or network
  • Automated archiving in scripts, combined with pipes for real-time processing
  • Storage optimization on servers with limited resources
  • Error diagnosis through secure archiving of system states before changes

As usual, you'll find special markers throughout the article:

  • 💡 Tips and hints for more efficient workflows
  • ⚠️ Warnings and pitfalls to save you trouble
  • Practical examples to follow along directly
  • Common sources of error and their solutions

How to get the most out of this article

For optimal learning, I strongly recommend trying out the concepts and techniques in your own Linux environment. Create test files and directories, experiment with the presented commands, and observe the results. Particularly when combining with pipes and streams, hands-on practice is essential – the understanding of data flow develops best through your own experiments. The more you work with these tools, the more intuitive their application becomes.

Let's now dive into the fascinating world of archiving and compression – a field that will revolutionize your efficiency as a Linux administrator and at the same time secure you important points in the LPIC-1 certification (LPI 101-500).

Fundamentals of archiving

Before we dive into specific tools like tar or gzip, let's clarify the fundamental concepts. Archiving and compression are two related but distinct techniques that help you as a Linux administrator manage data efficiently. Especially in combination with the streams and pipes from the last article, they become powerful tools for automated processes – just think about how you can pipe command output directly into a compressed archive.

What are archiving and compression?

Archiving means bundling multiple files and directories into a single file without altering the content. Imagine packing your suitcase for a trip: You put in clothing, books, and utensils, but nothing gets smaller or changes – it's just organized. In Linux, archiving primarily serves the structuring and transport of data, such as for backups or transferring entire directory trees.

Compression, on the other hand, reduces file size by removing redundant information or encoding it more efficiently. The content is mathematically "packed," similar to vacuum-sealing clothing to save space. The result is a smaller file that you must later decompress to recover the original content.

Compression is particularly useful for large log files or data transfers over networks with limited bandwidth.

💡 Tip: Archiving alone does not save storage space, and compression alone does not bundle files – combining both is the key to efficient data management. Why does this matter? In practice, you save time and resources, for example when backing up /etc/ configurations that you need to restore quickly later.

Differences between archiving and compression

The differences are crucial for choosing the right tools:

  • Purpose: Archiving organizes data (e.g., preserving directory structures), compression minimizes size (e.g., removing redundant bits). In the LPIC-1 exam, you'll often be asked when to use tar for archiving and gzip for compression – and why you combine them.
  • Impact on data: During archiving, files remain unchanged; during compression, they are re-encoded (losslessly or lossily, depending on the tool). Note: lossy compression (e.g., for images) is rare in system administration, where you typically need lossless methods to keep data intact.
  • Speed and efficiency: Archiving is fast because it only bundles files; compression requires computation time depending on the algorithm (gzip is fast, xz more efficient but slower). In practice, you choose based on your scenario: for quick backups use gzip, for long-term storage use xz.
  • Integration with streams/pipes: As learned in the last article, you can seamlessly integrate archiving and compression into pipes. Example: pipe the output of find to tar to dynamically archive and directly compress files – this saves temporary files and automates workflows.

🔧 Practical example:

Imagine you want to archive and back up /var/log/. With archiving alone (tar), you get an uncompressed file that preserves the structure. Adding compression (tar.gz) reduces the size by up to 70%, which is crucial when transferring to an offsite backup:


# 1. Archiving only (without compression):
tar -cvf var_log_backup.tar /var/log/

# 2. Archiving + compression (with gzip):
tar -czvf var_log_backup.tar.gz /var/log/

# Show size comparison:
du -sh var_log_backup.tar var_log_backup.tar.gz

Important note: Compression is not always reversible – always choose lossless formats for sensitive data like configurations. And: never compress already compressed files (e.g., JPEGs), as this can actually increase the size!

Relevance for data management and backup strategies

In system administration, these techniques form the core of every backup strategy. You save storage space on servers, reduce transfer times in the cloud, and ensure data integrity. Particularly relevant: in LPIC-1 scenarios, you often need to create backups combined with pipes, such as piping running processes (like mysqldump) directly into a compressed archive. This minimizes downtime and optimizes resources.

Why this matters in practice: As an administrator, you deal with growing data volumes daily – logs grow exponentially, configurations need versioning. With archiving and compression, you build scalable systems: a tar archive with gzip compression can shrink a 10 GB directory to 2 GB, saving gigabytes during monthly backups.

Important note: Many beginners compress without checking whether the data is compressible (e.g., encrypted files). Solution: test with small samples and compare sizes before/after – use du -sh to measure the effect.

Integration with streams and pipes from the previous article

The strength of these tools unfolds through integration with streams and pipes. Remember stdin/stdout? You can feed tar directly with pipes:


find /etc -name "*.conf" | tar -cvf config.tar -T -


mysqldump database | gzip > backup.sql.gz

💡 Tip: Use this for automated scripts. Why? It avoids temporary files, reduces I/O, and integrates seamlessly into cron jobs. Note: pipes forward stdout, so handle stderr separately to save errors to logs.

These fundamentals prepare you for the tools – let's now move on to tar, the workhorse of archiving.

tar – The standard archiving tool

Next, let's dive into tar, the absolute workhorse of archiving under Linux. If you still have the streams and pipes from the last article in mind, you'll quickly notice how perfectly tar combines with them – imagine piping the output of a find command directly into an archive without creating temporary files. This makes tar not just a tool for backups but a real enabler for automated workflows. In this section, I'll walk you through step by step what tar can do, why you need it, and what to watch out for so you can use it in your daily administration.

What is tar?

tar stands for Tape ARchive and was originally developed to save files to magnetic tapes – hence the name. Today, it is the standard tool under Linux for bundling files and directories into a single archive file without compressing them (that comes later).

💡 What exactly happens here? tar takes your files, directories, and their metadata (such as permissions, owners, and timestamps) and packs everything into a sequential file. Why do we do this? Because it preserves the directory structure – unlike a simple copy, tar lets you transport or back up entire folder hierarchies without losing anything.

As an administrator, tar is indispensable because it forms the basis for almost every backup strategy. Imagine you need to back up /etc/: without tar, you'd manually copy dozens of files; with tar, you do it in a single command. And in the LPIC-1 exam? There's no avoiding it – tar is part of examination objective 103.5 and is often tested in combination with pipes.

What should you watch out for? tar does not compress automatically, so always combine it with tools like gzip when space matters. What will you need this for later in practice? For daily backups, system migrations, or sending configuration sets – it saves time and reduces sources of error.

💡 Tip: tar is portable and runs on almost every Unix-like system. When transferring files between servers, it's your go-to tool because it respects the POSIX structure and uses no proprietary formats.

⚠️ Warning: tar overwrites files without asking if you're not careful when extracting. Always check with -t what's in the archive before extracting!

Typical source of error: Many forget that tar uses relative paths. Solution: always specify the base directory with -C to avoid absolute paths and ensure portability.


┌─────────────────────────────────────────────────────────────┐
│              HOW TAR WORKS: ARCHIVING                       │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│   Input files         tar process        Output archive     │
│   ┌─────────────┐     ┌─────────────┐     ┌─────────────┐   │
│   │ /project/   │ ──► │ - Bundles   │ ──► │ project.tar │   │
│   │   file1.txt │     │ - Structure │     │  (contains  │   │
│   │   file2.log │     │ - Metadata  │     │   metadata) │   │
│   └─────────────┘     └─────────────┘     └─────────────┘   │
│                                                             │
└─────────────────────────────────────────────────────────────┘

This shows how tar organizes your data – simple, but powerful.

Basic syntax and usage

The syntax of tar is straightforward but flexible:


tar [options] [archive_name] [files/directories]

What happens here? tar reads the specified files and creates or modifies the archive. Why this structure? It allows clear separation of operations like creating -c, listing -t, or extracting -x. Note: Without options, tar does nothing – you always need at least one function option like -c or -x.

Here's the basic usage:

What does the command do? -c creates a new archive, -v shows progress (verbose), -f specifies the filename. Why -v? It helps with troubleshooting since you can see which files are being added.


# Create archive:
tar -cvf archive.tar /path/to/directory

What do you need this for? For quick backups, e.g.:


tar -cvf etc_backup.tar /etc/.

Here you check without extracting – great for validating that everything is included.


tar -tvf archive.tar

Extracts everything to the current directory.


tar -xvf archive.tar

What to watch out for? Use -C /target/directory to control the extraction path and avoid overwrites.


tar -xvf archive.tar -C /target/directory

💡 Tip: Combine tar with pipes for dynamic inputs, e.g., find /var/log -type f | tar -cvf logs.tar -T - – this archives only specific files based on find's stdout.

⚠️ Warning: Without -f, tar outputs to stdout, which can lead to unexpected streams. Always use -f for files!

Typical source of error: Wrong option order – tar expects them without hyphorts for short forms (e.g., cvf, not -c -v -f). Solution:** Memorize common combinations like cvf (create verbose file).

Important options and their practical application

tar has dozens of options, but let's focus on the essentials – the ones you need for the LPIC-1 exam and daily work.

Option Description Practical Example
-c Creates a new archive tar -c /etc/ > etc.tar (via stdout)
-x Extracts an archive tar -x < backup.tar (via stdin)
-t Lists contents tar -t < archive.tar
-v Verbose output tar -cvf backup.tar /var/log (shows files)
-f Specify filename tar -cf archive.tar files (default)
-z Compress with gzip tar -czf archive.tar.gz /dir
-j Compress with bzip2 tar -cjf archive.tar.bz2 /dir
-J Compress with xz tar -cJf archive.tar.xz /dir
-C Change directory tar -xvf backup.tar -C /tmp/
-p Preserve permissions tar -xpf archive.tar (important for system files)
--exclude Exclude files tar -cf backup.tar /home --exclude=*.tmp

💡 What do these options do? They extend tar's functionality – e.g., -z integrates compression directly, saving time. Why combine? Because tar alone has no compression, but with -z/-j/-J it seamlessly invokes gzip, etc. Note: Order matters, -f usually comes last.

⚠️ Warning: -p is crucial for system backups as it preserves owner and permissions. Without it, files could become unusable!

🔧 Practical example:


tar -czf logs.tar.gz --exclude=*.old /var/log

# archives and compresses logs, excludes old files. Why? This reduces archive size and keeps it clean.


┌─────────────────────────────────────────────────────────────┐
│               TAR OPTIONS IN DETAIL (-czvf)                 │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│   tar -c     -z     -v     -f   backup.tar.gz  /etc/        │
│       │      │      │      │          │         │           │
│       │      │      │      │          │         └──source   │
│       │      │      │      │          └─ target file        │
│       │      │      │      └─ filename option               │
│       │      │      └─ verbose (progress)                   │
│       │      └─ gzip compression (.gz)                      │
│       └─ create (create archive)                            │
│                                                             │
└─────────────────────────────────────────────────────────────┘

Creating archives

Creating is tar's core function. Syntax: tar -c [options] -f archive.tar files. What happens? tar traverses the files recursively, stores metadata, and writes to the archive. Why is this useful? It preserves the directory structure, including symlinks and permissions – ideal for backups.

Step by step:

a) Select files:


tar -cf archive.tar file1 file2 /dir/

# What to watch for? Use relative paths to ensure portability.


tar -czf archive.tar.gz /dir/

# This invokes gzip internally – the result is smaller.


find /etc -name "*.conf" | tar -cf config.tar -T -

# Here you use stdin as file list – perfectly from the last article.

💡 Tip: For large files, use -M for multi-volume archives, e.g., tar -cMf backup.tar /bigdir – this splits across multiple files.

⚠️ Warning: tar doesn't stop on errors – check the exit code with $? after the command.

🔧 Practical example: tar -czpf full_backup.tar.gz / --exclude=/proc --exclude=/sys – full system backup, excluding virtual filesystems. What for? For disaster recovery.

Typical source of error: Including /proc causes infinite loops. Solution:** Always use --exclude for virtual directories.

Extracting archives

Extracting is the reverse process: tar -x [options] -f archive.tar. What happens? tar reads the archive and restores the structure. Why important? You need it for restore operations, e.g., after a crash.

Steps:

a) Check contents:


tar -tf archive.tar

# This lists without extracting – safe start.


tar -xzf archive.tar.gz -C /target/

# -z decompresses, -C changes directory.


tar -xzf archive.tar.gz file1 subdir/file2

# Extracts only specific files.

💡 Tip: With --strip-components=1 you remove leading directories, e.g., with tar -xzf package.tar.gz --strip=1.


┌─────────────────────────────────────────────────────────────┐
│           TAR EXTRACTION TO TARGET DIRECTORY (-C)            │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│   [ archive.tar.gz ] ──► [ tar -xzf ] ──► [ /target/ ]      │
│                                              │              │
│                                              ├── file1      │
│                                              ├── subdir/    │
│                                              └── file2      │
│                                                             │
└─────────────────────────────────────────────────────────────┘

Practical examples for system administration

In practice, you use tar for real-world scenarios.

Here are some:

a) Daily log backup:


tar -czf /backups/logs_$(date +%Y%m%d).tar.gz /var/log/

# Why? Automated with cron, saves space.


tar -cpzf config.tar.gz /etc/ --exclude=/etc/ssl/private

# -p preserves permissions, exclude protects sensitive keys.


df -h | tar -czf disk_usage.tar.gz -T /dev/stdin

# This archives the output directly – connects with streams.


tar -czf home.tar.gz /home/ --exclude=*.cache – user home

# Back up, skip cache. What for? For migrations.


ls /etc/ | tar -cf etc.tar -T -                                       # -T reads file list from stdin.
tar -czf - /etc/ | ssh user@remote 'cat > backup.tar.gz'    # Compresses and sends via SSH.
find /var/log -mtime -7 | tar -czf recent_logs.tar.gz -T - # Only files from the last week with find.

💡 Tip: Combine with gpg for encryption: tar -czf - /dir/ | gpg -e > encrypted.tar.gz.gpg

⚠️ Warning: Pipes can break with large data – use pv for progress.

Compression tools compared

gzip, bzip2, and xz

Now that we've covered tar as the central tool for archiving, let's turn to compression tools. These tools are essential for reducing the size of your archives – just think about how you combine tar with gzip to create tar.gz files. What happens here? Compression takes your data and encodes it more efficiently to save storage space without losing information (lossless). Why do we do this? In system administration, it helps transfer backups faster, optimize storage, and conserve network resources.

What should you watch out for? Choose the tool based on speed vs. compression ratio – and integrate it seamlessly with pipes as we learned in the streams section. What will you need this for later in practice? For daily log rotations, cloud uploads, or efficient data migrations where every megabyte counts.

In this section, I'll give you an overview of the common tools, explain each in detail, and wrap up with a comparison. We'll focus on gzip, bzip2, and xz, as these are the most relevant for LPIC-1 and fit perfectly with tar. Let's start – and remember: test the examples in your environment to feel the effect.

Overview of common compression tools

Under Linux, you have a variety of compression tools that differ in algorithm, speed, and efficiency. What are the core differences? Some prioritize speed (like gzip), others the compression ratio (like xz). Why does this matter? In practice, you choose based on your scenario: for quick backups use gzip, for long-term storage use xz. What to watch for? All are lossless, so they work for text, logs, or binary files – but not for already compressed formats like MP3.

Here's a brief overview table of the tools we'll cover:

Tool Algorithm Strengths Weaknesses Typical Use
gzip DEFLATE Fast, widely available Medium compression ratio Everyday backups, web compression
bzip2 Burrows-Wheeler Better ratio than gzip Slower Large text files, logs
xz LZMA2 Highest compression ratio Slowest Long-term archives, distributions

What happens during compression? The algorithm scans for redundant patterns and replaces them with shorter codes – e.g., repeated strings are referenced. Why combine with tar? Because tar bundles and these tools compress, creating an efficient tar.gz, etc. What do you need this for? In scripts where you use pipes:


tar -cf - /dir/ | gzip > archive.tar.gz – # this compresses on-the-fly.

💡 Tip: For modern alternatives like zstd (fast and efficient), check advanced guides, but for LPIC-1 these three are sufficient.

⚠️ Warning: Compression consumes CPU – with large files on weak servers, this can cause load spikes. Schedule jobs for off-peak times!

🔧 Practical example: Compare the tools on a log file: create a 100MB test file with


dd if=/dev/urandom of=test.log bs=1M count=100

❗ **Typical source of error: Wrong extension (e.g., .gz for bzip2) causes extraction errors. Solution: use file to check the type.


┌─────────────────────────────────────────────────────────────┐
│          COMPRESSION & DECOMPRESSION CYCLE                  │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│   ┌──────────────┐     ┌──────────────┐     ┌───────────┐   │
│   │ Input file   │ ──► │ Compressor   │ ──► │ .gz/.xz   │   │
│   │  (log.txt)   │     │ (gzip/xz/bz) │     │   file    │   │
│   └──────────────┘     └──────────────┘     └───────────┘   │
│           ▲                                       │         │
│           └────────── Decompression ─────────────┘         │
│                                                             │
└─────────────────────────────────────────────────────────────┘

gzip – Fast compression for everyday use

gzip is the go-to tool for fast, everyday compression. Based on the DEFLATE algorithm (from the 90s), it's integrated into virtually every Linux system. What happens here? gzip scans your file for repeated sequences and replaces them with Huffman codes and LZ77 references – the result is a .gz file that can be up to 70% smaller. Why do we do this? Because gzip is lightning fast: it compresses in seconds what xz takes minutes for, ideal for pipes and streams.

What to watch for? gzip isn't the most efficient, but the most balanced – perfect for logs or configs. What will you need this for later? For web servers (nginx compresses responses with gzip) or daily backups where speed counts.

💡 Tip: gzip is thread-safe but single-threaded – for multi-core, use pigz (parallel gzip) as a drop-in replacement.

⚠️ Warning: gzip doesn't handle directories – always combine with tar for folder structures, or you'll lose the hierarchy.

Basic operations

The syntax is simple: gzip [options] file. What happens? gzip compresses the file and replaces it with .gz (original is deleted). Why? It's optimized for single files, but flexible with pipes.

Compressing:


gzip file.txt

# Creates file.txt.gz, deletes original. Why delete? To save space – use -k to keep.


gunzip file.txt.gz

# Or

gzip -d # restores original.


cat file.txt | gzip > file.gz

# Compresses stdin to stdout – great for pipes from the last article.


tar -cf - /etc/ | gzip > etc.tar.gz

# combines archiving and compression in a stream.

Typical source of error: Compressing without backup – gzip deletes the original. Solution: always use -k or cp first.


┌─────────────────────────────────────────────────────────────┐
│             GZIP COMPRESSION: DEFLATE METHOD                │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│   file.txt (10 MB) ──► [ gzip ] ──► file.txt.gz (3 MB)      │
│                            │                                │
│                            ▼                                │
│                    DEFLATE algorithm:                       │
│                    ├─ LZ77 (reference dictionary)           │
│                    └─ Huffman encoding (bit optimization)   │
│                                                             │
└─────────────────────────────────────────────────────────────┘

Important options

gzip has some useful options:

Option Description Example
-1 to -9 Compression level (1=fast, 9=best) gzip -9 log.txt (maximum ratio)
-d Decompress gzip -d archive.gz
-k Keep original gzip -k file.txt (don't delete)
-c Output to stdout gzip -c file.txt > archive.gz
-v Verbose (shows ratio) gzip -v bigfile.txt (shows compression percentage)
-t Test (check integrity) gzip -t archive.gz

What happens with levels? -1 prioritizes speed (less CPU), -9 ratio (more CPU). Default is -6 – balanced. Why? For everyday use it's sufficient, but test for your data.

💡 Tip: For pipes, always use -c, e.g., mysqldump db | gzip -c > db.sql.gz – this compresses without temporary files.

Practical application examples

In administration, you use gzip daily.

Here are examples:

Log compression:


gzip /var/log/syslog.*

# reduces old logs, saves space. Why? Logs grow fast, gzip halves them.


tar -czvf backup.tar.gz /home/user

# daily user backup. What for? For quick restores.


df -h | gzip > disk_usage.gz

# compresses output. This connects with streams.

💡 Tip: In cron jobs: find /var/log -name "*.log" -mtime +7 | xargs gzip – automates old logs.

Typical source of error: Forgetting -c in pipes causes errors. Solution: always handle stdout explicitly.

bzip2 – Better compression for large files

bzip2 is the next step when gzip doesn't save enough. Based on Burrows-Wheeler Transform (BWT) plus Huffman, it achieves higher ratios than gzip, but is slower. What happens? bzip2 blocks files into 100–900KB chunks, sorts them, and encodes – ideal for text-based data. Why do we do this? For large files like logs or DB dumps, where 10–20% more savings count.

What to watch for? It's CPU-intensive, so not for real-time. What will you need this for? For monthly archives where size is prioritized.

Basic operations

Syntax: bzip2 [options] file. Similar to gzip, replaces original with .bz2.

Compressing, decompressing, and with streams:


bzip2 file.txt  # Creates file.txt.bz2.
bunzip2 file.txt.bz2  # Or bzip2 -d.
cat file | bzip2 > file.bz2  # with streams


tar -cjf backup.tar.bz2 /var/log # combined for better ratio.

Typical source of error: bzip2 on small files – overhead makes them larger. Solution: For < 1MB, prefer gzip.


┌─────────────────────────────────────────────────────────────┐
│      BZIP2 COMPRESSION: BURROWS-WHEELER TRANSFORM          │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│   file.txt (10 MB) ──► [ bzip2 ] ──► file.bz2 (2 MB)        │
│                             │                               │
│                             ▼                               │
│                     BWT algorithm:                          │
│                     ├─ Burrows-Wheeler block sorting        │
│                     ├─ Move-To-Front & Run-Length           │
│                     └─ Huffman encoding                     │
│                                                             │
└─────────────────────────────────────────────────────────────┘

Important options

Option Description Example
-1 to -9 Level (1=fast, 9=best) bzip2 -9 log.txt
-d Decompress bzip2 -d archive.bz2
-k Keep original bzip2 -k file
-v Verbose bzip2 -v bigfile (shows blocks)
-s Small blocks (less RAM) bzip2 -s hugefile

Levels change block size: -9 = 900KB blocks for better ratio.

💡 Tip: -v with -v (twice) for detailed stats – good for benchmarking.


bzip2 -9v access.log # for large logs, shows 70-90% savings.

xz – Maximum compression for archives

  • Compressing: **xz file.txt
  • Decompressing: **unxz file.xz
  • Streams: **cat file | xz > file.xz

🔧 Practical example:


tar -cJf backup.tar.xz /dir/ # highest ratio.

Typical source of error: xz on videos – useless, as they're already compressed.


┌─────────────────────────────────────────────────────────────┐
│               XZ COMPRESSION: LZMA2 METHOD                 │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│   file.txt (10 MB) ──► [ xz ] ──► file.xz (1.5 MB)          │
│                            │                                │
│                            ▼                                │
│                    LZMA2 algorithm:                         │
│                    ├─ Very large dictionaries (1.5 GB)      │
│                    ├─ Delta filters for structural data     │
│                    └─ Multi-threading support               │
│                                                             │
└─────────────────────────────────────────────────────────────┘

Important options

Option Description Example
-0 to -9 Level xz -9 log.txt
-d Decompress xz -d archive.xz
-k Keep xz -k file
-v Verbose xz -v bigfile
-e Extreme (better ratio) xz -e -9
-T Threads xz -T0 (all cores)

💡 Tip: -T0 for servers – uses all threads.

⚠️ Warning: -9e takes hours for GB – batch overnight.

🔧 Practical example:


xz -9e access.log # for ultimate savings.

Typical source of error: Out-of-RAM with -9. Solution: -3 for balance.

Practical application examples

  • ISO compression: **xz image.iso – like distros.
  • Tar: **tar -cJf db.tar.xz /db/ – best ratio for dumps.
  • Pipe: **tar -cf - /etc/ | xz -9 > etc.xz – with streams.

💡 Tip: In cron: xz -9 /backups/*.tar – compress after tar.

⚠️ Warning: xz is newer – older systems may need installation.

🔧 Practical example:


find /var -type f | tar -cf - -T - | xz > var.xz # dynamic.

Typical source of error: Wrong threads freeze system. Solution: limit with -T4.

Comparing the tools

Compression ratio, speed, and use cases

Based on tests (e.g., from 2024 benchmarks): gzip is fast (compression: 5–10x faster than xz, ratio: 60–70%), bzip2 medium (ratio: 70–80%, 2–3x slower than gzip), xz top (ratio: 80–90%, 5–10x slower). What does this mean? For a 1GB log: gzip: 300MB in 10s, bzip2: 250MB in 30s, xz: 200MB in 2min.

Tool Ratio (higher=better) Speed (compress) Speed (decompress) RAM Use case
gzip Medium (60-70%) High High Low Everyday, web, pipes
bzip2 High (70-80%) Medium Medium Medium Logs, text
xz Very high (80-90%) Low Medium High Archives, ISOs

┌─────────────────────────────────────────────────────────────┐
│            COMPRESSION COMPARISON: RATIO VS. SPEED          │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│   Compression ratio: xz (high) > bzip2 > gzip (low)         │
│   Speed:             gzip (fast) > bzip2 > xz (slow)        │
│                                                             │
│   Ratio ▲                                                   │
│        │  [ xz ]        (Max compression, high CPU load)    │
│        │  [ bzip2 ]     (Good compromise for text)          │
│        │  [ gzip ]      (High speed, standard)              │
│        └────────────────────────────────────────► Speed     │
│                                                             │
└─────────────────────────────────────────────────────────────┘

What to choose? gzip for speed (pipes), bzip2 for balance, xz for max ratio (storage).

🔧 Practical example:

Compress a log with all tools


time gzip -c log > g.gz; time bzip2 -c log > b.bz2; time xz -c log > x.xz

💡 Tip: Combined formats like tar.gz are standard in Linux distributions – learn them because you'll encounter them everywhere, from package managers to backups. Why this helps? It makes your scripts portable and efficient.

Typical source of error: Wrong options (e.g., -z for bzip2) cause "unrecognized archive format" errors. Solution: Remember: -z=gzip, -j=bzip2, -J=xz – and verify with file.

To visualize the process, here's an ASCII diagram for the combination:


┌─────────────────────────────────────────────────────────────┐
│                 TAR & COMPRESSION PIPELINE                  │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│   ┌─────────────┐     ┌───────────┐     ┌─────────────┐     │
│   │ Source data │ ──► │ tar       │ ──► │ Compression │     │
│   │ /etc/, /var │     │ (bundles) │     │ (gzip / xz) │     │
│   └─────────────┘     └───────────┘     └─────────────┘     │
│                              │                 │            │
│                              ▼                 ▼            │
│                         tar data stream   compress stream   │
│                                                             │
└─────────────────────────────────────────────────────────────┘

Creating compressed archives

(tar.gz, tar.bz2, tar.xz)

Creating compressed archives is straightforward: tar -c [compression-option] f archive.tar.[ext] files. What happens? tar bundles, internally forwards to the tool (gzip etc.), and writes the compressed file. Why internal integration? Tar invokes the tools automatically – saving commands. What to watch for? The extension: .gz for gzip, .bz2 for bzip2, .xz for xz – this helps with identification.

What for in practice? For versioned backups, e.g., tar.gz for daily, tar.xz for weekly (better ratio).

Step by step:


tar.gz: tar -czf archive.tar.gz /dir/ -c=create, -z=gzip, -f=file. # Ratio: 60%, fast.
tar.bz2: tar -cjf archive.tar.bz2 /dir/ -j=bzip2. # Ratio: 70-80%, medium speed.
tar.xz: tar -cJf archive.tar.xz /dir/ -J=xz. # Ratio: 80-90%, slow.

# With levels:
tar -cJf archive.tar.xz /dir/ --options=xz:9 # max ratio for xz.


tar -czvf backup.tar.gz /etc/

# Output shows files and compression

💡 Tip: For custom levels: tar -cf - /dir/ | xz -9 > archive.tar.xz – more flexible with pipes.

⚠️ Warning: High levels slow things down – test on test data before using in production.

🔧 Practical example:


tar -czf logs_$(date +%Y%m%d).tar.gz /var/log/ # dated backup. Why? For rotation in cron.

  • tar.gz: tar -xzf archive.tar.gz -C /target/
  • tar.bz2: tar -xjf archive.tar.bz2
  • tar.xz: tar -xJf archive.tar.xz
  • Selective: tar -xzf archive.tar.gz file1
  • With pipes: ssh user@remote 'cat archive.tar.gz' | tar -xzf -

# Extract with preserved permissions
tar -xzpf backup.tar.gz -C /restore/
# -p preserves permissions

💡 Tip: Check with --list (-t): tar -tzf archive.tar.gz | less – contents without extraction.

⚠️ Warning: Extracting as root can overwrite permissions – run as regular user.

🔧 Practical example:


tar -xJf full.xz -C /tmp/ --strip=1 # strips directories, for clean restore.


┌─────────────────────────────────────────────────────────────┐
│                 TAR DECOMPRESSION IN FLOW                   │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│   archive.tar.gz ──► [ tar -xzf ] ──► /target/directory/    │
│                           │                                 │
│                           ▼                                 │
│                 1. Unpack (gunzip)                          │
│                 2. Extract & set permissions                │
│                                                             │
└─────────────────────────────────────────────────────────────┘

Advanced options for combined operations

Advanced features make the combination powerful. What happens? You fine-tune with tar and compression options. Why? For optimization, e.g., multi-thread or exclude. What to watch for? Compatibility – xz options only with -J.

Options:

  • --options: tar -cJf archive.xz --options=xz:threads=0,xz:9 – all threads, max level.
  • --exclude: tar -czf backup.gz /home --exclude=*.tmp --exclude=cache/
  • -M: tar -cMf backup.tar.gz /big/ – multi-volume for >4GB.
  • With pipes: tar -cf - /dir/ --exclude=proc | bzip2 -9 > archive.bz2

# Advanced with exclude and level
tar -czf backup.tar.gz /var --exclude=/var/run --options=gzip:9

💡 Tip: For parallel: tar -cf - /dir/ | pbzip2 -9 > archive.bz2 – faster on multi-core.

⚠️ Warning: --options not for all – gzip doesn't support it, only xz/bzip2.

🔧 Practical example:


tar -cJf archive.xz /data/ --options=xz:extreme,xz:threads=4 # extreme for best ratio.

Typical source of error: Wrong syntax in --options – leads to "unknown option". Solution: Check the man page (man xz).

Practical examples with pipes and streams

Pipes make the combination dynamic. What happens? tar uses stdin/stdout for on-the-fly processing. Why? No temps, real-time compression. What to watch for? Pipe buffers – use pv for monitoring with large data.

Examples:

  • Dynamic: find /etc -name "*.conf" | tar -czf config.tar.gz -T -
  • Remote: tar -czf - /home/ | ssh remote 'cat > home.tar.gz'
  • With compression level: tar -cf - /logs/ | xz -9 > logs.xz

# Pipe with find and gzip
find /var/log -type f -mtime -30 | tar -czf recent_logs.tar.gz -T -
# Archives logs from the last 30 days

💡 Tip: With tee: tar -czf - /dir/ | tee archive.tar.gz | sha256sum – compresses and checksums in one.

🔧 Practical example:


mysqldump --all-databases | tar -czf - -T /dev/stdin | scp - user@backup:db.tar.gz # DB backup via pipe.

Typical source of error: Forgetting -T with stdin – leads to "no files". Solution: -T - for pipe input.

Practical backup strategies

Now that you've mastered the tools for archiving and compression, let's apply what you've learned to real-world scenarios: practical backup strategies. What happens here? You combine tar, gzip & co. with scripts, pipes, and automation to secure data – from local logs to complete system snapshots. Why do we do this? Backups are your safety net against data loss from hardware failures, ransomware, or human error; in system administration, they can mean the difference between hours and days of downtime.

What should you watch for? Consistency, automation, and testability – a backup you don't regularly test is worthless. What will you need this for later in practice? For compliance-compliant data protection in enterprises, quick restores after upgrades, or scaling server farms where automated backups are essential. In the LPIC-1 exam, this comes up in 103.5 and 104.5, often integrated with pipes to simulate real workflows.

In this section, we proceed step by step: From the fundamentals of a strategy, through backup types, to automation, cloud integration, troubleshooting, and best practices. We build on the previous sections – think tar with gzip in pipes for efficient streams. Try out the examples to see how backups can ease your daily work.

💡 Tips and hints: Start small – back up /etc/ first before backing up the entire system. Why? This builds confidence and helps catch errors early.

Backup strategy overview:


┌─────────────────────────────────────────────────────────────┐
│                BACKUP ARCHITECTURE & WORKFLOW               │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│   ┌─────────────┐     ┌────────────────┐     ┌───────────┐  │
│   │ Data source │ ──► │ Archiving &    │ ──► │ Storage   │  │
│   │ /etc, /home │     │  compression   │     │ Local/S3  │  │
│   └─────────────┘     └────────────────┘     └───────────┘  │
│                              │                      │       │
│                              ▼                      ▼       │
│                       Automation           Restore test     │
│                       (cron / systemd)     (monthly)        │
│                                                             │
└─────────────────────────────────────────────────────────────┘

Fundamentals of a backup strategy

A solid backup strategy is the cornerstone of every reliable administration. What happens here? You define what, how often, and where to back up, considering RTO (Recovery Time Objective) and RPO (Recovery Point Objective). Why do we do this? To minimize data loss and enable quick recovery – think about a server crash: with a good strategy, you're back online in minutes. What to watch for?

The 3-2-1 rule: 3 copies, 2 media, 1 offsite. What will you need this for later? For compliance (e.g., GDPR), where you must prove data is backed up, or in multi-server environments where centralized backups need to scale.

Step-by-step setup:

  1. Identify data: Critical (databases, configs) vs. non-essential (temps). Why? Focus resources on what matters, e.g., /etc/ and /var/lib/mysql/.
  1. Choose tools: tar for bundling, gzip/xz for compression, rsync for diffs. Integrate pipes: rsync -av /dir/ /backup/ | tar -czf daily.tar.gz.
  1. Define frequency: Daily for changes, weekly for full. Note: incremental to save bandwidth.
  1. Storage: Local for speed, cloud for redundancy. Test: Check mount points to avoid overfilling.

💡 Tips and hints: Use versioning – append date/time to filenames, e.g., backup_$(date +%Y%m%d).tar.gz. Why? Enables rollbacks to specific points.

⚠️ Warnings and pitfalls: Not testing backups – many discover too late that they're corrupt. Test monthly with tar -xzf and verification.

🔧 Practical examples:


# For a web server:
tar -czf web_backup.tar.gz /var/www/ /etc/apache2/ # backs up site and configs.

Typical sources of error: Forgetting to exclude temps – archives bloat. Solution: --exclude=/var/run/* in tar.

3-2-1 backup rule:


┌─────────────────────────────────────────────────────────────┐
│                 THE 3-2-1 BACKUP STRATEGY                  │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│   ┌─────────────┐     ┌─────────────┐     ┌─────────────┐   │
│   │   Copy 1    │     │   Copy 2    │     │   Copy 3    │   │
│   │ (local) NVMe│     │ (external)  │     │(offsite) S3 │   │
│   │             │     │    NAS      │     │             │   │
│   └─────────────┘     └─────────────┘     └─────────────┘   │
│          │                   │                   │          │
│          ▼                   ▼                   ▼          │
│   Production system   Local backup       Geo-redundancy     │
│                                                             │
└─────────────────────────────────────────────────────────────┘

Incremental vs. full backups

Incremental and full are the two main types. What happens with full? You back up everything anew, e.g., tar -czf full.tar.gz /dir/ – this creates a complete snapshot. Why? Simple and independent, ideal for weekly runs. What to watch for? High storage requirements and time – not for daily use.

Incremental: Only changes since last backup, e.g., with rsync -av --link-dest=prev_backup /dir/ new_backup/. Why? Saves 90% time/space by using hardlinks. What for? Daily backups where you integrate pipes: rsync … | tar -czf incr.tar.gz.

Comparison table:

Type Advantages Disadvantages Use case
Full Fast restore, independent High consumption Weekly/Monthly
Incremental Efficient, space-saving Complex restore (chain) Daily

💡 Tips and hints: Use --link-dest in rsync for incremental with hardlinks – simulates full backups at minimal storage.

⚠️ Warnings and pitfalls: Incremental chains break on missing link – always run full periodically.

🔧 Practical example:


# Full
tar -czf full_$(date).tar.gz /data/.

# Incremental:
rsync -av --delete --link-dest=../full /data/ incr_$(date)/.

Typical sources of error: Forgotten deletes in incremental – junk accumulates. Solution: --delete in rsync.

Incremental vs. Full:


┌─────────────────────────────────────────────────────────────┐
│         BACKUP TYPES: FULL VS. INCREMENTAL                  │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│   Full Backup:                  Incremental (Inc):          │
│   ┌────────────────┐            ┌────────┐  ┌────────┐      │
│   │ Full (all data)│            │  Inc 1 │─►│  Inc 2 │      │
│   └────────────────┘            └────────┘  └────────┘      │
│           │                          ▲           ▲          │
│           ▼                          └─────┬─────┘          │
│   Direct restore            Basis: [ Full-Backup ]          │
│                                                             │
└─────────────────────────────────────────────────────────────┘

Automated backups with scripts

Automation makes backups reliable. What happens? You write Bash scripts that call tar/gzip and schedule them with cron. Why? Manually you forget – automation runs 24/7. What to watch for? Error handling and logs – when it fails, you need alerts. What for? For production servers where daily runs are essential.

Script example:


#!/bin/bash
# backup.sh - Automated tar.gz backup
SOURCE=/etc/
BACKUP_DIR=/backups/
DATE=$(date +%Y%m%d)
tar -czf $BACKUP_DIR/etc_$DATE.tar.gz $SOURCE 2> error.log
if [ $? -ne 0 ]; then
  mail -s "Backup failed" admin@example.com < error.log
fi
# Rotate: Delete old >30 days
find $BACKUP_DIR -mtime +30 -delete


crontab -e, 0 2 * * * /path/backup.sh # daily at 2 AM.


tar -czf - $SOURCE | scp - user@cloud:etc_$DATE.tar.gz # directly to cloud.

💡 Tips and hints: Integrate gpg: tar -czf - /dir/ | gpg -e > encrypted.tar.gz.gpg – for secure cloud.

⚠️ Warnings and pitfalls: Cron runs as root – check permissions to avoid permission issues.

🔧 Practical example:

Script for incremental:


rsync -av --link-dest=../prev /dir/ incr_$DATE/ | tar -czf incr.tar.gz.


tar -czf backup.tar.gz /dir/; aws s3 cp backup.tar.gz s3://bucket/.


tar -czf - /dir/ | aws s3 cp - s3://bucket/backup.tar.gz

💡 Tips and hints: Use lifecycle policies in S3 – auto to Glacier after 30 days for cost savings.

⚠️ Warnings and pitfalls: Insecure transfers – always use HTTPS/encryption.

🔧 Practical example:


tar -czf - /data/ | gpg -e | aws s3 cp - s3://secure/encrypted.tar.gz.gpg

Typical sources of error: API keys exposed – store in .env, don't hardcode.

Diagram for cloud flow:


┌─────────────────────────────────────────────────────────────┐
│           STREAMING BACKUP WITHOUT INTERMEDIATE             │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│   ┌─────────────┐     ┌─────────────┐     ┌─────────────┐   │
│   │ Local /var  │ ──► │ tar | gzip  │ ──► │ Remote (S3) │   │
│   └─────────────┘     └─────────────┘     └─────────────┘   │
│          │                   │                   │          │
│          ▼                   ▼                   ▼          │
│   Read source        Pipeline flow      Direct backup       │
│   (No intermediate I/O on local disk)                       │
│                                                             │
└─────────────────────────────────────────────────────────────┘

Troubleshooting and common issues

Errors happen – here are solutions. What happens with tar: Error opening archive? Wrong format. Why? Option mismatch. What to watch for? Use file.

Common issues:

  • Corrupt archives: gzip -t archive.gz – tests integrity.
  • Files too large: tar -cMf - /dir/ | gzip > vol1.tar.gz – multi-volume.
  • Permission errors: tar -xpf – preserves permissions.
  • Pipe breaks: ulimit -p unlimited – increases pipe size.

💡 Tips and hints: Log everything: tar ... 2>&1 | tee backup.log

⚠️ Warnings and pitfalls: Restore without testing – data loss. Always validate.

🔧 Practical example:


tar -tzf backup.tar.gz > /dev/null # check without extraction.

Typical sources of error: Out of space: check df -h before backup.

Best practices for secure data archiving

To wrap up: Best practices.

  • What happens? You build robust systems.
  • Why? To minimize risks.
  • What to watch for? Encryption and monitoring.
  • Follow 3-2-1.
  • Automate with cron.
  • Encrypt: tar | gpg.
  • Test: Monthly restore.
  • Monitor: Nagios for backup jobs.

💡 Tips and hints: Use borgbackup for deduplicated, encrypted backups – extends tar.

⚠️ Warnings and pitfalls: Backups too old – rotate regularly.

Consolidating exercises

To reinforce what you've learned, here are hands-on exercises. What happens here? You actively apply the concepts of archiving, compression, and their combinations, often integrated with pipes and streams as discussed in earlier sections. Why do we do this? Theory alone isn't enough – by practicing, you internalize the commands, understand pitfalls, and build routine that helps you in the LPIC-1 exam and daily work.

What should you watch for? Correct syntax, error handling, and efficiency – always measure file sizes before/after to see the compression effect.

What will you need this for later in practice? For quick problem-solving under time pressure, such as creating ad-hoc backups or analyzing archives in an incident response scenario. These exercises are designed to build step by step: from simple to complex, with focus on integration – try them in a test environment and note the results to learn.

💡 Tips and hints: Work in a temporary directory like /tmp/exercises/ to maintain cleanliness. Why? This avoids system contamination and makes cleanup easy with rm -rf /tmp/exercises/.

Four exercises total: Each with objective, steps, expected output, and reflection questions. Why four? This covers the core areas – archiving, compression, combination, and automation – without overwhelming. Note: Use test files you create to avoid risking real data.

Let's start – and remember: Errors are learning opportunities!

⚠️ Warnings and pitfalls: Don't practice as root unless necessary – many commands need sudo for system directories, but test with user files to avoid permission errors.

Exercise structure:


┌─────────────────────────────────────────────────────────────┐
│            PRACTICAL WORKFLOW: BACKUP & VALIDATION          │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│   ┌─────────────┐     ┌─────────────┐     ┌─────────────┐   │
│   │ Preparation │ ──► │ Execution   │ ──► │ Verification│   │
│   │ (test data) │     │ (tar/gzip)  │     │(tar -t / du)│   │
│   └─────────────┘     └─────────────┘     └─────────────┘   │
│          │                   │                   │          │
│          ▼                   ▼                   ▼          │
│   Create structure    Create archive     Test contents      │
│                                                             │
└─────────────────────────────────────────────────────────────┘

Exercise 1: Create and extract a simple archive

Objective: Learn to create, check, and extract a basic archive with tar – without compression, to reinforce the fundamentals. What happens here? tar bundles files into a .tar file that you later unpack. Why this exercise first? It builds the foundation before we compress, and shows how metadata (permissions, timestamps) is preserved. What to watch for? Relative paths – use -C to avoid absolute paths.

What will you need this for later? For quick snapshots of config directories, e.g., before upgrades.

Steps:

Create test data:


mkdir exercise1; cd exercise1; for i in {1..3}; do mkdir dir$i; echo "Content $i" > dir$i/file$i.txt; done


tar -cvf archive.tar dir1 dir2 dir3


tar -tvf archive.tar


mkdir extract; tar -xvf archive.tar -C extract/

💡 Reflection: Compare du -sh exercise1/ vs. du -sh archive.tarwhy is the archive larger? (metadata overhead). What did you learn? tar preserves everything but isn't compressed – perfect for quick local backups. Tips and hints: Add -p when extracting (tar -xvpf) to preserve permissions – essential for system files.

⚠️ Warnings and pitfalls: Extraction without -C overwrites – always specify target directory.

Typical sources of error: tar: Removing leading / from member names – because of absolute paths. Solution: cd /etc/; tar -cvf config.tar apache2/ – use relative paths.

Exercise 2: Compare compression with different tools

Objective: Compress a file with gzip, bzip2, and xz and compare ratio/speed. What happens here? Each tool creates a smaller file, but with different effort. Why this exercise? It shows the trade-off between speed and efficiency you need for real strategies.

What to watch for? Use time for measurement and du -sh for sizes. What will you need this for later? To choose the best tool for your data, e.g., gzip for fast logs, xz for large archives.

Steps:

Create test file:


dd if=/dev/urandom of=testfile bs=1M count=50 # 50MB random data, simulates incompressible binary data.


time gzip -9 -k testfile
du -sh testfile.gz


time bzip2 -9 -k testfile
du -sh testfile.bz2


time xz -9 -k testfile
du -sh testfile.xz


gunzip testfile.gz; md5sum testfile testfile # after decompression, ensure integrity.

💡 Reflection: Which tool saved the most? Why was xz slower? Learn: For compressible data (text) xz is top, for random data gzip is sufficient. Integrate in pipes: cat testfile | gzip -9 > gz.pipe. Tips and hints: Use pv for progress: pv testfile | gzip > test.gz – shows speed in pipes.

⚠️ Warnings and pitfalls: High levels on large files – CPU spikes. Limit to -6 on servers.

Typical sources of error: File too large – check FS limits (ext4 max 16TB). Solution: Split before compression.

Diagram for comparison:


┌─────────────────────────────────────────────────────────────┐
│              TOOL SELECTION BY USE CASE                     │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│   ┌─────────────┐     ┌─────────────┐     ┌─────────────┐   │
│   │ gzip (Speed)│     │ bzip2 (Mid) │     │ xz (Max)    │   │
│   │ 30 MB / 5 s │     │ 25 MB / 15 s│     │ 20 MB / 60 s│   │
│   └─────────────┘     └─────────────┘     └─────────────┘   │
│          │                   │                   │          │
│          ▼                   ▼                   ▼          │
│    Daily backups       Log archive        Long-term archive  │
│                                                             │
└─────────────────────────────────────────────────────────────┘

Exercise 3: Create an automated backup script with pipes

Objective: Create a script that automates tar with compression and pipes. What happens here? The script backs up a directory, compresses, and logs errors. Why? Automation is key for consistent builds on previous concepts. What to watch for? Exit codes for error handling. What for later? For cron jobs in production.

Steps:

Create script:


nano backup_script.sh


#!/bin/bash
SOURCE=/etc/
BACKUP_DIR=/backups/
DATE=$(date +%Y%m%d)
ERROR_LOG=/var/log/backup.err
tar -czf $BACKUP_DIR/etc_$DATE.tar.gz $SOURCE 2> $ERROR_LOG
if [ $? -ne 0 ]; then
  echo "Backup failed: $(cat $ERROR_LOG)" | mail -s "Backup Error" admin@example.com
else
  echo "Backup successful: $BACKUP_DIR/etc_$DATE.tar.gz"
fi
# With pipe test: df -h | gzip > $BACKUP_DIR/disk_$DATE.gz


chmod +x and test: ./backup_script.sh


crontab -e, 0 3 * * * /path/backup_script.sh # nightly.

💡 Reflection: Why error handling? Without it, you won't notice failures. What did you learn? Scripts make backups reliable, pipes integrate additional data like df. Tips and hints: Extend with rsync for incremental: rsync -av $SOURCE $BACKUP_DIR/incr_$DATE/ | tar -czf incr.tar.gz.

⚠️ Warnings and pitfalls: Cron emails flood – redirect to log.

Typical sources of error: Filename collisions – include DATE.

Exercise 4: Troubleshooting archiving issues

Objective: Simulate and fix common errors. What happens? You create issues and fix them. Why? In reality, errors occur – practice sharpens your troubleshooting. What to watch for? Man pages and strace for deep dives.

What for later? For quick incident response.

Steps:

Corrupt archive:


tar -czf bad.tar.gz /dir/; dd if=/dev/urandom of=bad.tar.gz bs=1k count=1 conv=notrunc # corrupt it.


# Then:
tar -tzf bad.tar.gz – Error.


# Fix
gzip -t bad.tar.gz; # if fails, restore from old backup.


tar -czf root.tar.gz /root/ as user # "Permission denied".


# Fix:
sudo tar -czf ... – or chown beforehand.


tar -cf - /big/ | gzip > big.gz # breaks on OOM.


# Fix
ulimit -d unlimited; # or lower level: gzip -1.


tar -xzf archive.tar.gz # in full directory – overwrites.


# Fix:
tar -xzf -C /safe/ --keep-old-files

💡 Reflection: Which errors were most common? Why is testing essential? Learn: always validate with -t and monitor resources. Tips and hints: Use tee in pipes: tar -cf - /dir/ | tee >(gzip > gz) >(md5sum > check) – multi-output.

⚠️ Warnings and pitfalls: Ignored warnings in -v – always check logs.

🔧 Practical example:


tar -czf test.tar.gz /test/; tar -tzf test.tar.gz > /dev/null || echo "Error!"

Typical sources of error: Corruption from interrupt – atomic with mv after success.

Troubleshooting diagram:


┌─────────────────────────────────────────────────────────────┐
│               TROUBLESHOOTING ARCHIVING ISSUES              │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│   ┌─────────────┐     ┌─────────────┐     ┌─────────────┐   │
│   │ Symptom     │ ──► │   Analysis  │ ──► │  Fix        │   │
│   │(e.g. Perm.) │     │(tar -v/log) │     │ (sudo/chmod)│   │
│   └─────────────┘     └─────────────┘     └─────────────┘   │
│          │                   │                   │          │
│          ▼                   ▼                   ▼          │
│   Check symptom       Find cause        Adjust permissions  │
│                                                             │
└─────────────────────────────────────────────────────────────┘

Why these concepts matter for LPIC-1

Understanding archiving and compression is centrally important for the LPIC-1 exam and appears in multiple examination objectives:

  • 103.5: Use archives and compression
  • 103.4: Streams, pipes, and redirections (integration with archiving)
  • 104.5: Basic backup strategies (practical application)

These topics account for approximately 10–15% of the points in the LPIC-1 Exam 101 and simultaneously form the foundation for many other examination tasks, such as data management in real-world scenarios.

What happens here? In the exam, you'll often be asked to execute commands like tar -czf backup.tar.gz /directory/ or explain why you choose -z for gzip. Why do we do this? These concepts test your understanding of efficient data handling, which is central to every Linux administration – from securing sensitive files to optimizing storage.

What should you watch for? Integration with pipes: the exam loves scenarios where you pipe output (e.g., from find) directly into a compressed archive, as this shows you've mastered streams from the previous part. What will you need this for later in practice? In the certification, you learn the basics you need as an admin for automated backups or data migrations – a mistake in tar can ruin an entire system backup.

💡 Tip: Practice exam questions like "Create a compressed archive of /etc/ and extract it to /tmp/" – this covers 103.5 and trains your syntax confidence.

⚠️ Warning: Many candidates underestimate options like -p (preserve permissions) – in the exam, this can lead to point deductions, as permissions are crucial for system restores.

🔧 Practical example:


tar -czf etc_backup.tar.gz /etc/; tar -xzf etc_backup.tar.gz -C /tmp/


┌─────────────────────────────────────────────────────────────┐
│          LPIC-1 EXAM RELEVANCE (TOPIC AREAS)                │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│   ┌─────────────┐     ┌─────────────┐     ┌─────────────┐   │
│   │Archiving    │ ──► │Streams/Pipes│ ──► │ Backup plan │   │
│   │(Topic 103.5)│     │(Topic 103.4)│     │ (Topic 104) │   │
│   └─────────────┘     └─────────────┘     └─────────────┘   │
│          │                   │                   │          │
│          ▼                   ▼                   ▼          │
│   tar / gzip / xz     Pipes connect      Disaster-Rec       │
│                                                             │
└─────────────────────────────────────────────────────────────┘

In the exam, examiners expect you not only to know commands by heart but to explain why you choose tar with xz for long-term backups (high ratio) or gzip for fast pipes (speed). What happens if you ignore this? You lose points in scenario questions where you must write a backup script with error handling. Why is this critical? LPIC-1 aims for real competence – concepts like these separate theorists from practitioners.

What should you watch for? Combinations: Often you must explain tar -cf - /dir/ | gzip > backup.tar.gz, which tests streams. What will you need this for in the certification? It prepares you for higher levels like LPIC-2, where backups become more complex.

💡 Tip for preparation: Simulate Exam 101 with tools like tar on virtual machines – always test restore to understand the full cycle.

⚠️ Warning: Don't skip the metadata aspects – questions about -p (permissions) or -o (owner) are frequent, as they're essential for secure restores.

🔧 Practical example – exam task:

Back up /home/user/ incrementally with rsync and compress with xz


rsync -av /home/user/ incr/; tar -cJf incr.tar.xz incr/

💡 Note: Further exam information and practice tips can be found in our fundamentals module LPIC-1: Basic Navigation and Filesystem Commands.

Command Reference (Cheatsheet)

Command / Tool Key Options & Syntax Description & LPIC-1 Exam Relevance
tar tar -czvf archive.tar.gz /directory Creates gzip-compressed archive (-c Create, -z Gzip, -v Verbose, -f File)
tar tar -xzvf archive.tar.gz -C /target Extracts gzip archive to target directory (-x Extract, -C Directory)
tar tar -tjvf archive.tar.bz2 Lists contents of a bzip2 archive without extracting (-t List, -j Bzip2)
tar tar -xJvf archive.tar.xz Extracts xz-compressed archive (-J XZ)
gzip gzip file.txt / gzip -d file.gz Compresses file to .gz / decompresses (-d or gunzip, -k keeps original)
bzip2 bzip2 file.txt / bunzip2 file.bz2 Compresses with Burrows-Wheeler to .bz2 / decompresses (-k Keep)
xz xz file.txt / unxz file.xz Compresses with LZMA2 to .xz (highest ratio) / decompresses (-k Keep)
zcat / bzcat / xzcat zcat log.gz &#124; grep "ERROR" Shows contents of compressed files directly to stdout (without extracting to disk)
zip / unzip zip -r archive.zip /folder Creates/extracts cross-platform ZIP archive (-r recursive, -l lists contents)
cpio find . &#124; cpio -ov > archive.cpio Creates cpio archive via stdin (-o Create, -i Extract, -v Verbose)
dd dd if=/dev/sda of=disk.img bs=4M status=progress Bit-accurate block copy for drives, images, and low-level backups

Further Resources

Resource Description
GNU Tar Reference Manual Official documentation for the GNU tar archiving tool and all options
GNU Gzip Documentation Reference manual for the GNU gzip compression standard and algorithms
XZ Utils & LZMA SDK Technical specifications and documentation for XZ and LZMA2
LPI: LPIC-1 Exam 101 Objectives Official learning objectives for Topic 103.5: Archiving and compression tools

Conclusion

You've made it – in this sixth part of the LPIC-1 series, you've conquered the world of archiving and compression. What happened here? We covered everything from the fundamentals through tar as a bundling tool to compressors like gzip, bzip2, and xz, including their seamless integration with pipes and streams from the previous article.

Why did we do this? Because these techniques form the core of every robust data management: they save space, time, and resources, whether in daily backups or transferring large logs. You've learned how to create efficient tar.gz archives, implement incremental strategies, and troubleshoot issues – all practice-oriented with a focus on what to watch out for to avoid pitfalls. What will you need this for later in practice? As a Linux admin, you'll use these skills daily: for automated cron jobs, cloud uploads, or quick restores after system failures, where every minute counts.

Let's summarize the key points

tar bundles and preserves structures, compressors reduce size – combined, they become powerhouses like tar.xz for maximum efficiency. The exercises showed you how to apply this hands-on, and the backup strategies give you a framework for real-world scenarios. Particularly the 3-2-1 rule and script automation are game-changers: they protect you from data loss and make your workflows scalable.

💡 Tips and hints: Always integrate validations like tar -tzf into your scripts – this saves hours of troubleshooting. And: experiment with levels (e.g., gzip -9 vs. -1) to find the optimal balance for your specific data.

In the seventh part of our LPIC-1 series, we dive into "Searching and extracting data from files" – a topic that builds perfectly, as you've learned how to handle archives before searching them. You'll explore tools like find, locate, and grep in depth, including regular expressions for precise searches. This will round out your file management skills and bring you even closer to certification.

👉 To the course overview: All LPIC-1 articles & modules

Why combine tar with compression?

The combination is more than the sum of its parts. What happens? tar creates an uncompressed stream (stdout) that you pipe directly to a compression tool, which writes the output to a file. Why do we do this? Pure tar archives are large and uncompressed, wasting storage; compression alone doesn't bundle directories, so you lose structure.

The combo solves both: structure + size reduction. What should you watch for? Compatibility – tar.gz is universal, tar.xz more modern but not universally supported. What do you need this for in practice? For scalable backups: a daily tar.gz of /var/log/ reduces cloud transfer times by 70%, minimizes costs, and speeds up restores.

LPIC-1 relevance: You must master combined commands, often with pipes, to handle dynamic file lists. Why connected with streams? Because tar -cf generates stdout that you can pipe: tar -cf - /dir/ | gzip > archive.tar.gz – this avoids temporary files and integrates into scripts.

💡 Tip: For maximum efficiency, choose based on data: text-heavy (logs)? xz. Binary (images)? gzip, for better speed.

⚠️ Warning: Combined operations are CPU-intensive – with large data, plan RAM and CPU, or OOM errors will occur.

🔧 Practical example:


tar -czf daily_backup.tar.gz /etc/ /var/log/

# combines configs and logs. Why? For quick daily snapshots sent offsite via rsync.


┌─────────────────────────────────────────────────────────────┐
│                 SYNERGY: TAR + COMPRESSION                  │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│   ┌─────────────┐     ┌─────────────┐     ┌─────────────┐   │
│   │  tar alone  │  +  │ Compression │  =  │ Combined    │   │
│   ├─────────────┤     ├─────────────┤     ├─────────────┤   │
│   │ - Bundles   │     │ - Reduces   │     │ - Bundles & │   │
│   │ - Structure │     │   size      │     │   compresses│   │
│   │ - No compr. │     │ - Single    │     │ - Full tree │   │
│   └─────────────┘     └─────────────┘     └─────────────┘   │
│                                                             │
└─────────────────────────────────────────────────────────────┘

Share & export

Export as Markdown