LINUX
Backing up System Using Tar

Procedure Manual: Backing Up Your System with tar

This guide provides step-by-step instructions for creating a tar archive of your entire Linux system while excluding specific folders. A cheatsheet and troubleshooting section are included for quick reference.


Step 1: Prepare for the Backup

  1. Decide on the Archive Location:
    Determine where to save the backup file. Ensure there is enough space on the destination drive.
    Example: /path/to/backup.tar.gz.

  2. Identify Folders to Exclude:
    Common folders to exclude include:

    • /proc (kernel and process information)
    • /sys (system files)
    • /dev (device files)
    • /run (runtime data)
    • /tmp (temporary files)
    • /mnt and /media (mounted filesystems)
    • /lost+found (file system recovery data)

Step 2: Create the Backup

Option 1: Exclude Folders Directly in the Command

Use --exclude for each directory or file to exclude:

sudo tar --exclude=/proc --exclude=/sys --exclude=/dev --exclude=/run --exclude=/tmp --exclude=/mnt --exclude=/media --exclude=/lost+found -cvpzf /path/to/backup.tar.gz /

Option 2: Use an Exclude File

  1. Create an Exclude File:
    List all directories and files to exclude in a plain text file (e.g., exclude.txt):

    /proc
    /sys
    /dev
    /run
    /tmp
    /mnt
    /media
    /lost+found
  2. Run the Command with the Exclude File:

    sudo tar --exclude-from=/path/to/exclude.txt -cvpzf /path/to/backup.tar.gz /

Step 3: Verify the Archive

After creating the backup, verify its contents to ensure everything you need is included:

tar -tvf /path/to/backup.tar.gz

This will list all files in the archive without extracting them.


Cheatsheet for tar

Basic Syntax

sudo tar [OPTIONS] -f [ARCHIVE_NAME] [SOURCE]

Key Options

  • -c: Create a new archive.
  • -v: Verbose output (list files during the operation).
  • -p: Preserve file permissions.
  • -z: Compress the archive with gzip.
  • -f: Specify the archive file name.
  • --exclude: Exclude specific files or directories.
  • --exclude-from: Use a file containing paths to exclude.

Common Commands

  1. Create an Archive:

    sudo tar -cvpzf backup.tar.gz /path/to/source
  2. Exclude Directories:

    sudo tar --exclude=/path/to/exclude -cvpzf backup.tar.gz /
  3. Use an Exclude File:

    sudo tar --exclude-from=/path/to/exclude.txt -cvpzf backup.tar.gz /
  4. List Archive Contents:

    tar -tvf backup.tar.gz
  5. Extract an Archive:

    sudo tar -xvpzf backup.tar.gz -C /path/to/destination

Troubleshooting Tips

Problem: "tar: Exiting with failure status due to previous errors"

  • Cause: Missing permissions or inaccessible files.
  • Solution: Use sudo to ensure you have the necessary privileges.

Problem: Archive Fails to Compress

  • Cause: Insufficient disk space for the compressed file.
  • Solution: Check available space with df -h and ensure the destination drive has enough space.

Problem: Excluded Folders Are Still Included

  • Cause: Incorrect --exclude syntax.
  • Solution: Ensure the paths in the exclude file or command match the actual directory structure.

Problem: "gzip: stdin: unexpected end of file"

  • Cause: Corrupted archive or interrupted backup process.
  • Solution: Retry the backup process and ensure enough space and resources are available.

Problem: Performance Issues During Backup

  • Cause: Limited CPU or disk I/O capacity.
  • Solution: Limit resource usage by using the --checkpoint or --checkpoint-action options for progress monitoring or reduce compression by removing -z.