Skip to content
Visual explainer 3h to learn 5 steps

Backups & archiving with tar and rsync

Create compressed archives and incremental rsync backups and prove they restore.

By Invicly Tech, , in Linux Administration (RHCSA-style). About 10 minutes to read.

Topic 37 of 42

In short

tar bundles files into an archive, optionally compressed with gzip, bzip2 or xz, and preserves permissions. rsync copies only what changed, locally or over SSH, which makes it ideal for regular backups. A backup only counts once you have restored from it successfully.

Explained simply

A backup is a copy of your data stored somewhere else, so you can get it back after a mistake, a broken disk or an attack. It sounds simple, but most people only find out their backups are broken on the day they need them.

tar packs many files and folders into one archive file, keeping owners, permissions and dates. It can also compress the archive to save space. It is great for a snapshot of a folder, like all of /etc before a big change.

rsync copies folders from one place to another, even to another server over SSH. The first copy takes a while, but after that rsync only sends what changed. That makes it perfect for nightly backups.

A popular rule says: keep 3 copies of your data, on 2 different types of storage, with 1 copy off-site. And a backup only counts once you have restored from it and checked the result.

Press Play on a diagram to walk through it one step at a time.

CompareBackup tools
tar
  • Single archive file
  • Compress with z, j or J
  • Good for snapshots and transfers
  • Restore with tar -x
rsync
  • Copies only differences
  • Works over SSH
  • --delete mirrors removals
  • Great for nightly backups
3-2-1 rule
  • 3 copies of the data
  • 2 different media
  • 1 copy off-site
  • Test restores regularly
LayersWhat each layer protects against
  1. 1
    RAID or mirrored disks

    A single disk failing; not deletion

  2. 2
    Local snapshots

    Quick undo of mistakes; same hardware

  3. 3
    Backup on another server

    Server loss; rsync over SSH

  4. 4
    Off-site copy

    Fire, theft, site outage

  5. 5
    Offline or immutable copy

    Ransomware and admin mistakes

In depth

tar options that matter

tar has three main modes: c create, x extract and t list. f names the archive file and must come right before the file name. Compression is one letter: z for gzip (.tar.gz), j for bzip2 (.tar.bz2), J for xz (.tar.xz), or --zstd for zstd on newer systems. gzip is fast; xz is smaller but much slower; zstd is both fast and small.

tar removes the leading / from paths, so tar czf etc.tgz /etc stores etc/... and extracting it in /tmp/restore creates /tmp/restore/etc. Use -C DIR to choose where to extract. On RHEL, add --xattrs --acls --selinux so ACLs and SELinux labels survive. --exclude='*.log' skips files, and tar xzf archive.tgz etc/hosts extracts one file only.

  • tar tvf archive.tar.gz lists contents with owners and sizes
  • tar df archive.tar -C / compares an archive with the disk
  • gzip -t file.tar.gz tests the compressed file for damage

How rsync decides what to copy

By default rsync compares each file's size and modification time on both sides. If they match, the file is skipped. If not, it sends only the changed blocks, using a rolling checksum. That is why the second run of a big backup can take seconds.

-a (archive) is shorthand for -rlptgoD: recursive, keep links, permissions, times, group, owner and device files. It does not include hard links, ACLs or extended attributes; add -H, -A and -X for a full copy. --delete removes files from the target that no longer exist in the source, making a true mirror. -c forces full checksums when times cannot be trusted, but it is much slower. Always try a new command with -n (dry run) and -v first.

Flowrsync -a --delete src/ host:dst/
  1. 1
    Connect over SSH

    Starts rsync on the other side

  2. 2
    Build file lists

    Both sides list names, sizes, times

  3. 3
    Skip unchanged

    Same size and time: nothing sent

  4. 4
    Send differences

    Only changed blocks of changed files

  5. 5
    Delete extras

    --delete removes files gone from src

Snapshot-style backups with --link-dest

A plain mirror has a weakness: if a file is deleted or encrypted by ransomware today, tonight's sync copies that damage over the backup. You want several points in time, without storing everything many times.

rsync --link-dest solves this. Each night you create a new dated folder, and for files that did not change, rsync makes a hard link to the copy in yesterday's folder instead of copying it again. Every folder looks like a full backup, but unchanged files take no extra space. To delete an old day, just remove its folder; files still used by other days stay. Tools like rsnapshot automate this idea.

  • rsync -a --delete --link-dest=../2026-11-09 /srv/ 2026-11-10/
  • Hard links only work within one filesystem on the backup side
  • du -sh across folders counts shared files only once

RPO, RTO and the 3-2-1 rule

Two numbers describe what a backup plan must achieve. RPO (recovery point objective) is how much data you can afford to lose: nightly backups mean up to 24 hours. RTO (recovery time objective) is how long a restore may take. A plan with only off-site tape might meet the RPO but miss a four-hour RTO.

The 3-2-1 rule protects against different failures: 3 copies so one loss is not fatal, 2 kinds of storage so one hardware fault does not hit both, and 1 off-site copy against fire or theft. Many teams add one offline or immutable copy that an attacker with admin access cannot delete. A RAID array or a snapshot on the same disk is not a backup.

Databases and restore tests

Copying the files of a running database with tar or rsync often gives a broken backup, because the files change during the copy. Use the database's own tool, such as pg_dump or mysqldump, or stop the service or take a filesystem snapshot first.

Test restores on a schedule, not just once. Restore into a temporary folder or a test VM, then compare: diff -r for folders, sha256sum for files, and for a database, start it and run a few queries. Time the restore to check your RTO. Write the restore steps down, because the person restoring at 3 a.m. may not be the person who set up the backup.

Learn it in 5 short steps

About 35m each. Track them with a timer in your plan.

  1. 1
    Create and extract tar.gz and tar.xz~35m
  2. 2
    Sync directories with rsync~35m
  3. 3
    Back up to a remote host over SSH~35m
  4. 4
    Apply the 3-2-1 backup rule~35m
  5. 5
    Test restores regularly~35m

Key points

  • tar czf creates gzip archives, tar cJf xz archives; tar tf lists contents.
  • Use -C to choose where to extract instead of cd.
  • A trailing slash on the rsync source copies its contents, not the directory itself.
  • rsync -a preserves permissions and times; add -n first for a dry run.

Key terms

Tap a card to flip it. 0/5 known

Examples

bash
sudo tar czf /backup/etc-$(date +%F).tar.gz /etc
tar tzf /backup/etc-2026-11-09.tar.gz | head
sudo tar xzf /backup/etc-2026-11-09.tar.gz -C /tmp/restore
rsync -an --delete /srv/web/ backup01:/backups/web/   # dry run
rsync -a --delete /srv/web/ backup01:/backups/web/
tar cJf home.tar.xz /home && ls -lh home.tar.xz

Archive, list, restore elsewhere and sync incrementally over SSH.

Nightly snapshot backups to another server

You want a backup of /srv every night on backup01, with 14 days of history. Step 1: compute today's and yesterday's folder names. Step 2: rsync /srv into today's folder, hard-linking unchanged files to yesterday's. On the very first night yesterday's folder does not exist; rsync prints a warning and makes a full copy. Step 3: on the backup server, keep the 14 newest dated folders and delete the rest; the names sort by date, so this is safe. Run it from a systemd timer as a user with an SSH key.

bash
#!/usr/bin/env bash
set -euo pipefail
dest=backup01:/backups/web01
today=$(date +%F)
yday=$(date -d yesterday +%F)
rsync -aHAX --delete --numeric-ids \
  --link-dest="../$yday" /srv/ "$dest/$today/"
ssh backup01 'ls -1d /backups/web01/20* | head -n -14 | xargs -r rm -rf'   # keep the 14 newest
logger -t backup "web01 /srv saved to $today"

Prove an /etc archive restores correctly

Before a big change you saved /etc with tar. Now prove the archive is usable. Step 1: test the compressed file. Step 2: extract it into an empty folder, never over the live /etc. Step 3: compare the extracted tree with the real one; only files changed since the backup should differ. Step 4: restore a single file if needed, with its owner and mode kept.

bash
sudo tar --xattrs --acls --selinux -czf /backup/etc-$(date +%F).tgz /etc
gzip -t /backup/etc-2026-11-10.tgz && echo archive ok
sudo mkdir -p /tmp/restore
sudo tar -xzf /backup/etc-2026-11-10.tgz -C /tmp/restore
sudo diff -r /etc /tmp/restore/etc | head
# restore one file in place:
sudo tar -xzf /backup/etc-2026-11-10.tgz -C / etc/ssh/sshd_config

Quick reference

  • tar czf a.tgz dir (gzip), cjf (bzip2), cJf (xz), --zstd -cf (zstd)
  • tar tzf lists, tar xzf a.tgz -C /tmp/restore extracts somewhere safe
  • On RHEL add --xattrs --acls --selinux to keep ACLs and labels
  • rsync -a = -rlptgoD; add -H -A -X for hard links, ACLs and xattrs
  • src/ copies the contents; src copies the folder itself
  • Always run rsync --delete with -n first and read the output
  • --link-dest=../yesterday gives daily snapshots that share unchanged files
  • 3-2-1: three copies, two media, one off-site, and test restores

Common mistakes

  • Using rsync --delete with the wrong source path and wiping the backup.
  • Never testing a restore until the day it is needed.

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1Which command creates an xz-compressed archive of /home?

Question 2

2What does rsync -a NOT preserve by default?

Question 3

3Why is a nightly rsync --delete mirror alone a weak backup?

Question 4

4What does RPO describe?

Question 5

5You run tar czf etc.tgz /etc and extract it in /tmp/r. Where do the files end up?

0/5 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    Create a gzip archive of /etc/ssh, list its contents, and extract it into /tmp/sshcopy.

  2. Exercise 2medium

    Mirror ~/projects to a remote host, preview the changes first, then run it for real. Delete a local file and show that the mirror removes it too.

  3. Exercise 3hard

    Make two snapshot backups of a folder with --link-dest and prove that an unchanged file is stored only once.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

Is RAID a backup?
No. RAID keeps a server running when a disk fails, but a deleted or encrypted file is gone on every disk at once. You still need copies elsewhere.
Which compression should I use with tar?
gzip is the safe, fast default. xz makes smaller files but is slow to create. zstd, where available, is fast and compresses well.
Should the rsync source have a trailing slash?
With a slash, rsync copies the folder's contents into the target. Without it, it creates the folder itself inside the target. Pick one style and always dry-run first.
How often should I test restores?
On a regular schedule, such as monthly for important systems, and after any change to the backup setup. Time the restore so you know whether you meet your RTO.

Go deeper

ShareXLinkedInWhatsApp