Skip to content

Backups

Practical 4.6 backups-annex.pdf · PDF

Think of one thing on your phone or laptop you would hate to lose. Your photos, a project, a game save. Now count: how many copies of it exist, and where? If the answer is "one, right here", it is not a question of if you lose it. It is a question of when.

In RAID is not a backup you learned what a backup must be: a copy from another moment, in another place, that you have already restored. Today you make one with your own hands for Tramuntana Apps, break the original on purpose, and get it back.

What you will learn

  • State the 3-2-1 rule and check a backup plan against it
  • Make a full backup of a folder with tar, list it, and restore one folder or one file
  • Tell full, incremental and differential backups apart, and say which archives a restore needs
  • Mirror a folder with rsync, and predict what --delete will do with --dry-run
  • Prove a restore with diff -rq
  • Why a mirror is not a history

The 3-2-1 rule

In 2005 a photographer, Peter Krogh, wrote a book about keeping digital photos safe for a whole career: The DAM Book (DAM for Digital Asset Management, nothing to do with your course). In it he gave a rule that fits on one hand, and system administrators adopted it:

Rule Because
3 Three copies of the data: the original and two more One copy can be damaged the same day as the original
2 On two different kinds of storage Two disks of the same model, bought together, tend to die together
1 One copy in another place Fire, flood and thieves take everything in the room

For Tramuntana Apps:

flowchart LR
    subgraph OFFICE["In the office"]
        C1["<b>Copy 1</b><br>~/tramuntana-share<br>the original, system disk"]
        C2["<b>Copy 2</b><br>another disk<br>mounted on /mnt/backup"]
    end
    C3["<b>Copy 3</b><br>outside the office<br>needs the network (Units 3 and 6)"]
    C1 -->|"today: tar and rsync"| C2
    C1 -.->|"later in the course"| C3

An honest note about the lab

In deb-lab both disks are files on the same PC. The commands are the real ones; the safety is not.

Prepare the backup disk

You reuse one of the empty 1 GiB disks from LVM and RAID. This is all from the last few days: partition, file system, mount point, mount.

lsblk
sudo parted -s /dev/vdc mklabel gpt \
  mkpart backup ext4 1MiB 100%
sudo mkfs.ext4 -L backup /dev/vdc1
sudo mkdir /mnt/backup
sudo mount /dev/vdc1 /mnt/backup
sudo chown $USER: /mnt/backup
df -h /mnt/backup

If mkfs asks Proceed anyway? (y,N), it found the old file system from the LVM practical on this disk: answer y. Then df -h /mnt/backup must show your new partition (/dev/vdc1, about 988M), not the system disk. If it shows the system disk, your "backup" is on the same disk as the original.

/mnt/backup is deliberately not in /etc/fstab: it plays the role of an external disk you connect to make the copy.

A full backup with tar

tar (from tape archive) packs a whole folder into one file. Three letters do almost everything:

tar -czf ARCHIVE FOLDER     # c = create   z = compress with gzip   f = into this file
tar -tzf ARCHIVE            # t = list what is inside
tar -xzf ARCHIVE WHAT       # x = extract (everything, or only WHAT)

Make the backup and look inside it:

cd ~
ls -lh tramuntana-share/design/old
tar -czf /mnt/backup/wed-full.tgz tramuntana-share
ls -lh /mnt/backup
tar -tzf /mnt/backup/wed-full.tgz

The fake promo-video.mp4 is 20M, and yet the archive of the whole folder is about 25K. Is it incomplete? No: tar -tzf lists every file. That video is 20 MB of nothing but zeros, the most repetitive data there is, and the rest is short text. Repetitive data shrinks enormously; real video and photos hardly shrink at all.

Careful: tar does not compress

tar only packs many files into one. The z asks gzip to compress the pack. A .tgz (or .tar.gz) is both.

Disaster and restore

Pau cleans the wrong folder:

rm -r tramuntana-share/admin
ls tramuntana-share

Aina calls you. Bring back only her folder from the archive:

tar -xzf /mnt/backup/wed-full.tgz \
  tramuntana-share/admin
ls tramuntana-share/admin

invoices, staff.csv and wifi.txt are back. Aina was without her folder for about a minute, because the copy was on another disk and you knew the command. If you mistype the path, tar says Not found in archive and extracts nothing.

You are not safe when you make the backup

You are safe when you have restored from it once. Before that, you had a file that you hoped was a backup.

Full, incremental, differential

Your full backup is tiny. The real projects folder of a studio is 100 GB. A full copy every night means 700 GB a week, and hours of copying, to save the two files that changed. There are smarter ways.

Think of a diary. A full backup is the whole diary photocopied. An incremental backup is "what happened today". A differential backup is "everything that happened since Sunday", rewritten every night.

Full Incremental Differential
Copies Everything, every time What changed since the last backup (of any kind) What changed since the last full
Size each night Always big Small Grows day by day
To restore you need The last one The full and every incremental after it, in order The full and the last differential
Weak point Space and time One broken link breaks the chain Bigger every day until the next full

Work it out with numbers. Full backup on Sunday night: 100 GB. Every working day, 2 GB of files are new or changed (different files each day).

Night of Mon Tue Wed Thu Disk dies Friday morning: restore...
Incremental 2 GB 2 GB 2 GB 2 GB Sunday + Mon + Tue + Wed + Thu: 5 archives
Differential 2 GB 4 GB 6 GB 8 GB Sunday + Thu: 2 archives

Now Wednesday's archive cannot be read. With incrementals you can only get back to Tuesday night: the chain is broken, and Thursday's archive alone is not enough. With differentials there is no problem: Thursday's archive holds everything since Sunday.

The sentence to remember

Incrementals are cheap to make and delicate to restore. Differentials are the opposite.

tar can make incrementals with a small notebook file (-g) where it writes what it already saved:

tar -czf /mnt/backup/mon-full.tgz -g /mnt/backup/week.snar tramuntana-share
echo 'Invoice 2026-006' > tramuntana-share/admin/invoices/2026/invoice-2026-006.txt
tar -czf /mnt/backup/tue-incr.tgz -g /mnt/backup/week.snar tramuntana-share
tar -tzf /mnt/backup/tue-incr.tgz

The full archive is about 25,800 bytes; the incremental is about 785, and its list shows the folders and only one file: the new invoice.

rsync: copy only what changed

In 1996, in Australia, Andrew Tridgell and Paul Mackerras announced rsync. The idea: before copying, compare both sides and send only the differences. Tridgell is also the author of Samba, the file server you will install later in the course.

sudo apt install rsync
rsync -av tramuntana-share/ /mnt/backup/mirror/
rsync -av tramuntana-share/ /mnt/backup/mirror/

-a (archive) copies folders inside folders, with dates and owners; -v (verbose) says what it does. The first run lists every file. The second run lists no files at all: nothing changed, so almost nothing is sent.

Change two files and sync again:

cd ~/tramuntana-share
echo 'new client' > admin/client-new.txt
echo 'v2' > design/logos/icon.svg
cd ~
rsync -av tramuntana-share/ /mnt/backup/mirror/

Only admin/client-new.txt and design/logos/icon.svg travel.

Trap 1: the slash

tramuntana-share/ with a slash means "the content of the folder". Without the slash, rsync copies the folder itself, and you get mirror/tramuntana-share/....

--delete, and why you look first

--delete makes the copy identical: files that no longer exist in the original are deleted in the copy too. Always look first with --dry-run, which shows what would happen and does nothing.

rm tramuntana-share/admin/wifi.txt
rsync -av --delete --dry-run \
  tramuntana-share/ /mnt/backup/mirror/
deleting admin/wifi.txt
...
(DRY RUN)

ls /mnt/backup/mirror/admin still shows wifi.txt: nothing was done. Run it without --dry-run and wifi.txt is gone from the mirror too. Where can you get it back? From the old tar archive:

tar -xzf /mnt/backup/wed-full.tgz \
  tramuntana-share/admin/wifi.txt

Careful: a mirror is not a backup

A mirror has one moment: now. Delete a file, sync, and it is gone on both sides. It is the RAID of LVM and RAID, only slower. A backup needs old versions too.

Prove it

A restore you have not checked is a guess. Extract the whole archive into an empty folder (-C means "extract into that folder") and compare it with the live data:

mkdir ~/rtest
tar -xzf /mnt/backup/wed-full.tgz -C ~/rtest
diff -rq ~/rtest/tramuntana-share tramuntana-share

diff -rq compares two folders recursively (-r) and only says which files differ (-q, quiet):

Only in tramuntana-share/admin: client-new.txt
Files /home/student/rtest/tramuntana-share/design/logos/icon.svg and tramuntana-share/design/logos/icon.svg differ

That is exactly the work done after the backup. If the system disk died now, Tramuntana would lose the new client and the new logo.

So which is the good backup? The mirror lost wifi.txt. The old archive does not have the new client. Neither alone: you want new and old at once, without storing 100 GB every night. That is the tool of the next page. All the steps, with space for your evidence, are on the Practical 4.6 sheet.

Key takeaways

  • The 3-2-1 rule: three copies, on two kinds of storage, one in another place.
  • tar -czf creates a compressed archive, tar -tzf lists it, tar -xzf extracts all of it or only one path.
  • Incremental = since the last backup of any kind (small, delicate chain). Differential = since the last full (grows, needs only two archives).
  • rsync copies only what changed; --delete makes the copy identical, so check first with --dry-run.
  • A mirror has one moment in time: it repeats your deletions. A backup keeps old versions.
  • diff -rq proves a restore by listing every difference with the live data.

Quick check

1. What does the 3-2-1 rule ask for?
  • 3 copies, 2 kinds of storage, 1 copy in another place
  • 3 disks in RAID, 2 partitions, 1 file system
  • 3 backups a day, 2 a week, 1 every month
  • 3 passwords, 2 users, 1 administrator account

The 3-2-1 rule (Peter Krogh, 2005): the original plus two copies, on two different kinds of storage, with one copy off-site so that a fire or a thief cannot take everything.

2. What does an incremental backup copy?
  • What changed since the last backup of any kind
  • What changed since the last full backup only
  • Everything in the folder, every single time
  • Only the files you choose by hand each night

An incremental backup is "what happened today": the changes since the previous backup, whatever its type. Changes since the last full is the definition of a differential.

3. What does tar -tzf full.tgz do?
  • Lists what is inside the archive
  • Extracts the archive into the current folder
  • Creates the archive from the current folder
  • Tests the disk where the archive is stored

t = list, z = it is gzip-compressed, f = this file. Listing is the quick way to check that an archive holds what you think, before you need it.

4. Full backup on Sunday, a differential every night. The disk dies on Thursday morning. Which archives do you need?
  • Sunday's full and Wednesday's differential
  • Only Wednesday's differential archive
  • Sunday's full and Monday, Tuesday and Wednesday
  • Every archive made during the whole month

Each differential holds everything changed since the last full, so the latest one is enough on top of the full. Monday-to-Wednesday in order is what an incremental chain would need.

5. You delete a file in the original and run rsync -av --delete to the mirror. What happens to the file in the mirror?
  • It is deleted there too
  • It stays there for ever
  • It is renamed to keep it
  • It is moved to a trash folder

--delete makes the copy identical to the original, so a file gone from the original disappears from the mirror. That is why you check with --dry-run first, and why a mirror is not a backup.

6. The archive of a 20 MB folder is only 25 KB. What is the most likely explanation?
  • The files are very repetitive, so gzip shrank them enormously
  • tar skipped every file that was bigger than 1 MB
  • The archive only lists file names, not their content
  • The backup disk compresses everything written to it

gzip describes repetition in fewer bytes, and 20 MB of zeros is pure repetition. Check with tar -tzf: every file is listed. Real video or photos would barely shrink.

7. After a restore into ~/rtest, diff -rq prints one line: Only in tramuntana-share/admin: client-new.txt. Select the two that are true.
  • The file was created after the backup was made
  • If the original were lost now, that file would be lost
  • The restore failed and the archive is damaged
  • The backup holds a file that the original does not

diff -rq lists the differences between the restored copy and the live data. Only in tramuntana-share means the file exists only in the live folder: it is newer than the backup, so no backup has it yet.

You had to choose between a fresh mirror with no past and an old archive with no present. Next, one program that gives you both, in the space of one: restic and the restore drill.