From 697198834c6a861d30b8fbfe4162525c87155e00 Mon Sep 17 00:00:00 2001 From: blargg Date: Mon, 17 May 2021 18:54:13 -0700 Subject: [PATCH 1/3] nixos/borgbackup: Add a persistentTimer option. Persistent starts the backup service on power on if it was missed while the system was powered down, for example. --- nixos/modules/services/backup/borgbackup.nix | 24 ++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/nixos/modules/services/backup/borgbackup.nix b/nixos/modules/services/backup/borgbackup.nix index 6804055a2940..cb2c4d6381f9 100644 --- a/nixos/modules/services/backup/borgbackup.nix +++ b/nixos/modules/services/backup/borgbackup.nix @@ -102,6 +102,14 @@ let inherit (cfg) startAt; }; + mkBackupTimers = name: cfg: + nameValuePair "borgbackup-job-${name}" { + description = "BorgBackup job ${name} timer"; + timerConfig = { + Persistent = cfg.persistentTimer; + }; + }; + # utility function around makeWrapper mkWrapperDrv = { original, name, set ? {} @@ -321,6 +329,19 @@ in { ''; }; + persistentTimer = mkOption { + default = false; + type = types.bool; + example = true; + description = literalDocBook '' + Set the persistentTimer option for the + systemd.timer + 5 + which triggers the backup immediately if the last trigger + was missed (e.g. if the system was powered down). + ''; + }; + user = mkOption { type = types.str; description = '' @@ -695,6 +716,9 @@ in { # A repo named "foo" is mapped to systemd.services.borgbackup-repo-foo // mapAttrs' mkRepoService repos; + # A job named "foo" is mapped to systemd.timers.borgbackup-job-foo + systemd.timers = mapAttrs' mkBackupTimers jobs; + users = mkMerge (mapAttrsToList mkUsersConfig repos); environment.systemPackages = with pkgs; [ borgbackup ] ++ (mapAttrsToList mkBorgWrapper jobs); From 91dfaa5453fca2a12f375d25b084f82d1a370491 Mon Sep 17 00:00:00 2001 From: Felix Buehler Date: Tue, 11 Jan 2022 13:43:07 +0100 Subject: [PATCH 2/3] nixos/borgbackup: start remote backup only if network is available --- nixos/modules/services/backup/borgbackup.nix | 2 ++ 1 file changed, 2 insertions(+) diff --git a/nixos/modules/services/backup/borgbackup.nix b/nixos/modules/services/backup/borgbackup.nix index cb2c4d6381f9..05f222d5fbe2 100644 --- a/nixos/modules/services/backup/borgbackup.nix +++ b/nixos/modules/services/backup/borgbackup.nix @@ -108,6 +108,8 @@ let timerConfig = { Persistent = cfg.persistentTimer; }; + # if remote-backup wait for network + after = optional (cfg.persistentTimer && !isLocalPath cfg.repo) "network-online.target"; }; # utility function around makeWrapper From 7caa6f4de48ae4de02e12aaa13a9eb9a66013ae8 Mon Sep 17 00:00:00 2001 From: Felix Buehler Date: Mon, 17 Jan 2022 16:00:31 +0100 Subject: [PATCH 3/3] nixos/borgbackup: move systemd.timers logic into single block --- nixos/modules/services/backup/borgbackup.nix | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/nixos/modules/services/backup/borgbackup.nix b/nixos/modules/services/backup/borgbackup.nix index 05f222d5fbe2..2c307a701f3e 100644 --- a/nixos/modules/services/backup/borgbackup.nix +++ b/nixos/modules/services/backup/borgbackup.nix @@ -99,14 +99,15 @@ let BORG_REPO = cfg.repo; inherit (cfg) extraArgs extraInitArgs extraCreateArgs extraPruneArgs; } // (mkPassEnv cfg) // cfg.environment; - inherit (cfg) startAt; }; mkBackupTimers = name: cfg: nameValuePair "borgbackup-job-${name}" { description = "BorgBackup job ${name} timer"; + wantedBy = [ "timers.target" ]; timerConfig = { Persistent = cfg.persistentTimer; + OnCalendar = cfg.startAt; }; # if remote-backup wait for network after = optional (cfg.persistentTimer && !isLocalPath cfg.repo) "network-online.target"; @@ -719,7 +720,8 @@ in { // mapAttrs' mkRepoService repos; # A job named "foo" is mapped to systemd.timers.borgbackup-job-foo - systemd.timers = mapAttrs' mkBackupTimers jobs; + # only generate the timer if interval (startAt) is set + systemd.timers = mapAttrs' mkBackupTimers (filterAttrs (_: cfg: cfg.startAt != []) jobs); users = mkMerge (mapAttrsToList mkUsersConfig repos);