Every night it said success
A charity's backup software sent a green tick every night for a year. On the day they needed it, the archives would not open.
Nobody had ever tried to restore one. A backup you have not restored is a hope, not a plan.
By the end of this lesson
Note
I can test a restore and measure it against a recovery point objective.
Key words
| Word | What it means | Example |
|---|---|---|
| Restore test | bringing a backup back to prove it works | |
| RPO | the most recent work you can afford to lose | 24 hours |
| RTO | how long you can afford to be down | 8 hours |
| Restore chain | a full backup and the incrementals after it | |
| Integrity | data being exactly as it was saved |
The cells students fill in are left blank in this preview.
Starter
Have a go
Starter
3 questionsTwo numbers decide what good enough means
The RPO is how much recent work the business can afford to lose. Nightly backups give an RPO of about a day.
The RTO is how long it can afford to be down. Both are set by the business, not by IT. A restore test shows whether the backups really meet them.
Worked example: Kestrel Accounts on a Wednesday
| Fact | Value | Against the target |
|---|---|---|
| Last good backup | Tuesday 02:00 | |
| Files locked | Wednesday 14:00 | |
| Work lost | 36 hours | Misses an RPO of 24 hours |
| Service back | Thursday 10:00 | |
| Time down | 20 hours | Meets an RTO of 24 hours |
The cells students fill in are left blank in this preview.
Check
Have a go
Check
3 questionsA restore test checks every file
restoretest.py restores archives in order into an empty folder. A restore chain is the full backup, then each incremental after it.
It then checks every file's hash against the manifest taken when the backup ran. That checks integrity, not just that the archive opened. It also times the restore.
Check lesson 1's scripts are here
cd ~/rr && ls mkshare.py manifest.py drill.py compare.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Build a fresh share and its manifest
rm -rf share && python3 mkshare.py share && python3 manifest.py share > before.txtShown here, not run. Press Start lab to type it into your own lab’s terminal.
Make day 1's full backup
rm -rf backups && mkdir -p backups && tar --listed-incremental=backups/share.snar -czf backups/share-day1-full.tar.gz share && python3 manifest.py share > backups/share-day1.manifest && ls backupsShown here, not run. Press Start lab to type it into your own lab’s terminal.
Make day 2's changes and an incremental backup
python3 mkshare.py share 2 && tar --listed-incremental=backups/share.snar -czf backups/share-day2-incr.tar.gz share && python3 manifest.py share > backups/share-day2.manifestShown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the restore test, part 1
printf '%s\n' 'import hashlib, os, shutil, subprocess, sys, time' 'manifest, archives = sys.argv[1], sys.argv[2:]' 'start = time.monotonic()' 'shutil.rmtree("restore-test", ignore_errors=True)' 'os.mkdir("restore-test")' 'for archive in archives:' ' done = subprocess.run(["tar", "--listed-incremental=/dev/null", "-xzf", os.path.abspath(archive)], cwd="restore-test", capture_output=True)' ' if not done.returncode == 0:' ' print("FAIL: could not read", archive)' ' sys.exit(1)' ' print("restored", archive)' 'want = dict(line.split()[::-1] for line in open(manifest))' 'got = {}' 'for base, dirs, files in os.walk("restore-test/share"):' ' for name in files:' ' path = os.path.join(base, name)' ' got[os.path.relpath(path, "restore-test/share")] = hashlib.sha256(open(path, "rb").read()).hexdigest()' 'right = [p for p in want if got.get(p) == want[p]]' 'wrong = [p for p in want if p in got and not got[p] == want[p]]' > restoretest.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the restore test, part 2
printf '%s\n' 'missing = [p for p in want if p not in got]' 'print("files expected:", len(want), " right:", len(right), " wrong:", len(wrong), " missing:", len(missing))' 'for p in wrong + missing:' ' print(" not as backed up:", p)' 'print("restore took", round(time.monotonic() - start, 1), "seconds")' 'print("PASS" if len(right) == len(want) else "FAIL")' >> restoretest.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Test the whole chain against day 2
python3 restoretest.py backups/share-day2.manifest backups/share-day1-full.tar.gz backups/share-day2-incr.tar.gzShown here, not run. Press Start lab to type it into your own lab’s terminal.
Test the full backup alone against day 2
python3 restoretest.py backups/share-day2.manifest backups/share-day1-full.tar.gzShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsA broken archive fails the test
A disk that fills up, or a job stopped half way, leaves an archive cut short. The backup job may still report success.
Make one on purpose: copy the first 400 bytes of day 2's archive. Then test the chain with it.
Make a cut-short copy of day 2's archive
head -c 400 backups/share-day2-incr.tar.gz > backups/share-day2-bad.tar.gz && ls -l backups/share-day2-*.tar.gzShown here, not run. Press Start lab to type it into your own lab’s terminal.
Test the chain with the broken archive
python3 restoretest.py backups/share-day2.manifest backups/share-day1-full.tar.gz backups/share-day2-bad.tar.gz; echo "exit status $?"Shown here, not run. Press Start lab to type it into your own lab’s terminal.
A common mistake
Warning
Many people think a backup job that reports success can be restored. It only says the job finished. Only a restore, checked file by file, proves the archive is whole. Test on a schedule, and after every change to the backups.
Test on a schedule and keep the result
Run a restore test every month, and a full rehearsal at least once a year. Write down the time it took and the files that failed.
Compare the time with the RTO and the backup times with the RPO. A failed test is an alert, like any other.
Try it: how much work is lost?
backups = [2, 26, 50] # hours since Monday 00:00 when each backup finished
incident = 63
last = max(b for b in backups if b <= incident)
print("lost", incident - last, "hours of work")
print("meets a 24 hour RPO" if incident - last <= 24 else "misses a 24 hour RPO")
# Remove 50 from the list, then press Run.
Make a free account to save your work and track your progress.
Assignment: measure a restore against targets
Times are strings written "YYYY-MM-DD HH:MM".
hours_between(start, end) returns the hours from start to end as a float.
last_good(backups, incident) takes a list of (time, ok) pairs and returns the latest time whose backup is ok and is at or before incident, or None.
report(backups, incident, restored, rpo, rto) returns a dictionary: "lost" (hours from the last good backup to the incident), "down" (hours from the incident to restored) and "misses", a list holding "RPO" when lost is over rpo and "RTO" when down is over rto, in that order. With no good backup, lost is None and "RPO" is missed.
def hours_between(start, end):
return 0.0
def last_good(backups, incident):
return None
def report(backups, incident, restored, rpo, rto):
return {"lost": 0.0, "down": 0.0, "misses": []}
Make a free account to save your work and track your progress.
Exit quiz
Have a go