Skip to content
$ lynuxpro
← Linux

LESSON 12 OF 26 · INTERMEDIATE · 15 MIN

Archives, compression and backups

In one minute

  • Tools to bundle many files into one (tar), make files smaller (compression), copy them smartly between machines (rsync), and plan backups that really work.
  • You will package files all the time: bundling an app for deployment, sending logs to a vendor, backing up configs before an upgrade, copying data between servers.
  • tar packs files like a suitcase. gzip squeezes the suitcase smaller. rsync copies only what changed since last time. A backup plan decides what to copy, how often, where to keep it, and how to get it back.
  • 👉 Before every risky change, for deployments, for moving data, and for nightly backups.

Think of it like: moving house. tar = packing everything into boxes. Compression = vacuum-packing the boxes so they take less space. rsync = only moving the boxes that are new.

tar: bundling files

tar (from "tape archive") joins many files and folders into one file, keeping their permissions and folder layout.

👉 Backups, shipping an app, or sending a folder to someone.

tar czf backup.tgz /etc/nginxtar xzf backup.tgz -C /tmp
SHELLbashutf-8 · 2L

The letters are options:

LetterMeaning
cCreate an archive
xExtract an archive
tList what is inside, without extracting
zCompress with gzip (.tar.gz or .tgz)
j / JCompress with bzip2 / xz (smaller, slower)
vVerbose: print each file
f nameThe archive's file name (put f last, right before the name)
-C dirExtract into this folder

Remember it: create, extract, list: "Create, eXtract, Tell me what is inside". Add z to zip it, and f comes last before the file name.

Common recipes:

tar czf app-1.4.tgz /opt/app             # create a compressed archivetar tzf app-1.4.tgz | head               # look inside firsttar xzf app-1.4.tgz -C /opt/restore      # extract somewhere specifictar czf logs.tgz --exclude="*.tmp" /var/log/app
OUTPUTterminalutf-8 · 4L

Always list an archive with t before you extract one from someone else, so you know where the files will land.

Compression tools

Compression makes files smaller, so they use less disk and copy faster.

👉 Old logs, backups and anything you send over the network.

ToolExtensionSpeedSizeNotes
gzip.gzFastGoodThe usual default
bzip2.bz2SlowerSmallerOlder
xz.xzSlowestSmallestGood for software releases
zstd.zstVery fastSmallThe modern choice, used by many tools
zip.zipFastGoodBest for sharing with Windows users
gzip app.loggunzip app.log.gz
SHELLbashutf-8 · 2L

gzip file replaces the file with file.gz. To read a compressed log without unpacking it, use zcat or zgrep:

zgrep ERROR /var/log/syslog.2.gz
OUTPUTterminalutf-8 · 1L

rsync: smart copying

rsync copies files, but only the parts that changed. It can resume a broken copy and works over SSH.

Copying 100 GB every night is slow. If only 1 GB changed, rsync copies just that 1 GB.

👉 Syncing folders, simple backups, moving big data between servers.

rsync -av /opt/app/ backup-host:/backups/app/
SHELLbashutf-8 · 1L
OptionMeaning
-aArchive mode: everything inside, keeping permissions, times and links
-vVerbose
-zCompress while sending
-PShow Progress and allow resuming
--deleteDelete files at the destination that are gone from the source
-nDry run: show what would happen, change nothing
--excludeSkip matching files

The slash at the end matters:

  • rsync -a src/ dest/ copies what is inside src into dest.
  • rsync -a src dest/ copies the folder src into dest, giving dest/src.

Remember it: Slash at the end = "the contents of". No slash = "the folder itself".

Always do a dry run (-n) before using --delete. If the source path is wrong, --delete can wipe the destination.

scp: quick one-off copies

scp copies a file to or from another machine over SSH.

👉 A quick, single copy. For anything big or repeated, use rsync.

scp app.tgz ubuntu@10.0.1.5:/tmp/          # local to remotescp ubuntu@10.0.1.5:/var/log/app.log .     # remote to local
OUTPUTterminalutf-8 · 2L

Designing backups that work

A backup plan answers: what do we copy, how often, where do we keep it, and how do we get it back?

A backup you have never restored is only a hope. Many teams find out their backups are empty during an outage.

Follow the 3-2-1 rule:

  • 3 copies of your data
  • on 2 different types of storage
  • with 1 copy somewhere else (another region or another account)
QuestionGood answer
What is backed up?Data and configs. Not things you can rebuild from Git or images
How often?Depends on how much data you can afford to lose (called RPO)
How long is it kept?For example: daily for 7 days, weekly for 4 weeks, monthly for 12 months
Is it encrypted?Yes, both when stored and when sent
Is restoring tested?Yes, on a schedule, timing how long it takes (called RTO)

Remember it: RPO = how much data can we lose? RTO = how long can we be down?

Databases are special. Use the database's own tool (pg_dump, mysqldump) or managed snapshots (such as RDS). Do not tar the live data files. You can get a broken copy, because the files change while you copy them.

A simple scheduled backup

#!/usr/bin/env bashset -euo pipefailSTAMP=$(date +%F)tar czf "/backups/etc-$STAMP.tgz" /etcaws s3 cp "/backups/etc-$STAMP.tgz" s3://acme-backups/etc/find /backups -name "etc-*.tgz" -mtime +7 -delete
OUTPUTterminalutf-8 · 6L
  1. Make a date stamp, such as 2026-11-30.
  2. Pack /etc into a dated archive.
  3. Copy it off the server, to S3.
  4. Delete local copies older than 7 days.

Run it every night with a systemd timer or cron. You will build a full version in the Bash Scripting course.

Real-world scenario: back up before an upgrade

Before upgrading nginx, you save its config so you can go back in seconds:

tar czf backup.tgz /etc/nginxtar xzf backup.tgz -C /tmpls /tmp
SHELLbashutf-8 · 3L

If the upgrade breaks something, restoring is one extract and one reload.

Common mistakes

  • rsync --delete with the wrong path, which deletes the backup itself. Dry-run first.
  • Forgetting the end-slash rule, and ending up with folders one level too deep.
  • Never testing restores.
  • Backing up live database files with tar. Use database dump tools or snapshots.

Practice

  1. Make a compressed archive of /etc/nginx called nginx-backup.tgz.
  2. Extract it into /tmp and check that the files are there.
  3. Compress and uncompress a log file with gzip.
  4. Explain the difference between rsync -a src/ dest/ and rsync -a src dest/.

Cheat sheet

CommandWhat it does
tar czf a.tgz dirCreate a gzip archive
tar tzf a.tgzList what is inside
tar xzf a.tgz -C dirExtract into a folder
gzip f / gunzip f.gzCompress / uncompress
zgrep x f.gzSearch inside a compressed file
rsync -avn src/ dest/Dry-run sync
scp f host:/pathCopy a file over SSH

Key takeaways

  • tar packs many files into one. tar czf packs, tar xzf unpacks.
  • gzip makes files smaller.
  • rsync copies only what changed. Try it with -n first.
  • A good backup has 3 copies, on 2 kinds of storage, with 1 kept somewhere else.
  • A backup only counts if you have tested getting the data back.