From ad796f155bc2e44d6944f002ad95596e39711670 Mon Sep 17 00:00:00 2001 From: Tanner Doshier Date: Thu, 7 Jan 2016 05:06:05 -0600 Subject: [PATCH] nixos: tarsnap - make systemd timer persistent A machine may not always be active (or online!) when a backup timer triggers, meaning backups can be missed - now we properly set the tarsnap timer's Persistent option so systemd will run the command even when the machine wasn't online at that exact time. However, we also need to make sure that we can contact the tarsnap server reliably before we start the backup. So, we attempt to ping the access endpoint in a loop with a sleep, before continuing. This fixes #8823. Signed-off-by: Austin Seipp --- nixos/modules/services/backup/tarsnap.nix | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/nixos/modules/services/backup/tarsnap.nix b/nixos/modules/services/backup/tarsnap.nix index 57121e23855..3a51e6b7aa6 100644 --- a/nixos/modules/services/backup/tarsnap.nix +++ b/nixos/modules/services/backup/tarsnap.nix @@ -242,9 +242,16 @@ in systemd.services."tarsnap@" = { description = "Tarsnap archive '%i'"; - requires = [ "network.target" ]; + requires = [ "network-online.target" ]; + after = [ "network-online.target" ]; - path = [ pkgs.tarsnap pkgs.coreutils ]; + path = [ pkgs.iputils pkgs.tarsnap pkgs.coreutils ]; + + # In order for the persistent tarsnap timer to work reliably, we have to + # make sure that the tarsnap server is reachable after systemd starts up + # the service - therefore we sleep in a loop until we can ping the + # endpoint. + preStart = "while ! ping -q -c 1 betatest-server.tarsnap.com &> /dev/null; do sleep 3; done"; scriptArgs = "%i"; script = '' mkdir -p -m 0755 ${dirOf cfg.cachedir} @@ -259,11 +266,15 @@ in IOSchedulingClass = "idle"; NoNewPrivileges = "true"; CapabilityBoundingSet = "CAP_DAC_READ_SEARCH"; + PermissionsStartOnly = "true"; }; }; + # Note: the timer must be Persistent=true, so that systemd will start it even + # if e.g. your laptop was asleep while the latest interval occurred. systemd.timers = mapAttrs' (name: cfg: nameValuePair "tarsnap@${name}" { timerConfig.OnCalendar = cfg.period; + timerConfig.Persistent = "true"; wantedBy = [ "timers.target" ]; }) cfg.archives;