A Checksum Proves a Copy Is Identical, Not That the Backup Restores

Linux

A checksum is a short fingerprint computed from every byte of a file. Compute it again later, or on a copy, and compare: the same value means the bytes are the same, a different value means something changed. It answers one question only, "is this copy byte-for-byte the original?". Whether the original was any good is a separate question.

How SHA-256 Works as a Checksum

SHA-256 is a cryptographic hash function. It reads any amount of data and outputs 256 bits, written as 64 hexadecimal characters:

$ sha256sum etcd-node1-20261123T171508Z.tar.zst
3f1c…9a0e  etcd-node1-20261123T171508Z.tar.zst

Three properties make it useful:

  • Deterministic: the same bytes always give the same hash, on any machine.
  • Avalanche effect: change one bit anywhere and about half the output bits flip. The new hash looks unrelated to the old one, so there's no "almost matching".
  • Collision-resistant: nobody knows how to find two different files with the same SHA-256 hash, even on purpose. An accidental match is out of reach (2^256 possible values).

Older checksums like CRC32 (used by ZIP, Ethernet) or MD5 also catch accidents, but CRC32 is only 32 bits and both can be forged deliberately. SHA-256 is the usual choice today.

The .sha256 File

sha256sum name > name.sha256 stores the hash and the file name on one line: 64 hex characters, two spaces, the name, a newline. For my bundles that's about 100 bytes, the small file next to each bundle in ls -l.

sha256sum -c name.sha256 reads that line, recomputes the hash of the named file, and prints name: OK or name: FAILED (exit code 1). The name in the file is relative, so run it from the folder that holds the bundle (cd "$dir" && sha256sum -c …).

Where My etcd Backup Checks It

  1. On the node, right after building the bundle: sha256sum writes <bundle>.sha256.
  2. On TrueNAS, after the copy: the script runs sha256sum -c in the NAS folder and logs remote: … (checksum OK). That catches an NFS write cut short by a network drop or a full dataset. One caveat: the read right after the write can come from the node's own NFS cache, so it proves the bytes got through the client, not that the NAS disk stored them. ZFS's own checksums and scrubs cover the disk side.
  3. On the Mac, after make etcd-pull copies it over ssh: …tar.zst: OK proves the transfer.

Each check compares against the hash computed once, at the source, before the file traveled.

What a Checksum Doesn't Prove

  • That the backup restores. A snapshot taken from a broken etcd, or a bundle missing the PKI, checksums perfectly. Only a restore proves a backup: etcd-restore-drill loads the snapshot into a throwaway etcd and compares key counts with the live cluster (see An etcd Snapshot Alone Can't Rebuild a kubeadm Control Plane, So Back Up the PKI with It). The etcd snapshot also carries its own integrity hash, which etcdutl snapshot restore checks: that one is about the database inside, the .sha256 is about the file around it.
  • That nobody tampered with it. The hash sits next to the file. Anyone who can rewrite the bundle can rewrite the .sha256 too. Protection against tampering needs a secret: an HMAC (a hash mixed with a key), or a signature with a private key (GPG, minisign), or at least the hash stored somewhere the attacker can't write. Checksums protect against accidents, not attackers.
  • That the copy is complete if the hash file is missing. No .sha256, no check. A backup job should treat a missing hash as a failure.

References

Review cards

Flashcard What does `remote: … (checksum OK)` prove about an etcd backup, and what doesn't it prove?

The NAS copy is byte-for-byte identical to the bundle built on the node. It doesn't prove the snapshot inside restores: only a restore drill does.

Flashcard Why doesn't a `.sha256` file next to a bundle protect against tampering?

Whoever can rewrite the bundle can rewrite the hash next to it. Tamper protection needs a secret: an HMAC key, a signature, or the hash stored out of the attacker's reach.

Quiz One bit of a 670 KB bundle flips during a copy. What happens to its SHA-256 hash?
  • One hex character changes
  • Nothing, one bit is below the hash's precision
  • About half the output bits change, so the hash looks unrelated
  • The hash only changes if the bit is in the snapshot

The avalanche effect: any change to the input scrambles the whole output, so sha256sum -c reports FAILED.

Flashcard Why does `sha256sum -c name.sha256` need to run from the folder that holds the file?

The .sha256 line stores the file name as written when it was created, here a relative name, so -c looks for it in the current directory.