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.
tarpacks files like a suitcase.gzipsqueezes the suitcase smaller.rsynccopies 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 /tmpThe letters are options:
| Letter | Meaning |
|---|---|
c | Create an archive |
x | Extract an archive |
t | List what is inside, without extracting |
z | Compress with gzip (.tar.gz or .tgz) |
j / J | Compress with bzip2 / xz (smaller, slower) |
v | Verbose: print each file |
f name | The archive's file name (put f last, right before the name) |
-C dir | Extract 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/appAlways 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.
| Tool | Extension | Speed | Size | Notes |
|---|---|---|---|---|
| gzip | .gz | Fast | Good | The usual default |
| bzip2 | .bz2 | Slower | Smaller | Older |
| xz | .xz | Slowest | Smallest | Good for software releases |
| zstd | .zst | Very fast | Small | The modern choice, used by many tools |
| zip | .zip | Fast | Good | Best for sharing with Windows users |
gzip app.loggunzip app.log.gzgzip 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.gzrsync: 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/| Option | Meaning |
|---|---|
-a | Archive mode: everything inside, keeping permissions, times and links |
-v | Verbose |
-z | Compress while sending |
-P | Show Progress and allow resuming |
--delete | Delete files at the destination that are gone from the source |
-n | Dry run: show what would happen, change nothing |
--exclude | Skip matching files |
The slash at the end matters:
rsync -a src/ dest/copies what is insidesrcintodest.rsync -a src dest/copies the foldersrcintodest, givingdest/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 localDesigning 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)
| Question | Good 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- Make a date stamp, such as
2026-11-30. - Pack
/etcinto a dated archive. - Copy it off the server, to S3.
- 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 /tmpIf the upgrade breaks something, restoring is one extract and one reload.
Common mistakes
rsync --deletewith 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
- Make a compressed archive of
/etc/nginxcallednginx-backup.tgz. - Extract it into
/tmpand check that the files are there. - Compress and uncompress a log file with gzip.
- Explain the difference between
rsync -a src/ dest/andrsync -a src dest/.
Cheat sheet
| Command | What it does |
|---|---|
tar czf a.tgz dir | Create a gzip archive |
tar tzf a.tgz | List what is inside |
tar xzf a.tgz -C dir | Extract into a folder |
gzip f / gunzip f.gz | Compress / uncompress |
zgrep x f.gz | Search inside a compressed file |
rsync -avn src/ dest/ | Dry-run sync |
scp f host:/path | Copy a file over SSH |
Key takeaways
tarpacks many files into one.tar czfpacks,tar xzfunpacks.gzipmakes files smaller.rsynccopies only what changed. Try it with-nfirst.- 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.