os/hosts/droppie/restic-backup.nix

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

51 lines
1.2 KiB
Nix
Raw Normal View History

2023-01-28 22:27:52 +01:00
{pkgs, ...}: let
shutdownWaitMinutes = 15;
2022-11-27 23:35:12 +01:00
shutdownScript = pkgs.writeShellScriptBin "shutdown-wait" ''
2022-11-27 21:36:46 +01:00
STATUS_FILES="/media/internal/backups-pub-solar/status"
2022-11-27 21:36:46 +01:00
running=""
2022-11-27 21:36:46 +01:00
for f in $STATUS_FILES; do
declare started
declare finished
2022-11-27 21:36:46 +01:00
started=$(source $f ; echo ''${BACKUP_STARTED})
finished=$(source $f ; echo ''${BACKUP_FINISHED})
2022-11-27 21:36:46 +01:00
if [ -z "''${finished}" ]; then
echo "backup $(dirname $f) still running"
running="yes"
break
fi
done
2022-11-27 21:36:46 +01:00
if [ -n "''${running}" ] && [ "''${running}" = "yes" ]; then
echo "backups are still running"
exit 1
fi
2022-11-27 21:36:46 +01:00
echo "WARNING: System will be shut down within the next 15 minutes" | wall
2022-11-27 21:36:46 +01:00
sleep 10
2022-11-27 21:36:46 +01:00
shutdown -P +${builtins.toString shutdownWaitMinutes}
'';
2023-01-28 22:27:52 +01:00
in {
systemd.services."shutdown-after-backup" = {
enable = true;
serviceConfig = {
2022-11-27 23:35:12 +01:00
ExecStart = "${shutdownScript}/bin/shutdown-wait";
Type = "oneshot";
};
};
systemd.timers."shutdown-after-backup" = {
enable = true;
2022-11-27 21:36:46 +01:00
timerConfig = {
2022-12-06 12:30:59 +01:00
OnCalendar = "3..9:* Etc/UTC";
};
2023-01-28 22:27:52 +01:00
wantedBy = ["timers.target"];
partOf = ["shutdown-after-backup.service"];
};
}