My backups failed for 79 days and nothing told me
The backup script on my main lab host failed every night from May 1 to July 19. The cleanup that would have fixed it sat below the line that kept failing, and the retention policy could never have fit on the disk.
Here is a sketch of the script that backed up my main lab host, with everything removed except the lines that matter.
#!/bin/bash
set -euo pipefail
for guest in $GUESTS; do
run_backup "$guest"
if [ $? -eq 0 ]; then
record_success "$guest"
fi
done
prune_old_backups
It ran from cron at 2 a.m. every day. From May 1 to July 19 it failed every time. For 79 days that host took no backups, and I found out on day 79.
May 1: no backup. Night 1 of 79.
One square per night. The job ran at 2 a.m., and for 79 days it failed.
Both bugs are visible in the sketch if you know what the backup volume looked like.
What the script did on a full disk
The backups went to a dedicated volume. When that volume was full, the backup command for the first guest failed and returned a non-zero exit status.
The second line of the script is set -euo pipefail. The -e in it is the errexit option. The Bash manual's description is that the shell exits immediately when a command returns a non-zero status, with a list of exceptions. A failed backup in the middle of a loop is not one of the exceptions. So the script stopped on that line.
Pruning old backups is the only step in the script that frees space, and it was the last line. On a night when the backup failed, the script never reached it. The volume stayed full. The next night the same command failed on the same line.
The cleanup could only run on nights when the backup had succeeded, and those were the nights when it was least needed. Once the volume filled, the script had no path back.
The if after the backup is the other thing to notice. It reads like error handling. Under -e it does nothing. If the backup fails, the shell has already exited before the test runs. If the shell reaches the test, the status is zero every time. I had a line in the script that looked like a check for failure and could never observe one.
There is a long-running FAQ entry in the Bash community about this option, titled "Why doesn't set -e (or set -o errexit, or trap ERR) do what I expected?" It describes the rules for when the shell exits as "extremely convoluted," and its maintainer's advice is blunt: "don't use set -e. Add your own error checking instead." I would not go that far for every script. In a short one where any failure should stop everything, the option does what I want. A backup script has a duty that continues after a failure, and errexit handles that case badly.
Why the disk was full
The second bug is arithmetic I never did.
Retention was set to keep 14 archives for each guest. The largest guest is the container that runs my assistant. Each of its archives was 46 GB, and 34 GB of that was language model files. Those are the models I wrote about deleting three weeks ago. Fourteen copies of that one guest came to about 645 GB.
All the guests together, at 14 copies each, came to about 916 GB. The backup volume holds 916 GB.
So the policy asked the volume to hold exactly as much as it could, with nothing left over for the archive being written. The volume was always going to fill. May 1 happened to be the night it did.
The 34 GB of model files deserve their own sentence. They can be downloaded again at any time. I was keeping fourteen copies of them in the one place that existed to protect things I could not replace.
Why nothing told me
The script recorded its results in a database. No alert was attached to that record, and I was not reading it.
A backup is an easy thing to lose track of, because nothing consumes one until the day you need a restore. A web server that stops gets noticed by its users. A backup job that stops has no users.
I have written some version of that several times this year. In May, two of my machines lost power for two days and nothing alerted me, because every monitor ran on the machines that were down. That outage began ten days after the backups stopped. Everything came back by itself when the power returned. If a disk had failed to come back, the newest backup of every guest on that host would have predated May.
The rewrite
I replaced the script on July 19 and kept the old one beside it.
Prune first. The new script frees space before it uses any.
No errexit. It keeps -u and pipefail and drops -e. It checks the result of each backup itself.
Keep 7 per guest. Half the old number, and this time I multiplied it out against the volume.
Leave out what can be downloaded again. The model directory is excluded from the assistant container's backup.
Tell me when it fails. A failed backup now sends a message to my phone through the same chat app my assistant uses.
By August 10 the backups for that host took 323 GB of the 916.
Two things are still wrong, and I would rather say so than leave the impression that the rewrite finished the job.
The alert runs on the host it reports on. It covers a backup that ran and failed. It cannot cover a backup that never ran. Earlier this month that host was down for eight days. No backups were taken, and the new alert was silent the whole time, because a script that is not running has nothing to report. In July I built a monitor that runs on my laptop and checks the lab from outside the hosts. During those eight days it was not running either.
The main host's backups also still exist in exactly one place, on a second drive inside the same machine. I said in April that a copy has to live somewhere else, and I said it again in July. One part of that has moved. The third machine, which I found in July with no backups of any kind, has had a nightly job since August 10, and its archives are copied to the main host each night. The copy in the other direction does not exist yet.
The check that would have caught it
A check on the result would have caught this on the first night, and it does not need to know anything about how the script works.
For each guest, find the date of the newest backup archive. If it is more than two days old, alert. That is the whole check. It fires when the script fails, when the script is never started, when the disk is full and when someone disables the job and forgets. It should run somewhere other than the host being backed up.
Then multiply the retention policy out. Copies kept, times the size of each guest's archive, summed across guests, compared with the size of the volume, with room left for the one being written. If the product is close to the capacity, the policy does not fit.
At work the backup product has a dashboard that says the jobs succeeded. I would still ask the two questions separately. Who gets paged when the newest restore point for a system is older than its policy allows? And who last multiplied the retention policy against the repository?
One caveat on all of it. An archive that exists and is recent is evidence that a backup ran. Only a restore proves that it worked, and nothing in my rewrite tests one.
Sources
Views are my own.