Archives, Compression & Backups
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.
- 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.
gzipis fast and everywhere,bzip2is smaller but slower,xzis the smallest and slowest.
tar can do both in one go, which is why you see files like these:
| File | What it is | Create with |
|---|---|---|
backup.tar | archive, not compressed | tar -cf |
backup.tar.gz or .tgz | archive compressed with gzip | tar -czf |
backup.tar.bz2 | archive compressed with bzip2 | tar -cjf |
backup.tar.xz | archive compressed with xz | tar -cJf |
backup.zip | archive + compression in one format, common on Windows | zip -r |
The shop lives in /opt/shop (config/, uploads/, logs/), and older backups are kept in /srv/backups.
- Press Start lab. You get your own Ubuntu machine on the right, just for you.
- Read each step. Commands with a ▶ Run button can be sent to the terminal in one click, but typing them yourself helps you remember.
- 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.
- Stuck? Open the hint. Nothing you do here can break anything real.
Your first tar archive
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
| Flag | Meaning |
|---|---|
-c | create an archive |
-x | extract an archive |
-t | list what is inside |
-z / -j / -J | compress with gzip / bzip2 / xz |
-v | verbose: print each file |
-f FILE | the archive file name (always put it right after f) |
-C DIR | change to this directory first |
Read tar -czvf as "create, gzip, verbose, file".
2. Look at what you are backing up
ls -l /opt/shop/config
du -sh /opt/shop/*3. Create the archive
cd /opt/shop
tar -czvf /root/backups/shop-config.tar.gz configWe 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:
tar -czvf /root/backups/shop-config.tar.gz -C /opt/shop config4. Look inside without extracting
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
tar -czvf /root/backups/shop-config.tar.gz -C /opt/shop configWhat each part does
tar= tape archive, the standard tool to pack many files into one.-ccreate an archive,-zcompress it with gzip,-vlist each file as it's added,-f FILEthe archive's name (-fmust come last, right before the name).-C /opt/shop config: first change into/opt/shop, then archive theconfigfolder, so the archive containsconfig/...instead of the full path.- You'll see each file path printed as it's packed (from
-v).
Exclude files and compress logs
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
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 | headPut --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
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
ls -lh /opt/shop/logs
gzip /opt/shop/logs/access-2026-11-01.log
ls -lh /opt/shop/logsgzip replaces the file with a smaller .gz version. You can still read it without unpacking:
zcat /opt/shop/logs/access-2026-11-01.log.gz | head -3
zgrep checkout /opt/shop/logs/access-2026-11-01.log.gz | wc -lgunzip file.gz (or gzip -d) turns it back. Add -k to keep the original file.
4. zip, for sharing with Windows users
cd /opt/shop && zip -r /tmp/uploads.zip uploads
unzip -l /tmp/uploads.zip | tail -3- Create
/root/backups/shop-nologs.tar.gzcontaining the whole/opt/shopfolder exceptlogs/. - Compress
/opt/shop/logs/access-2026-11-01.logwithgzip, so only the.gzfile 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
tar -czf /root/backups/shop-nologs.tar.gz --exclude='shop/logs' -C /opt shop
gzip /opt/shop/logs/access-2026-11-01.logWhat 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 theshopfolder.gzip FILE: compress one file in place.access-2026-11-01.logbecomesaccess-2026-11-01.log.gzand the original disappears.
Restore a single file
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
ls -l /opt/shop/config
cat /opt/shop/config/app.confpayments.conf is gone, and app.conf is already at version 2.0. The old backups are in /srv/backups:
ls -lh /srv/backups2. Look inside the most recent backup
tar -tzvf /srv/backups/shop-2026-11-20.tar.gz3. Read a file without extracting it
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:
mkdir -p /tmp/restore
tar -xzvf /srv/backups/shop-2026-11-20.tar.gz -C /tmp/restore config/payments.conf
ls -R /tmp/restoreThen put it in place:
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/shopwith no file name would silently overwriteapp.confwith 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
tar -xzvf /srv/backups/shop-2026-11-20.tar.gz -C /opt/shop config/payments.confWhat each part does
-xextract,-zit's gzip-compressed,-vshow each file,-fthe archive to read.-C /opt/shop: extract into this folder instead of the current one.config/payments.confat the end: extract only this one file, not the whole archive. The name must match exactly how it's stored (check withtar -tzf ARCHIVE).
An automatic backup script
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
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
ls -1t /srv/backups/shop-*.tar.gz
ls -1t /srv/backups/shop-*.tar.gz | tail -n +4ls -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:
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
nano /usr/local/bin/backup-shop.sh#!/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
chmod +x /usr/local/bin/backup-shop.sh
backup-shop.sh
ls -lh /srv/backupsCreate an executable script /usr/local/bin/backup-shop.sh that:
- archives
/opt/shop/configinto/srv/backups/shop-YYYY-MM-DD.tar.gz(today's date), and - deletes older backups so only the 3 newest
shop-*.tar.gzfiles 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.
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 -czfcreates,tar -tzflists andtar -xzfextracts. Use-jfor bzip2,-Jfor xz, and-C DIRto choose where paths start.--excludekeeps logs and caches out of backups.gzip,zcatandzgrephandle single compressed files,zip -randunzipcover the Windows-friendly format.- Restore one file by naming it at the end of
tar -x, and check it first withtar -xOf. Never extract a whole old archive over live data. - Dated names (
$(date +%F)) plus rotation (ls -1t | tail -n +N | xargs -r rm, orfind -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.
Pack and compress files with tar, gzip and zip, restore a single file from an old backup and write a dated backup script that cleans up after itself.
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.