Documentation
¶
Overview ¶
Package backup copies the state hlab keeps on a machine back to where hlab runs, and puts it back again.
What it protects is small and irreplaceable: a DNS zone, a DHCP lease database, service credentials, printer configuration. What it deliberately does not protect is bulk data mounted from elsewhere on the machine — a media library is large, is not hlab's, and would turn a cheap safety net into something nobody runs.
Index ¶
Constants ¶
const ( MarkerBegin = "##HLAB#BACKUP#" MarkerEnd = "##HLAB#ENDBACKUP#" MarkerNothing = "hlab:nothing-to-back-up" MarkerRestored = "hlab:restored" )
Markers delimiting the archive in the script's output.
const RemoteDir = "/opt/hlab/stacks"
RemoteDir is the directory backed up on each machine.
const StageCommand = "sh -c 'cat > /tmp/hlab-restore.b64'"
StageCommand is the command that receives a base64 archive on stdin.
The data travels as the command's input rather than inside the script, because a script carrying twenty megabytes of payload is awkward to quote and impossible to read when something goes wrong.
Variables ¶
var ErrNothing = errors.New("nothing to back up")
ErrNothing means the machine has no hlab-managed state. That is an ordinary answer, not a failure: most machines in a fleet run no stacks, and treating them as errors would make a healthy run exit non-zero and be useless to anything scheduled.
var Excluded = []string{
"*.log", "*.log.*", "logs",
"blocklists", "stats", "cache.bin", ".cache", "deps", "tts",
"*.db", "*.db-wal", "*.db-shm",
"*.sqlite", "*.sqlite-wal", "*.sqlite-shm",
}
Excluded holds the patterns left out of an archive.
The point of this backup is rebuilding a machine, not preserving its history. Everything here is either regenerated on first run, or is live state that should not be restored at all:
- Logs and caches come back on their own and dominate the archive — unfiltered, a fleet of a few megabytes of configuration was carried as fourteen megabytes of DNS blocklists and printer agent logs, plus several hundred downloaded UI icons.
- A database copied while its service is running is the dangerous one. The file plus its write-ahead log is an inconsistent snapshot, and restoring it can produce a corrupt database rather than an old one. Home Assistant's recorder holds history, which is data rather than configuration, and is excluded for both reasons.
Functions ¶
func BuildArchiveScript ¶
func BuildArchiveScript() string
BuildArchiveScript renders the script that produces a backup.
func BuildRestoreScript ¶
BuildRestoreScript renders the script that puts a backup back.
Types ¶
type Archive ¶
Archive is one machine's backup, already verified.
func ParseArchive ¶
ParseArchive extracts and verifies the archive from a machine's output.
Both the checksum and the length are checked, against the values the machine computed on the file it actually wrote. That is the whole point: a truncated transfer produces a tar that looks plausible and fails only when somebody is trying to restore from it, which is the worst possible moment to find out.