Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .sops.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
# SOPS encryption policy.
#
# Everything under secrets/ matching *.sops.yaml is encrypted with the age
# recipient below, and so is every firewall backup under backups/firewall/.
# recipient below, and so is every firewall backup under backups/firewall/
# and every switch backup under backups/switch/.
#
# Those two live in one rule rather than two so the recipient is written once.
# They live in one rule rather than several so the recipient is written once.
# A second rule would be a second copy of the key, and rotating one and not the
# other is the kind of drift this repository keeps finding.
#
Expand Down Expand Up @@ -171,7 +172,7 @@ creation_rules:
# fenrir a token, so its rule went in, with a placeholder for fenrir's key,
# before secrets/sensor.sops.yaml could exist.

- path_regex: (secrets/.*|backups/firewall/.*)\.sops\.ya?ml$
- path_regex: (secrets/.*|backups/firewall/.*|backups/switch/.*)\.sops\.ya?ml$
age: >-
age1yrdu996u5mhdh0qf93l7s8zz8stneqnqxpncrcarrmgxvsy264rqmkcs6x,
age19mkg76v0x70wkqwuykxxqpdwrq8mhgklpjw44j6xw73psnhwyvcqa9995j
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,14 @@ backup-firewall: ## Pull morpheus's pfSense config, encrypt it to ./backups/, co
@# backup-volumes.sh already owns KEEP there.
./scripts/backup-firewall.sh $(ARGS)

.PHONY: backup-switch
backup-switch: ## Pull neo's RouterOS /export, encrypt it to ./backups/switch/, copy it to oracle
@# backup-firewall.sh with BACKUP_DEVICE=switch: the same encrypt, verify,
@# copy and retention, for the switch. Its knobs are SW_* (SW_KEEP,
@# SW_OFFHOST), never FW_*, so an override meant for the firewall in
@# /etc/default/homelab-timers does not move the switch's exports.
BACKUP_DEVICE=switch ./scripts/backup-firewall.sh $(ARGS)

.PHONY: backup
backup: ## Quiesce the stack, archive its volumes to ./backups/, verify, copy to oracle
@# Thin on purpose. This target used to BE the implementation, and every
Expand Down
13 changes: 13 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,19 @@ docstring gives: it is a record, not a claim about now.
generator was changed. `snmp.yaml` gains the two GETs, and `ups.test.yaml`
gives each rule a firing case and a quiet one. The alert rules now
number 206: 178 in Prometheus and 28 in Loki.
- **`neo`'s configuration is now backed up nightly.** Until today it lived
only on the switch, which a re-check after the switch audit found; a dead
`neo` meant rebuilding it by hand from `network.md` and the wiki's port
map. `make backup-switch` is `backup-firewall.sh` with
`BACKUP_DEVICE=switch`: it logs in as a new user `backup` (group `read,ssh`,
from `10.7.7.1` only, `prometheus`'s key) through morpheus, takes
`/export`, encrypts it to the firewall export's recipients, verifies it and
copies it to `oracle`, keeping thirty a side. `homelab-backup-switch.timer`
runs it at 09:45. The export carries no password or key, and the restore in
[`swap-the-switch.md`](runbooks/swap-the-switch.md) lists the four things
to put back by hand. It is not carried on the offsite medium; a fire that
takes `prometheus` and `oracle` takes the switch too, and its rebuild is
documented.
- **`UpsOnBattery` now pages on a short transfer.** On 2026-11-21 the UPS
ran on battery for 82 seconds, for distorted input, and nothing paged. The
rule was `upsSecondsOnBattery > 0` for 30 seconds, which needs two scrapes in
Expand Down
9 changes: 8 additions & 1 deletion docs/network.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,13 @@ truth for what a box actually does.
[ADR-0127](adr/0127-relay-neos-log-through-morpheus.md) records why, and
[`ship-firewall-logs.md`](runbooks/ship-firewall-logs.md) §10 is the
procedure.
- The configuration leaves the switch too. A user `backup`, in a group
with `read` and `ssh` and nothing else, limited to `10.7.7.1` and
logging in by `prometheus`'s key only, is what `make backup-switch`
runs `/export` as, nightly at 09:45, jumped through morpheus. The
export is encrypted like the firewall's and copied to `oracle`; it
leaves out users, certificates and the SNMPv3 passphrases.
[`swap-the-switch.md`](runbooks/swap-the-switch.md) has the restore.
- DHCP disabled.
- **`morpheus` pins thirteen reservations in its ARP table, on every
segment but Skids and Degens.** Each is a Kea reservation with *Create a
Expand Down Expand Up @@ -353,7 +360,7 @@ listed under [Hicks](#hicks--vlan-50--trusted), and nothing else.
- `oracle` runs the Lemmiwinks wiki and its Postgres — it has since 2025-11-12,
and [ADR-0011](adr/0011-keep-the-wiki-internal.md) depends on it — and holds
the off-host copies of the firewall export that `make backup-firewall` pushes
to it, of the weekly volume sets that `make backup` pushes
to it, of `neo`'s export that `make backup-switch` pushes, of the weekly volume sets that `make backup` pushes
([#535](https://github.com/Gerrrt/HomeLab/issues/535)), of Jellyfin's
state that `make backup-nas` pulls off `smaug` and pushes on
([ADR-0045](adr/0045-pull-jellyfins-state-from-a-snapshot-over-ssh.md)), and
Expand Down
13 changes: 8 additions & 5 deletions docs/runbooks/schedule-maintenance.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,19 @@ there is one copy of record
([ADR-0048](../adr/0048-carry-the-estates-backup-sets-with-the-second-recipient.md),
[`copy-the-backups-offsite.md`](copy-the-backups-offsite.md)).

Two jobs' output leaves this host. `backup-firewall` copies every export to
Three jobs' output leaves this host. `backup-firewall` copies every export to
`oracle` and **fails if it cannot**, so its `ScheduledJobFailed` also means "the
config has stopped leaving `prometheus`" — a file that never left is a failed
run, not a partial success. `backup-volumes` does the same with every complete
set, weekly, after the stack is back up
([#535](https://github.com/Gerrrt/HomeLab/issues/535)), and `verify-backups`
([#535](https://github.com/Gerrrt/HomeLab/issues/535)). `backup-switch` is
`backup-firewall` for `neo`, the same script with `BACKUP_DEVICE=switch`, and
keeps `SW_KEEP` exports, default thirty. And `verify-backups`
asks `oracle` to hash what it holds every morning, so a copy that has stopped
existing is that job's failure within a day. No rule names either job; the
generic pair above carries both. The same runs bound what accumulates on each
existing is that job's failure within a day. No rule names any of the three;
the generic pair above carries them all. The same runs bound what accumulates on each
side — `FW_KEEP`, default thirty, for the exports; `KEEP`, default seven, for
the sets, on both sides — so neither job can fill either disk;
the sets, on both sides — so none of them can fill either disk;
[`restore-the-firewall.md`](restore-the-firewall.md) §0 and
[`restore-the-stack.md`](restore-the-stack.md) §0 have the windows and how to
change them. Both copies ride on one key exchange between the two laptops, in
Expand Down Expand Up @@ -102,6 +104,7 @@ the host.
| `backup-wiki` | `make backup-wiki` | daily 04:45 | 2 days |
| `verify-backups` | `make verify-backups` | daily 05:30 | 3 days |
| `backup-firewall` | `make backup-firewall` | daily 04:30 | 3 days |
| `backup-switch` | `make backup-switch` | daily 09:45 | 3 days |
| `snmp-verify` | `make snmp-verify` | Wednesdays 06:30 | 14 days |
| `check-versions` | `make check-versions` | Wednesdays 06:45 | 14 days |
| `dashboards-drift` | `make dashboards-export ARGS=--check` | daily 07:30 | 2 days |
Expand Down
63 changes: 63 additions & 0 deletions docs/runbooks/swap-the-switch.md
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,69 @@ community. Then close

---

## Restoring neo from its nightly export

`make backup-switch` takes neo's `/export` every night at 09:45, encrypts it to
the same recipients as the firewall's export, and keeps thirty here and on
`oracle`. To list them, on both sides:

```bash
make backup-switch ARGS=--list
```

If `prometheus` is gone, fetch the newest from `oracle` into a checkout on a
machine that holds an age key:

```bash
ssh atropos@10.0.99.30 ls -1r backups/switch
mkdir -p backups/switch
scp atropos@10.0.99.30:backups/switch/config-<STAMP>.sops.yaml backups/switch/
Comment thread
Gerrrt marked this conversation as resolved.
Resolved
```

Decrypt it to RAM, not to disk, readable by you alone:

```bash
(umask 077; sops --decrypt --input-type yaml --output-type binary backups/switch/config-<STAMP>.sops.yaml > /dev/shm/neo.rsc)
```

Bring the switch to the state §1.3 leaves it in: RouterOS 7 long-term and
`reset-configuration no-defaults=yes`. Import the leaf certificate and its
key first, under the name `neo.matrix.elysium`, as §1.4 does: the export's
`www-ssl` and `api-ssl` lines name it, and fail to import without it. Then
upload `neo.rsc` through WinBox's *Files*, and import it:

```text
/import file-name=neo.rsc
```

The import turns VLAN filtering on part-way through, so a WinBox session by MAC
may drop. The import carries on. Reconnect from the switch LAN at `10.7.7.2`,
check the import reported `Script file loaded and executed successfully`
(in `/log/print` if the session dropped),
then delete `/dev/shm/neo.rsc` on the workstation.

The export leaves out three more things. Put them back by hand:

- **Users.** The groups come back with the import, the users do not. Add
`gerrrt` with the address list from [`network.md`](../network.md), and
`homeassistant` as
[`connect-the-switch-to-home-assistant.md`](connect-the-switch-to-home-assistant.md)
§3 sets it. Add `backup` with address `10.7.7.1/32` and `prometheus`'s public
key (`/user/ssh-keys/add user=backup key="<~/.ssh/id_ed25519.pub>"`,
which RouterOS 7.24 takes; on a release that refuses it, upload the file and
use `/user/ssh-keys/import`). The reset brings the default `admin` back,
enabled: once `gerrrt` logs in, run `/user/disable admin`.
- **The SNMPv3 passphrases.** The `prometheus` community comes back without
them. Set the values the repository's secrets already hold, the way §1.5
and [`rotate-snmp-community.md`](rotate-snmp-community.md) §4 set them, not
fresh ones, then run `make snmp-verify`.
- **Device-mode.** It is not configuration, so it is not in the export. If
`/system/device-mode/print` shows the features on, set it again (§1.3). It
needs the reset button.

Then run `make backup-switch` once by hand, which proves the `backup` login
end to end.

## Also required

- The MokerLink leaves the estate carrying a community that cannot be deleted,
Expand Down
2 changes: 1 addition & 1 deletion scripts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ and only its fixtures run in CI, through `self-tests.sh`.
| --- | --- | --- |
| `backup-volumes.sh` | `backup` | Quiesces the observability stack, archives, encrypts and verifies its volumes, and copies them to `oracle` |
| `restore-volumes.sh` | `restore` | Puts a backup set back |
| `backup-firewall.sh` | `backup-firewall` | `morpheus`'s pfSense config, encrypted at rest and copied off-host |
| `backup-firewall.sh` | `backup-firewall`, `backup-switch` | `morpheus`'s pfSense config, and `neo`'s RouterOS export with `BACKUP_DEVICE=switch`, each encrypted at rest and copied off-host |
| `backup-nas.sh` | `backup-nas` | The media tier's state, pulled from `smaug`'s newest snapshot ([ADR-0045](../docs/adr/0045-pull-jellyfins-state-from-a-snapshot-over-ssh.md)) |
| `backup-wiki.sh` | `backup-wiki` | A dump of the wiki's database, pulled off `oracle` ([ADR-0065](../docs/adr/0065-pull-the-wikis-database-to-prometheus-as-a-dump.md)) |
| `backup-library.sh` | `backup-library` | Immich's library, archived on `trinity` and copied to `oracle` ([ADR-0064](../docs/adr/0064-copy-immichs-library-to-oracle-until-the-off-estate-copy-exists.md)) |
Expand Down
90 changes: 74 additions & 16 deletions scripts/backup-firewall.sh
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,54 @@
# the dir relative to that user's home unless absolute. The
# nightly unit can override it in /etc/default/homelab-timers.
# FW_KEEP default 30 exports to retain, here and there
#
# THE SWITCH, THROUGH THE SAME SCRIPT
#
# BACKUP_DEVICE=switch (`make backup-switch`) takes neo's RouterOS `/export`
# instead, with everything after the pull unchanged: encrypt, verify, copy to
# oracle, retention. The switch's export holds no password or key (RouterOS
# leaves users, certificates and SNMP passphrases out of it), but it does hold
# the rule bodies docs/security.md keeps unpublished, so it is handled exactly
# like config.xml. It goes to backups/switch/ here and on oracle.
#
# Its knobs are SW_*, never FW_*. Both units read /etc/default/homelab-timers,
# and an FW_OFFHOST set there for the firewall must not redirect the switch.
# SW_HOST default 10.7.7.2
# SW_USER default backup read and ssh policy only, address 10.7.7.1/32
# SW_JUMP default admin@10.0.99.1 the switch LAN admits SSH from itself
# only, so the login arrives from morpheus's 10.7.7.1
# SW_OFFHOST default atropos@10.0.99.30:backups/switch
# SW_KEEP default 30
set -euo pipefail

FW_HOST="${FW_HOST:-10.0.99.1}"
FW_USER="${FW_USER:-root}"
FW_PATH="${FW_PATH:-/cf/conf/config.xml}"
FW_OFFHOST="${FW_OFFHOST:-atropos@10.0.99.30:backups/firewall}"
FW_KEEP="${FW_KEEP:-30}"
BACKUP_DEVICE="${BACKUP_DEVICE:-firewall}"
case $BACKUP_DEVICE in
firewall)
FW_HOST="${FW_HOST:-10.0.99.1}"
FW_USER="${FW_USER:-root}"
FW_PATH="${FW_PATH:-/cf/conf/config.xml}"
FW_OFFHOST="${FW_OFFHOST:-atropos@10.0.99.30:backups/firewall}"
FW_KEEP="${FW_KEEP:-30}"
KEEP_VAR=FW_KEEP
PULL_CMD="cat $FW_PATH"
PULL_JUMP=()
RUNBOOK=docs/runbooks/restore-the-firewall.md
;;
switch)
FW_HOST="${SW_HOST:-10.7.7.2}"
FW_USER="${SW_USER:-backup}"
FW_PATH="/export"
FW_OFFHOST="${SW_OFFHOST:-atropos@10.0.99.30:backups/switch}"
FW_KEEP="${SW_KEEP:-30}"
KEEP_VAR=SW_KEEP
PULL_CMD="/export"
PULL_JUMP=(-J "${SW_JUMP:-admin@10.0.99.1}")
RUNBOOK=docs/runbooks/swap-the-switch.md
;;
*)
printf '\033[0;31mBACKUP_DEVICE must be firewall or switch, got %s\033[0m\n' "$BACKUP_DEVICE" >&2
exit 1 ;;
esac

# Anchored to the repository, not to the working directory, and that is not
# cosmetic (#611). These shipped relative, and agreed with where
Expand All @@ -121,7 +162,7 @@ FW_KEEP="${FW_KEEP:-30}"
# backup-volumes.sh:158 and backup-nas.sh:155 were already absolute; this is
# the odd one out being brought into line.
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
OUT_DIR="${REPO_ROOT}/backups/firewall"
OUT_DIR="${REPO_ROOT}/backups/${BACKUP_DEVICE}"
SOPS_POLICY="${REPO_ROOT}/.sops.yaml"

# Nightly, so 30 is a month of history — long enough to reach back past a bad
Expand All @@ -132,7 +173,7 @@ SOPS_POLICY="${REPO_ROOT}/.sops.yaml"
# Rejected rather than clamped: a mistyped FW_KEEP in an environment file is
# not a request to delete every backup of the firewall.
if ! [[ $FW_KEEP =~ ^[0-9]+$ ]] || ((FW_KEEP < 1)); then
printf '\033[0;31mFW_KEEP must be a positive integer, got %s\033[0m\n' "$FW_KEEP" >&2
printf '\033[0;31m%s must be a positive integer, got %s\033[0m\n' "$KEEP_VAR" "$FW_KEEP" >&2
exit 1
fi

Expand Down Expand Up @@ -178,10 +219,10 @@ need() { command -v "$1" >/dev/null 2>&1 || { red "missing dependency: $1"; exit
# The length floor is RLENGTH, not an interval expression: mawk 1.3.4-20240123
# (Debian and Ubuntu's default awk) reads /x{50,}/ as exactly 50 and cut every
# key to its first 54 characters (found 2026-09-26, #92).
recipients() { # [policy file, default .sops.yaml]
awk '
recipients() { # [policy file, default .sops.yaml] [device, default $BACKUP_DEVICE]
awk -v want="backups/${2:-$BACKUP_DEVICE}/" '
/^[[:space:]]*#/ { next }
/path_regex:/ { if (inrule) exit; if ($0 ~ /backups\/firewall/) inrule = 1; next }
/path_regex:/ { if (inrule) exit; if (index($0, want)) inrule = 1; next }
inrule {
line = $0
while (match(line, /age1[0-9a-z]+/)) {
Expand Down Expand Up @@ -224,9 +265,26 @@ verify() {
# are the config XML.
plain="$(env -i "PATH=$PATH" "HOME=$HOME" sops --decrypt --input-type yaml --output-type binary "$f" 2>/dev/null)" \
|| { red "FAILED to decrypt $f"; exit 1; }
local rules ver
if [[ $BACKUP_DEVICE == switch ]]; then
# The export's first line names the release, and a switch config without
# a bridge is not this switch's.
ver=$(sed -n '1s/.* by RouterOS \([^[:space:]]*\).*/\1/p' <<<"$plain" | tr -d '\r')
if [[ -z $ver ]] || ! grep -q '^/interface bridge' <<<"$plain"; then
red "decrypted $f but it does not look like a RouterOS export of neo"; exit 1
fi
Comment thread
Gerrrt marked this conversation as resolved.
Resolved
# RouterOS skips a menu it cannot read in time and carries on, leaving a
# `#error exporting` line where it was. That is a partial config, and
# accepting it would let retention prune a whole one.
if grep -q '^#error' <<<"$plain"; then
red "decrypted $f but RouterOS left part of it out ($(grep -c '^#error' <<<"$plain") #error line(s))"; exit 1
fi
rules=$(sed -n '/^\/ip firewall filter/,/^\/[a-z]/p' <<<"$plain" | grep -c '^add ' || true)
green "verified $(basename "$f") — RouterOS ${ver}, ${rules} input filter rules"
return 0
fi
grep -q '<pfsense>' <<<"$plain" \
|| { red "decrypted $f but it does not look like a pfSense config"; exit 1; }
local rules ver
rules=$(grep -c '<rule>' <<<"$plain" || true)
ver=$(sed -n 's:.*<version>\(.*\)</version>.*:\1:p' <<<"$plain" | head -1)
green "verified $(basename "$f") — config version ${ver:-unknown}, ${rules} firewall rules"
Expand Down Expand Up @@ -508,7 +566,7 @@ case "${1:-}" in
L=age1lablablablablablablablablablablablablablablablablablablabl
expect() { # <name> <want> <policy text>
printf '%s\n' "$3" > "$t/sops.yaml"
local got; got="$(recipients "$t/sops.yaml")"
local got; got="$(recipients "$t/sops.yaml" firewall)"
if [[ $got == "$2" ]]; then printf '\033[0;32m PASS\033[0m %s\n' "$1"
else printf '\033[0;31m FAIL\033[0m %s\n got %s\n expected %s\n' "$1" "$got" "$2"; fail=1; fi
}
Expand Down Expand Up @@ -630,13 +688,13 @@ info "pulling $FW_PATH from $FW_USER@$FW_HOST"
# shellcheck disable=SC2094 # --filename-override never opens $DEST; it is a
# string sops matches creation_rules against. The only reader here is
# /dev/stdin and the only writer is the redirect.
if ! "${SSH[@]}" "$FW_USER@$FW_HOST" "cat $FW_PATH" \
if ! "${SSH[@]}" "${PULL_JUMP[@]}" "$FW_USER@$FW_HOST" "$PULL_CMD" \
| sops --encrypt --age "$AGE_RECIPIENT" --input-type binary --output-type yaml \
--filename-override "$DEST" /dev/stdin \
> "$DEST"; then
rm -f "$DEST"
red "pull or encrypt failed — nothing written"
red "check: SSH enabled on pfSense, key authorised for $FW_USER, sops on PATH,"
red "check: SSH reaches $FW_HOST${PULL_JUMP[1]:+ through ${PULL_JUMP[1]}}, key authorised for $FW_USER, sops on PATH,"
red "and a .sops.yaml creation rule matching $DEST"
exit 1
fi
Expand All @@ -663,7 +721,7 @@ if ((LOCAL_ONLY)); then
printf '\n'
info "--local-only: this is on the same host as everything else it protects."
info "Retention was applied here; $OFFHOST_TARGET was not touched."
info "Run without the flag, or copy it yourself — see docs/runbooks/restore-the-firewall.md"
info "Run without the flag, or copy it yourself — see $RUNBOOK"
exit 0
fi

Expand All @@ -673,7 +731,7 @@ if ! copy_offhost; then
printf '\n'
red "wrote $DEST but the off-host copy FAILED — this backup is on the machine it protects"
red "check: $OFFHOST_TARGET reachable, its host key in ~/.ssh/known_hosts, and this"
red "host's key authorised there — docs/runbooks/restore-the-firewall.md"
red "host's key authorised there — $RUNBOOK"
exit 1
fi

Expand Down
5 changes: 3 additions & 2 deletions scripts/check_sops_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,10 +409,11 @@ def main() -> int:
problems.append(f"path_regex {raw!r} does not compile — {exc}")

# The paths sops is actually asked to encrypt. Stack secrets files, plus one
# firewall export: the stamp is arbitrary, so any well-formed name stands in
# for the whole class.
# firewall export and one switch export: the stamp is arbitrary, so any
# well-formed name stands in for the whole class.
paths = [f"secrets/{stack}.sops.yaml" for stack in stacks()]
paths.append("backups/firewall/config-20260101T000000Z.sops.yaml")
paths.append("backups/switch/config-20260101T000000Z.sops.yaml")

matched_by: dict[str, str] = {}
used: set[str] = set()
Expand Down
Loading