Monitoring primarily
This commit is contained in:
+23
-36
@@ -82,13 +82,25 @@ in
|
||||
# Pre-backup hook: pg_dump + nextcloud maintenance mode
|
||||
# -----------------------------------------------------------------------
|
||||
systemd.services."homey-backup-pre" = {
|
||||
description = "Pre-backup hooks (pg_dump, NC maintenance mode)";
|
||||
description = "Pre-backup hooks (pg_dump, NC maintenance mode, secrets env)";
|
||||
serviceConfig = {
|
||||
Type = "oneshot";
|
||||
ExecStart = pkgs.writeShellScript "backup-pre" ''
|
||||
set -euo pipefail
|
||||
podman="${pkgs.podman}/bin/podman"
|
||||
|
||||
# Write S3 credentials env file now, before restic-backups-homey.service
|
||||
# starts — systemd loads EnvironmentFile= before ExecStartPre runs, so
|
||||
# the file must already exist when the restic unit activates.
|
||||
install -m 0600 /dev/null /run/restic-homey-secrets.env
|
||||
{
|
||||
printf 'AWS_ACCESS_KEY_ID=%s\n' \
|
||||
"$(cat ${config.sops.secrets."restic/s3_access_key_id".path})"
|
||||
printf 'AWS_SECRET_ACCESS_KEY=%s\n' \
|
||||
"$(cat ${config.sops.secrets."restic/s3_secret_access_key".path})"
|
||||
printf 'RESTIC_CACHE_DIR=%s\n' "${dataDir}/restic-cache"
|
||||
} >> /run/restic-homey-secrets.env
|
||||
|
||||
# Put Nextcloud into maintenance mode (if running)
|
||||
if systemctl is-active --quiet podman-nextcloud.service; then
|
||||
$podman exec nextcloud php occ maintenance:mode --on || true
|
||||
@@ -105,19 +117,6 @@ in
|
||||
};
|
||||
};
|
||||
|
||||
systemd.services."homey-backup-post" = {
|
||||
description = "Post-backup hooks (take NC out of maintenance mode)";
|
||||
serviceConfig = {
|
||||
Type = "oneshot";
|
||||
ExecStart = pkgs.writeShellScript "backup-post" ''
|
||||
set -euo pipefail
|
||||
if systemctl is-active --quiet podman-nextcloud.service; then
|
||||
${pkgs.podman}/bin/podman exec nextcloud php occ maintenance:mode --off || true
|
||||
fi
|
||||
'';
|
||||
};
|
||||
};
|
||||
|
||||
# -----------------------------------------------------------------------
|
||||
# Restic backup service
|
||||
# -----------------------------------------------------------------------
|
||||
@@ -125,7 +124,7 @@ in
|
||||
repository = cfg.repository;
|
||||
passwordFile = config.sops.secrets."restic/password".path;
|
||||
|
||||
# Runtime env file written by ExecStartPre (see systemd override below)
|
||||
# Runtime env file written by homey-backup-pre.service (which runs first)
|
||||
environmentFile = "/run/restic-homey-secrets.env";
|
||||
|
||||
paths = [
|
||||
@@ -137,6 +136,9 @@ in
|
||||
"${dataDir}/jellyfin"
|
||||
"${dataDir}/transmission"
|
||||
# Deliberately excluded: media/* (large, can be re-downloaded)
|
||||
# Monitoring — uptime-kuma has monitors/history, ntfy has user accounts
|
||||
"${dataDir}/uptime-kuma"
|
||||
"${dataDir}/ntfy"
|
||||
];
|
||||
|
||||
# Exclude Nextcloud's raw DB directory in favour of the pg_dump file
|
||||
@@ -157,36 +159,21 @@ in
|
||||
];
|
||||
};
|
||||
|
||||
# Wire the pre/post hooks around the restic job and inject secrets
|
||||
# Wire the pre/post hooks around the restic job
|
||||
systemd.services."restic-backups-homey" = {
|
||||
requires = [ "homey-backup-pre.service" ];
|
||||
after = [ "homey-backup-pre.service" ];
|
||||
serviceConfig = {
|
||||
# Write runtime env file with actual secret values (restic needs the
|
||||
# raw values; it does not support _FILE suffix env vars).
|
||||
ExecStartPre = [
|
||||
(pkgs.writeShellScript "restic-inject-secrets" ''
|
||||
install -m 0600 /dev/null /run/restic-homey-secrets.env
|
||||
{
|
||||
printf 'AWS_ACCESS_KEY_ID=%s\n' \
|
||||
"$(cat ${config.sops.secrets."restic/s3_access_key_id".path})"
|
||||
printf 'AWS_SECRET_ACCESS_KEY=%s\n' \
|
||||
"$(cat ${config.sops.secrets."restic/s3_secret_access_key".path})"
|
||||
printf 'RESTIC_CACHE_DIR=%s\n' "${dataDir}/restic-cache"
|
||||
} >> /run/restic-homey-secrets.env
|
||||
'')
|
||||
];
|
||||
ExecStopPost = [
|
||||
(pkgs.writeShellScript "restic-cleanup-secrets" ''
|
||||
(pkgs.writeShellScript "restic-post-hooks" ''
|
||||
# Always runs on stop, success or failure
|
||||
rm -f /run/restic-homey-secrets.env
|
||||
if systemctl is-active --quiet podman-nextcloud.service; then
|
||||
${pkgs.podman}/bin/podman exec nextcloud php occ maintenance:mode --off || true
|
||||
fi
|
||||
'')
|
||||
];
|
||||
};
|
||||
};
|
||||
|
||||
systemd.services."homey-backup-post" = {
|
||||
after = [ "restic-backups-homey.service" ];
|
||||
wantedBy = [ "restic-backups-homey.service" ];
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user