Verify backup content
Compare backed-up file bytes with a trusted SHA-256 manifest and report missing files separately.
A successful job is not a successful restore
A backup job can exit successfully while copying the wrong files or collecting an incomplete dataset. Content verification provides another check: compare the backed-up bytes with a manifest captured from the intended source. That manifest must itself come from a trusted process.
SHA-256 identifies file bytes, including trailing newlines. Hash the file through input redirection rather than capturing its text in a variable first; command substitution removes trailing newlines and would change what is being measured. A hash does not prove application consistency or that a database can be recovered, so restoration still needs testing.
Keep outcomes distinct
A missing file has no observed digest. A present file with a different digest is a mismatch. A present empty file still has a real digest. Report all three outcomes instead of treating an absent file as empty or stopping at the first mismatch.
The browser provides sha256sum; macOS provides shasum with the -a 256 option. A small wrapper chooses the available utility and returns only its digest. Read trusted manifest basenames literally and quote paths so a backup called “team notes.txt” stays one file. The next lesson practises copying verified backups into a separate restore directory.
sha256_file() {
if command -v sha256sum >/dev/null 2>&1; then value=$(sha256sum < "$1") || return 1
else value=$(shasum -a 256 < "$1") || return 1;fi
printf '%s\n' "${value%% *}"
}
printf 'Backup digest: %s\n' "$(sha256_file './backup/settings.conf')"
Put it into practice.
expected.tsv has header file<TAB>sha256 and trusted unique basenames without slash, tab or newline, with valid lowercase hashes. For each entry inspect backup/<file>. Print file<TAB>status<TAB>actual_sha256 in manifest order: ok for a matching regular file, mismatch for different bytes, missing with actual - otherwise. Leave backups unchanged. Support sha256sum in Linux and shasum -a 256 on macOS.
See an explained solution
sha256_file() {
if command -v sha256sum >/dev/null 2>&1; then value=$(sha256sum < "$1") || return 1
else value=$(shasum -a 256 < "$1") || return 1;fi
printf '%s\n' "${value%% *}"
}
printf 'file\tstatus\tactual_sha256\n'
while IFS=$'\t' read -r name expected; do
[ "$name" = file ] && continue
if [ ! -f "./backup/$name" ]; then printf '%s\tmissing\t-\n' "$name";continue;fi
actual=$(sha256_file "./backup/$name") || exit 1
status=mismatch;[ "$actual" != "$expected" ] || status=ok
printf '%s\t%s\t%s\n' "$name" "$status" "$actual"
done < expected.tsv
The wrapper normalises the two platform command names without changing the bytes. Each manifest entry receives a result, so one mismatch does not hide later missing backups and an empty file is still verified correctly.