From af2c7fb6c9f9cb3a0e75362c9383ff71a723177c Mon Sep 17 00:00:00 2001 From: Jake Walker Date: Tue, 12 Aug 2025 18:27:12 +0100 Subject: [PATCH] add failure webhook --- defaults/main.yml | 2 ++ tasks/main.yml | 8 ++++++++ templates/restic-backup.service.j2 | 3 +++ templates/restic-failure.service.j2 | 7 +++++++ 4 files changed, 20 insertions(+) create mode 100644 templates/restic-failure.service.j2 diff --git a/defaults/main.yml b/defaults/main.yml index 111a446..6f8cbcc 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -26,6 +26,8 @@ restic_ssh_private_key_path: "/root/.ssh/backup" restic_systemd_timer_on_calender: "*-*-* 03:00:00" restic_systemd_timer_randomized_delay_sec: 0 +restic_failure_webhook_url: null + restic_arch_map: i386: '386' x86_64: 'amd64' diff --git a/tasks/main.yml b/tasks/main.yml index e2ed9d6..a2710d1 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -41,6 +41,14 @@ restic_folders_combined: "{{ restic_default_folders + restic_folders }}" notify: Reload systemd +- name: Add systemd service for restic failure + when: restic_failure_webhook_url is not none + ansible.builtin.template: + src: restic-failure.service.j2 + dest: /etc/systemd/system/restic-failure.service + mode: "0644" + notify: Reload systemd + - name: Add systemd timer for restic ansible.builtin.template: src: restic-backup.timer.j2 diff --git a/templates/restic-backup.service.j2 b/templates/restic-backup.service.j2 index 25013ea..c4848cd 100644 --- a/templates/restic-backup.service.j2 +++ b/templates/restic-backup.service.j2 @@ -1,5 +1,8 @@ [Unit] Description=Restic backup +{% if restic_failure_webhook_url is not none %} +OnFailure=restic-failure.service +{% endif %} [Service] Type=oneshot diff --git a/templates/restic-failure.service.j2 b/templates/restic-failure.service.j2 new file mode 100644 index 0000000..75a39e8 --- /dev/null +++ b/templates/restic-failure.service.j2 @@ -0,0 +1,7 @@ +[Unit] +Description=Restic backup failure + +[Service] +Type=oneshot +User={{ restic_user }} +ExecStartPre=/bin/sh -c 'curl -X POST -H "Content-Type: application/json" -d "{\"text\": \"Restic backup service failed on {{ inventory_hostname }}!\"}" {{ restic_failure_webhook_url }}'