$_ CLI In Depth
Learning progress 0%
Build · Disks, Mounts & Archives

Build: An Incremental Backup with tar

Make a full archive, an incremental archive that holds only changes, and restore the chain.

~40 min Intermediate 3 checked steps

Build it on a real Linux machine

The guided lab boots Linux in your browser. Each step runs a check script on that machine, so it passes only when your files and output are right.

What you will build

Step 1: Make the full backup

Create a directory named notes with two files, a.txt and b.txt. Make a full archive named full.tar with a new snapshot file named snap.snar. The snapshot file is what makes the next run incremental.

BASH4 lines
mkdir -p notes
echo first > notes/a.txt
echo second > notes/b.txt
tar --listed-incremental=snap.snar -cf full.tar notes

Step 2: Make an incremental backup

Wait one second, then change a.txt and create c.txt. Run tar with the same snapshot file to make incr.tar. The archive should hold a.txt and c.txt, but not b.txt, which did not change.

BASH5 lines
sleep 1
echo more >> notes/a.txt
echo third > notes/c.txt
tar --listed-incremental=snap.snar -cf incr.tar notes
tar -tf incr.tar

Step 3: Restore the chain in order

Extract full.tar and then incr.tar into an empty directory named restore. Compare the restored notes with the current notes using diff -r.

BASH4 lines
mkdir -p restore
tar -xf full.tar -C restore
tar -xf incr.tar -C restore
diff -r notes restore/notes && echo restored

Files

  • walkthrough.sh: every step of the reference solution, in order.

How it is checked

Each step in the guided lab has a check script that runs on the lab machine after you finish. It passes only when the files and output it expects are there, so the lab cannot be completed by reading alone.

Reference files

Read these after you have tried the lab. They are the solutions the checks were verified against.

walkthrough.sh

BASH11 lines
#!/usr/bin/env bash
# Reference solution for every step of the guided lab, in order.

# Step 1: Make the full backup
cd /workspace && mkdir -p notes && echo first > notes/a.txt && echo second > notes/b.txt && tar --listed-incremental=snap.snar -cf full.tar notes

# Step 2: Make an incremental backup
cd /workspace && sleep 1 && echo more >> notes/a.txt && echo third > notes/c.txt && tar --listed-incremental=snap.snar -cf incr.tar notes

# Step 3: Restore the chain in order
cd /workspace && mkdir -p restore && tar -xf full.tar -C restore && tar -xf incr.tar -C restore

Lessons this build uses