Skip to content

Commit

Permalink
Periodimail: send e-mails every two days if deretheni backups fail.
Browse files Browse the repository at this point in the history
  • Loading branch information
edanaher committed Oct 24, 2017
1 parent ce47ab4 commit 5762cf1
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
5 changes: 5 additions & 0 deletions chileh-backups.nix
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
{ config, lib, pkgs, ... }:

let two-days = 60 * 60 * 24 * 2 - 60; in
{
config = lib.mkIf config.host.chileh-backups.enable {
services.periodimail.intervals = [ two-days ];
systemd.services.backup-deretheni = {
description = "Backup deretheni";
path = with pkgs; [ borgbackup bup rsync openssh ];
wants = [ "network-online.target" ];
unitConfig = {
OnFailure = "periodimail-${builtins.toString two-days}@%n.service";
};
serviceConfig = {
User = "edanaher";
Group = "users";
Expand Down
1 change: 1 addition & 0 deletions configuration.nix
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
./exim
./pulseaudio
./xserver.nix
./scripts/periodimail.nix
];

boot.kernelParams = lib.optional (config.host.class != "server")
Expand Down
53 changes: 53 additions & 0 deletions scripts/periodimail.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{ config, lib, pkgs, ... }:

let periodimail = pkgs.writeScript "periodimail" ''
#!/bin/sh
unit=''${2//\//_}
mkdir -p /tmp/.periodimail
MARKFILE=/tmp/.periodimail/$unit
if [ -f $MARKFILE ]; then
echo File exists for $unit
if [[ $((`stat -c %Y $MARKFILE` + $1)) -lt `date +%s` ]]; then
echo Removing stale file $unit
rm $MARKFILE
fi
fi
if [ ! -f $MARKFILE ]; then
echo Mailing for $unit
touch $MARKFILE
exim [email protected] <<EOF
From: [email protected]
To: [email protected]
Subject: Chileh unit '$2' failed
$(journalctl -u $2 -n 10)
EOF
fi
echo Finished $unit
'';
serviceForInterval = interval: {
name = "periodimail-${builtins.toString interval}@";
value = {
description = "Send e-mail on unit failure, but only every ${builtins.toString interval} seconds";
path = with pkgs; [ systemd exim ];
wants = [ "network-online.target" ];
serviceConfig = {
ExecStart = "${periodimail} ${builtins.toString interval} %I";
};
};
};
in
{
options = {
services.periodimail.intervals =
lib.mkOption {
type = lib.types.listOf lib.types.int;
default = [];
description = "Intervals to run periodimail on (in seconds)";
};
};

config = {
systemd.services = lib.listToAttrs (map serviceForInterval config.services.periodimail.intervals);
};
}

0 comments on commit 5762cf1

Please sign in to comment.