Archives, Compression & Backups Beginner 35 min
Lab overview

Archives, Compression & Backups

35 min 4 checked steps Beginner
The situation

The shop is about to be upgraded and the team lead wants a backup of its configuration first. On top of that, someone deleted the payments config last night and it has to come back from an old backup without touching the newer files. You will finish by automating backups with a script that keeps only the latest few.

By the end of this lab you will be able to
  • Create, list and extract tar archives, with gzip, bzip2 and xz compression
  • Exclude files from a backup and compress single files with gzip and zip
  • Restore one file from an archive into the right place without overwriting anything else
  • Write a backup script with dated file names and automatic rotation

Archiving and compressing are two different jobs.

  • An archive packs many files and folders into one file, keeping names, folders, permissions and owners. On Linux that is tar ("tape archive", from the days of tape backups).
  • Compression makes a file smaller. gzip is fast and everywhere, bzip2 is smaller but slower, xz is the smallest and slowest.

tar can do both in one go, which is why you see files like these:

FileWhat it isCreate with
backup.tararchive, not compressedtar -cf
backup.tar.gz or .tgzarchive compressed with gziptar -czf
backup.tar.bz2archive compressed with bzip2tar -cjf
backup.tar.xzarchive compressed with xztar -cJf
backup.ziparchive + compression in one format, common on Windowszip -r

The shop lives in /opt/shop (config/, uploads/, logs/), and older backups are kept in /srv/backups.

The steps
1
Your first tar archiveCreate a compressed archive of the shop's configuration and look inside it.
2
Exclude files and compress logsBack up the shop without its bulky logs, and compress an old log file.
3
Restore a single fileBring back the deleted payments.conf from last week's backup without overwriting newer files.
4
An automatic backup scriptWrite a script that makes a dated backup and keeps only the three newest.
How this lab works
  1. Press Start lab. You get your own Ubuntu machine on the right, just for you.
  2. Read each step. Commands with a ▶ Run button can be sent to the terminal in one click, but typing them yourself helps you remember.
  3. Do the Your task part, then press Check my work. The check looks at the machine and tells you what is right or still missing.
  4. Stuck? Open the hint. Nothing you do here can break anything real.
Step 1 of 4

Your first tar archive

Objective

Create a compressed archive of the shop's configuration and look inside it.

Why we do this: before any upgrade, migration or risky change, we take a backup of the configuration. tar is the tool for it on every Linux server, and it is also how Docker images, Helm charts and many release artifacts are packaged.

1. The tar flags you need

FlagMeaning
-ccreate an archive
-xextract an archive
-tlist what is inside
-z / -j / -Jcompress with gzip / bzip2 / xz
-vverbose: print each file
-f FILEthe archive file name (always put it right after f)
-C DIRchange to this directory first

Read tar -czvf as "create, gzip, verbose, file".

2. Look at what you are backing up

bash
ls -l /opt/shop/config
du -sh /opt/shop/*

3. Create the archive

bash
cd /opt/shop
tar -czvf /root/backups/shop-config.tar.gz config

We cd first so the archive stores the short path config/app.conf instead of the full opt/shop/config/app.conf. That makes restoring to another place easy. The same thing without cd:

bash
tar -czvf /root/backups/shop-config.tar.gz -C /opt/shop config

4. Look inside without extracting

bash
tar -tzvf /root/backups/shop-config.tar.gz
ls -lh /root/backups/
file /root/backups/shop-config.tar.gz

-t lists the contents with permissions, owners and sizes. Always check an archive after creating it: a backup you never tested is only a hope.

Create /root/backups/shop-config.tar.gz, a gzip-compressed tar archive of the /opt/shop/config folder, with paths starting at config/.

Expected result: tar -tzf /root/backups/shop-config.tar.gz lists config/app.conf and config/database.conf.

Hint: the commands, explained
text
tar -czvf /root/backups/shop-config.tar.gz -C /opt/shop config

What each part does

  • tar = tape archive, the standard tool to pack many files into one.
  • -c create an archive, -z compress it with gzip, -v list each file as it's added, -f FILE the archive's name (-f must come last, right before the name).
  • -C /opt/shop config: first change into /opt/shop, then archive the config folder, so the archive contains config/... instead of the full path.
  • You'll see each file path printed as it's packed (from -v).
Step 2 of 4

Exclude files and compress logs

Objective

Back up the shop without its bulky logs, and compress an old log file.

Why we do this: logs are often the biggest part of an application folder and rarely belong in a backup. Leaving them out makes backups smaller and faster. The logs themselves are usually compressed after a few days, which is exactly what logrotate does on every server.

1. Leave folders out with --exclude

bash
du -sh /opt/shop/logs /opt/shop/uploads
tar -czf /root/backups/shop-nologs.tar.gz --exclude='shop/logs' -C /opt shop
tar -tzf /root/backups/shop-nologs.tar.gz | head

Put --exclude before the folder names: GNU tar only applies it to paths that come after it. It takes a pattern, so --exclude='*.log' would skip every .log file instead. You can repeat it as many times as you need.

2. Compare the compression formats

bash
cd /tmp
tar -czf shop.tar.gz  -C /opt shop
tar -cjf shop.tar.bz2 -C /opt shop
tar -cJf shop.tar.xz  -C /opt shop
ls -lh shop.tar.*

xz is usually the smallest and gzip the fastest. Most teams use gzip unless storage is expensive.

3. Compress a single file with gzip

bash
ls -lh /opt/shop/logs
gzip /opt/shop/logs/access-2026-11-01.log
ls -lh /opt/shop/logs

gzip replaces the file with a smaller .gz version. You can still read it without unpacking:

bash
zcat /opt/shop/logs/access-2026-11-01.log.gz | head -3
zgrep checkout /opt/shop/logs/access-2026-11-01.log.gz | wc -l

gunzip file.gz (or gzip -d) turns it back. Add -k to keep the original file.

4. zip, for sharing with Windows users

bash
cd /opt/shop && zip -r /tmp/uploads.zip uploads
unzip -l /tmp/uploads.zip | tail -3
  1. Create /root/backups/shop-nologs.tar.gz containing the whole /opt/shop folder except logs/.
  2. Compress /opt/shop/logs/access-2026-11-01.log with gzip, so only the .gz file remains.

Expected result: the archive lists shop/uploads/... but no shop/logs/, and /opt/shop/logs/access-2026-11-01.log.gz exists.

Hint: the commands, explained
text
tar -czf /root/backups/shop-nologs.tar.gz --exclude='shop/logs' -C /opt shop
gzip /opt/shop/logs/access-2026-11-01.log

What each part does

  • --exclude='shop/logs': skip anything matching this path. It must come before the folder you archive.
  • -C /opt shop: change into /opt, then archive the shop folder.
  • gzip FILE: compress one file in place. access-2026-11-01.log becomes access-2026-11-01.log.gz and the original disappears.
Step 3 of 4

Restore a single file

Objective

Bring back the deleted payments.conf from last week's backup without overwriting newer files.

Why we do this: most restores are not "restore everything". Someone deletes one file, and we need that file back from last week's backup without rolling back everything that changed since. Extracting a whole old archive over live data is a classic way to cause a second outage.

1. Confirm what is missing

bash
ls -l /opt/shop/config
cat /opt/shop/config/app.conf

payments.conf is gone, and app.conf is already at version 2.0. The old backups are in /srv/backups:

bash
ls -lh /srv/backups

2. Look inside the most recent backup

bash
tar -tzvf /srv/backups/shop-2026-11-20.tar.gz

3. Read a file without extracting it

bash
tar -xzOf /srv/backups/shop-2026-11-20.tar.gz config/payments.conf

-O sends the file to the screen instead of the disk. That is a safe way to check you have the right version.

4. Extract only one file

Name the file at the end of the command, exactly as tar -t shows it. Practise in a scratch folder first:

bash
mkdir -p /tmp/restore
tar -xzvf /srv/backups/shop-2026-11-20.tar.gz -C /tmp/restore config/payments.conf
ls -R /tmp/restore

Then put it in place:

bash
cp /tmp/restore/config/payments.conf /opt/shop/config/

You could also extract straight into /opt/shop with -C /opt/shop config/payments.conf. Because only that one file is named, nothing else is touched.

Danger: tar -xzf /srv/backups/shop-2026-11-20.tar.gz -C /opt/shop with no file name would silently overwrite app.conf with the old version 1.3.

Restore /opt/shop/config/payments.conf from /srv/backups/shop-2026-11-20.tar.gz, leaving the newer app.conf (version 2.0) as it is.

Expected result: cat /opt/shop/config/payments.conf shows the payments settings, and grep version /opt/shop/config/app.conf still prints version=2.0.

Hint: the commands, explained
text
tar -xzvf /srv/backups/shop-2026-11-20.tar.gz -C /opt/shop config/payments.conf

What each part does

  • -x extract, -z it's gzip-compressed, -v show each file, -f the archive to read.
  • -C /opt/shop: extract into this folder instead of the current one.
  • config/payments.conf at the end: extract only this one file, not the whole archive. The name must match exactly how it's stored (check with tar -tzf ARCHIVE).
Step 4 of 4

An automatic backup script

Objective

Write a script that makes a dated backup and keeps only the three newest.

Why we do this: manual backups get forgotten. In real teams a script runs every night (from cron, which is the next lab), names each backup with the date, and deletes old ones so the disk never fills up. "Disk full because of old backups" is a very common incident.

1. Dates in file names

bash
date +%F
echo "shop-$(date +%F).tar.gz"

date +%F prints YYYY-MM-DD. Names in that format sort in date order, which makes old ones easy to find.

2. Keep only the newest N files

bash
ls -1t /srv/backups/shop-*.tar.gz
ls -1t /srv/backups/shop-*.tar.gz | tail -n +4

ls -1t lists one file per line, newest first. tail -n +4 prints from the 4th line onwards, which is everything except the three newest. Those are the files to delete:

bash
ls -1t /srv/backups/shop-*.tar.gz | tail -n +4 | xargs -r rm -v --

xargs turns the list into arguments for rm, and -r means "do nothing if the list is empty". (find /srv/backups -name 'shop-*.tar.gz' -mtime +7 -delete is the other common approach: delete by age instead of by count.)

3. Write the script

bash
nano /usr/local/bin/backup-shop.sh
bash
#!/bin/bash
set -euo pipefail

BACKUP_DIR=/srv/backups
KEEP=3
FILE="$BACKUP_DIR/shop-$(date +%F).tar.gz"

tar -czf "$FILE" -C /opt/shop config
echo "created $FILE"

ls -1t "$BACKUP_DIR"/shop-*.tar.gz | tail -n +$((KEEP + 1)) | xargs -r rm -v --

set -euo pipefail makes the script stop at the first error instead of carrying on and deleting backups after a failed one.

4. Run it

bash
chmod +x /usr/local/bin/backup-shop.sh
backup-shop.sh
ls -lh /srv/backups

Create an executable script /usr/local/bin/backup-shop.sh that:

  1. archives /opt/shop/config into /srv/backups/shop-YYYY-MM-DD.tar.gz (today's date), and
  2. deletes older backups so only the 3 newest shop-*.tar.gz files remain.

Run it once.

Expected result: ls /srv/backups shows today's backup plus the two most recent older ones. The check runs your script again, so it must work more than once.

Hint

Type the script from section 3 into nano, save with Ctrl+O, Enter, exit with Ctrl+X, then run chmod +x /usr/local/bin/backup-shop.sh and backup-shop.sh.

Lab complete

Brilliant work!

You backed up the shop before an upgrade, rescued a deleted file without breaking anything newer, and built a backup script that cleans up after itself.

Interview-ready takeaways:

  • tar -czf creates, tar -tzf lists and tar -xzf extracts. Use -j for bzip2, -J for xz, and -C DIR to choose where paths start.
  • --exclude keeps logs and caches out of backups. gzip, zcat and zgrep handle single compressed files, zip -r and unzip cover the Windows-friendly format.
  • Restore one file by naming it at the end of tar -x, and check it first with tar -xOf. Never extract a whole old archive over live data.
  • Dated names ($(date +%F)) plus rotation (ls -1t | tail -n +N | xargs -r rm, or find -mtime +7 -delete) stop backups from filling the disk. Test your restores, not just your backups.

Next: the Scheduling Jobs with cron lab, where this kind of script gets run automatically.

root@devopsshack offline

Your Linux machine is ready to launch

Starting gives you a private Ubuntu server for up to 45 minutes. It is deleted when you finish.

Feedback