Files
nix-config/modules/nixos/base/btrbk.nix
T
ryan4yin | 二花 e37042f61c feat(youko): restic backup of the service state to the HDD (part 1) (#299)
* feat(hosts): per-host restic backups to the SMB remote

Replace the disabled aggregate job (which rsynced every host's /persistent
into /tmp - a tmpfs - and silently produced incomplete backups) with one
restic repo per host, backing up its own data directly:

- shared module lib/genResticBackup.nix: secrets, rclone/SMB target
  (rclone:smb-downloads:/Downloads/homelab-backup/<host>), retention, and
  excludes for the regenerable/bulky paths (podman overlay layers, microvm
  and libvirt disk images, nfs golden images, logs, caches)
- youko additionally backs up the service state under /persistent/var/lib
  and dumps all postgres databases first (a consistent SQL artifact beside
  the crash-consistent data dir)
- shoryu/shushou back up /etc/agenix, /etc/ssh and the k3s token

Needs three agenix secrets before it can run: restic-password,
ssh-key-for-restic-backup and rclone-conf-for-restic-backup.

* refactor(hosts): only restic does off-host; btrbk stays local

btrbk works at the subvolume level, so a  would ship the whole
@persistent - including podman's overlay storage, the microVM/libvirt
images and the nfs golden images - which is exactly the regenerable bulk
we want out of the backups.

So: btrbk keeps making the cheap local CoW snapshots (quick rollbacks),
and restic owns the off-host copies with file-level excludes. Back up the
whole /persistent minus those excludes, so nothing is missed.

Update the btrbk module's comment to say so.

* fix(restic): never back up keys; drop the implicit /etc/agenix

restic's own password lives in /etc/agenix, so backing that up would store
the repository's password inside the repository. Exclude agenix, the ssh
host keys and the usual credential dirs (.ssh, .gnupg, .aws, gcloud).

* refactor(restic): make the backup a proper module

Replace lib/genResticBackup.nix with modules/nixos/base/restic-backup.nix,
which declares modules.restic-backup options (enable, repository, paths,
exclude, requiresMountsFor, postgresDump) like the btrbk module does.
youko just sets the options now.

* refactor(restic): exclude the whole podman storage, trim retention

Exclude /persistent/var/lib/containers wholesale: the image layers are
re-pullable and podman's storage DB is not consistent when copied from a
running podman. uptime-kuma's named volume lives there, but its data is
not wanted anyway.

Retention: daily 3, weekly 2, monthly 2 - no yearly.

* refactor(restic): back up a short-lived btrfs snapshot, drop pg_dump

A live copy of a running postgres data dir is only crash-consistent. Instead
of a daily pg_dumpall (which duplicates the data in the repository and gets
slower as the DB grows), take a short-lived read-only btrfs snapshot in
backupPrepareCommand, back that up, and delete it right after - a
consistency step, not a second backup. The excludes are relative now so they
work for both the snapshot and a direct path.

* feat(secrets): restic-password for modules.restic-backup

With the rekeyed secret in nix-secrets (storage group, alongside
hdd-luks-crypt-key), the module's passwordFile resolves.

* fix(restic): agenix's secret is under /run/agenix, not /etc/agenix

* fix(restic): take the password path from agenix, not a hardcoded one

config.age.secrets."restic-password".path follows agenix's secretsDir, so it
stays correct if that changes.
2026-09-21 19:00:35 +08:00

110 lines
3.7 KiB
Nix

{
config,
lib,
...
}:
let
cfg = config.modules.btrbk;
in
{
# ==================================================================
#
# btrbk - scheduled LOCAL btrfs snapshots.
# https://github.com/digint/btrbk
#
# Snapshots are created in `snapshotDir` (relative to the btrfs top-level
# subvolume mounted at `volume`) and pruned by `snapshot_preserve`. With the
# defaults they land in the @snapshots subvolume (mounted at /snapshots):
#
# /btr_pool/@snapshots/@persistent.<timestamp>
#
# These are same-filesystem snapshots: they protect against accidental
# deletion and bad edits, NOT against disk loss. Off-host copies are handled
# by restic (lib/genResticBackup.nix), which can exclude the regenerable bulk
# at the file level; btrbk works at the subvolume level, so a `target` would
# also ship podman's overlay storage and the VM disk images. Set `target`
# (plus `services.btrbk.sshAccess` on the receiving host) only if you want
# that.
#
# The host MUST mount the btrfs top-level subvolume (subvolid=5) at `volume`;
# this is enforced by an assertion so a missing mount fails evaluation instead
# of failing silently at 3am.
#
# Restore a snapshot (offline; stop writers first):
# 1. btrfs subvolume delete /btr_pool/@persistent
# 2. btrfs subvolume snapshot /btr_pool/@snapshots/@persistent.<timestamp> \
# /btr_pool/@persistent
# 3. reboot, or remount /persistent, to pick up the restored subvolume.
#
# ==================================================================
options.modules.btrbk = {
enable = lib.mkEnableOption "scheduled btrfs snapshots via btrbk";
volume = lib.mkOption {
type = lib.types.str;
default = "/btr_pool";
description = "Mount point of the btrfs top-level subvolume (subvolid=5).";
};
subvolume = lib.mkOption {
type = lib.types.str;
default = "@persistent";
description = "Source subvolume to snapshot, relative to {option}`modules.btrbk.volume`.";
};
snapshotDir = lib.mkOption {
type = lib.types.str;
default = "@snapshots";
description = "Directory the snapshots are created in, relative to {option}`modules.btrbk.volume`.";
};
target = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
description = ''
Optional off-host btrbk backup target (`target` in btrbk.conf). When
null, only local snapshots are kept, which does not protect against
disk loss.
'';
};
onCalendar = lib.mkOption {
type = lib.types.str;
default = "Tue,Sat *-*-* 3:45:20";
description = "systemd calendar expression for the snapshot timer.";
};
};
config = lib.mkIf cfg.enable {
assertions = [
{
assertion = builtins.hasAttr cfg.volume config.fileSystems;
message = "modules.btrbk: no filesystem is mounted at `${cfg.volume}`; mount the btrfs top-level subvolume (subvolid=5) there so btrbk can snapshot `${cfg.volume}/${cfg.subvolume}`.";
}
];
services.btrbk.instances.btrbk = {
onCalendar = cfg.onCalendar;
settings = {
# keep daily snapshots for 7 days, and always keep 2 days worth.
snapshot_preserve = "7d";
snapshot_preserve_min = "2d";
# retention for an optional off-host target.
target_preserve = "9d 4w 2m";
target_preserve_min = "no";
volume.${cfg.volume} = {
snapshot_dir = cfg.snapshotDir;
subvolume.${cfg.subvolume} = {
snapshot_create = "always";
};
}
// lib.optionalAttrs (cfg.target != null) {
target = cfg.target;
};
};
};
};
}