gzip and tar ‐ #backups - five4nets/Linux-Knowledgebase GitHub Wiki

Linux gzip and tar Tutorial for Backing Up Files

This tutorial explains how to use the Linux gzip and tar commands to compress and archive files for backups. It includes detailed command explanations, practical examples, and references for further reading.

Introduction

The gzip command compresses individual files, reducing their size, while tar (tape archive) bundles multiple files and directories into a single archive file, often combined with gzip for compression. These tools are essential for creating efficient backups in Linux.

Prerequisites

  • A Linux system with gzip and tar installed (most distributions include these by default).
  • Basic familiarity with the Linux command line.
  • Write permissions in the backup destination directory.

The gzip Command

Overview

gzip (GNU zip) compresses a single file, replacing it with a .gz file. It does not archive multiple files or preserve directory structures.

Basic Syntax

gzip [options] filename

Common Options

  • -d: Decompress a .gz file (same as gunzip).
  • -k: Keep the original file after compression.
  • -r: Recursively compress files in directories.
  • -1 to -9: Set compression level (1 = fastest, least compression; 9 = slowest, best compression; default is 6).
  • -v: Verbose output, showing compression details.

Example 1: Compressing a Single File

Compress a file named document.txt:

gzip document.txt

This creates document.txt.gz and removes document.txt.

Example 2: Compressing with Verbose Output and Keeping Original

Compress logfile.log while keeping the original file and showing details:

gzip -k -v logfile.log

Output:

logfile.log:  75.3% -- created logfile.log.gz

Both logfile.log and logfile.log.gz remain.

Example 3: Decompressing a File

Decompress document.txt.gz:

gzip -d document.txt.gz

This restores document.txt and removes document.txt.gz.

The tar Command

Overview

tar creates an archive file containing multiple files and directories. It can be combined with gzip to create compressed .tar.gz (or .tgz) archives.

Basic Syntax

tar [options] archive_name files_or_directories

Common Options

  • -c: Create a new archive.
  • -x: Extract files from an archive.
  • -f: Specify the archive file name.
  • -z: Use gzip compression (creates .tar.gz).
  • -v: Verbose output, listing processed files.
  • -C: Change to a specified directory before archiving or extracting.

Example 4: Creating a Tar Archive

Archive a directory my_project into my_project.tar:

tar -cvf my_project.tar my_project

Output lists files added to my_project.tar.

Example 5: Creating a Compressed Tar Archive

Create a compressed archive backup.tar.gz from my_project:

tar -czvf backup.tar.gz my_project

The -z flag applies gzip compression, creating backup.tar.gz.

Example 6: Extracting a Tar Archive

Extract my_project.tar to the current directory:

tar -xvf my_project.tar

Example 7: Extracting a Compressed Tar Archive

Extract backup.tar.gz to a specific directory /tmp/restore:

mkdir /tmp/restore
tar -xzvf backup.tar.gz -C /tmp/restore

Practical Backup Examples

Example 8: Backing Up a Home Directory

Create a compressed backup of the user’s home directory (/home/user) to /backups/home_backup.tar.gz:

tar -czvf /backups/home_backup.tar.gz /home/user

This archives and compresses all files and subdirectories in /home/user.

Example 9: Incremental Backup with tar

Create an incremental backup of /home/user/documents, excluding temporary files:

tar -czvf /backups/documents_backup.tar.gz --exclude="*.tmp" /home/user/documents

The --exclude flag skips files matching the pattern *.tmp.

Example 10: Restoring a Backup

Restore /backups/home_backup.tar.gz to /tmp/restore:

mkdir /tmp/restore
tar -xzvf /backups/home_backup.tar.gz -C /tmp/restore

Combining gzip and tar Manually

You can use tar to create an uncompressed archive and then compress it with gzip.

Example 11: Manual Compression

  1. Create a tar archive:
tar -cvf archive.tar my_project
  1. Compress it with gzip:
gzip archive.tar

This creates archive.tar.gz.

Example 12: Manual Decompression

  1. Decompress the .gz file:
gzip -d archive.tar.gz
  1. Extract the tar archive:
tar -xvf archive.tar

Tips for Effective Backups

  • Verify Backups: Always verify backups by extracting to a temporary location and checking file integrity.
  • Use Absolute Paths: For backups, use absolute paths (e.g., /home/user) to avoid confusion.
  • Automate Backups: Use cron to schedule regular backups.
  • Store Offsite: Copy backups to external drives or cloud storage for safety.
  • Check Disk Space: Ensure the destination has enough space before starting.

Common Errors and Solutions

  • Permission Denied: Ensure you have write permissions for the backup destination. Use sudo if needed.
  • File Not Found: Verify file or directory paths. Use ls to check existence.
  • Corrupted Archive: Test archives with tar -tzvf backup.tar.gz to detect corruption early.

References