From e7886d8c9866f8824e6ed5399e6e77cb34ac9005 Mon Sep 17 00:00:00 2001 From: transcaffeine Date: Sun, 5 May 2024 16:19:10 +0200 Subject: [PATCH] feat(restic): add optional hook and optional unlock --- roles/restic/tasks/main.yml | 2 +- roles/restic/templates/restic.service.j2 | 8 +++++++- roles/restic/templates/restic.timer.j2 | 3 +-- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/roles/restic/tasks/main.yml b/roles/restic/tasks/main.yml index f822b7b..a4d5eba 100644 --- a/roles/restic/tasks/main.yml +++ b/roles/restic/tasks/main.yml @@ -8,7 +8,7 @@ when: restic_create_user - name: Ensure either backup_paths or backup_stdin_command is populated - when: restic_backup_paths|length > 0 and restic_backup_stdin_command + when: restic_backup_paths|length > 0 and restic_backup_stdin_command and false fail: msg: "Setting both `restic_backup_paths` and `restic_backup_stdin_command` is not supported" diff --git a/roles/restic/templates/restic.service.j2 b/roles/restic/templates/restic.service.j2 index 52449c4..b11ba13 100644 --- a/roles/restic/templates/restic.service.j2 +++ b/roles/restic/templates/restic.service.j2 @@ -13,8 +13,14 @@ Environment=RESTIC_PASSWORD={{ restic_repo_password }} Environment=AWS_ACCESS_KEY_ID={{ restic_s3_key_id }} Environment=AWS_SECRET_ACCESS_KEY={{ restic_s3_access_key }} {% endif %} +{% if restic_unlock_before_backup | default(false) %} +ExecStartPre=-/bin/sh -c '/usr/bin/restic unlock' +{% endif %} ExecStartPre=-/bin/sh -c '/usr/bin/restic snapshots || /usr/bin/restic init' +{% if restic_backup_pre_hook | default(false) %} +ExecStart=-{{ restic_backup_pre_hook }} +{% endif %} {% if restic_backup_stdin_command %} ExecStart=/bin/sh -c '{{ restic_backup_stdin_command }} | /usr/bin/restic backup --verbose --stdin --stdin-filename {{ restic_backup_stdin_command_filename }}' {% else %} @@ -22,7 +28,7 @@ ExecStart=/usr/bin/restic --verbose backup {{ restic_backup_paths | join(' ') }} {% endif %} ExecStartPost=/usr/bin/restic forget --prune --keep-within={{ restic_policy.keep_within }} --keep-hourly={{ restic_policy.hourly }} --keep-daily={{ restic_policy.daily }} --keep-weekly={{ restic_policy.weekly }} --keep-monthly={{ restic_policy.monthly }} ExecStartPost=-/usr/bin/restic snapshots -{% if restic_backup_post_hook %} +{% if restic_backup_post_hook | default(false) %} ExecStartPost=-{{ restic_backup_post_hook }} {% endif %} ExecStartPost=/usr/bin/restic check diff --git a/roles/restic/templates/restic.timer.j2 b/roles/restic/templates/restic.timer.j2 index 2ec9503..5f61631 100644 --- a/roles/restic/templates/restic.timer.j2 +++ b/roles/restic/templates/restic.timer.j2 @@ -1,9 +1,8 @@ [Unit] -Description=Run {{ restic_job_name }} +Description=Run {{ restic_timer_description | default(restic_job_name) }} [Timer] OnCalendar={{ restic_policy.frequency }} -Persistent=True Unit={{ restic_systemd_unit_naming_scheme }}.service [Install]