feat(restic): migrate to systemd template units

This commit is contained in:
2025-03-09 13:07:03 +01:00
parent 788c4bada0
commit cc8e06f7d5
11 changed files with 139 additions and 101 deletions

View File

@ -0,0 +1,35 @@
---
- name: Check if 'restic_state' is valid
ansible.builtin.fail:
msg: >-2
Unknown value '{{ restic_state }}' for 'restic_state'!
Supported values are {{ restic_states | join(', ') }}
when: restic_state not in restic_states
- name: Ensure 'restic_job_name' is properly populated
ansible.builtin.fail:
msg: >-2
Unsupported restic_job_name '{{ restic_job_name | string }}'!
when:
- not (restic_job_name | string | length > 0)
- name: Ensure either backup_paths or backup_stdin_command is populated
ansible.builtin.fail:
msg: >-2
Setting both `restic_backup_paths` and `restic_backup_stdin_command`
is not supported!
when: restic_backup_paths|length > 0 and restic_backup_stdin_command and false
- name: Ensure a filename for stdin_command backup is given
ansible.builtin.fail:
msg: >-2
`restic_backup_stdin_command` was set but no filename for the resulting
output was supplied in `restic_backup_stdin_command_filename`.
when: restic_backup_stdin_command and not restic_backup_stdin_command_filename
- name: Ensure backup frequency adheres to systemd's OnCalender syntax
command:
cmd: "systemd-analyze calendar {{ restic_policy.frequency }}"
register: systemd_calender_parse_res
failed_when: systemd_calender_parse_res.rc != 0
changed_when: false

View File

@ -0,0 +1,20 @@
---
- name: Ensure restic is installed
block:
- name: Ensure restic is installed via apt
apt:
package: restic
state: latest
when: ansible_os_family == 'Debian'
- name: Ensure restic is installed via dnf
dnf:
name: restic
state: latest
when: ansible_os_family == 'RedHat'
- name: Ensure restic is installed using the auto-detected package-manager
package:
name: "{{ restic_package_name }}"
state: present
when: ansible_os_family not in ['RedHat', 'Debian']

View File

@ -1,46 +1,35 @@
---
- name: Check if role input is valid
ansible.builtin.include_tasks:
file: check.yml
- name: Ensure {{ restic_user }} system user exists
user:
- name: Ensure restic is {{ restic_state }}
ansible.builtin.include_tasks:
file: install.yml
- name: Ensure restic user '{{ restic_user }}' is {{ restic_state }}
ansible.builtin.user:
name: "{{ restic_user }}"
state: present
system: true
state: "{{ restic_state }}"
system: "{{ restic_user_system }}"
create_home: "{{ restic_user_create_home }}"
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 and false
fail:
msg: "Setting both `restic_backup_paths` and `restic_backup_stdin_command` is not supported"
- name: Ensure a filename for stdin_command backup is given
when: restic_backup_stdin_command and not restic_backup_stdin_command_filename
fail:
msg: "`restic_backup_stdin_command` was set but no filename for the resulting output was supplied in `restic_backup_stdin_command_filename`"
- name: Ensure backup frequency adheres to systemd's OnCalender syntax
command:
cmd: "systemd-analyze calendar {{ restic_policy.frequency }}"
register: systemd_calender_parse_res
failed_when: systemd_calender_parse_res.rc != 0
changed_when: false
- name: Ensure restic is installed
block:
- name: Ensure restic is installed via apt
apt:
package: restic
state: latest
when: ansible_os_family == 'Debian'
- name: Ensure restic is installed via dnf
dnf:
name: restic
state: latest
when: ansible_os_family == 'RedHat'
- name: Ensure restic is installed using the auto-detected package-manager
package:
name: "{{ restic_package_name }}"
state: present
when: ansible_os_family not in ['RedHat', 'Debian']
- name: Ensure restic backup scripts are {{ restic_state }}
ansible.builtin.copy:
src: "{{ script.source }}"
dest: "{{ script.destination }}"
mode: "{{ script.mode }}"
loop:
- source: restic-backup.sh
destination: "{{ restic_systemd_service_exec_start }}"
mode: "0510"
- source: restic-snapshot-metrics.sh
destination: "{{ restic_script_generate_snapshot_metrics }}"
mode: "0510"
loop_control:
loop_var: script
label: "{{ script.source }}"
- name: Ensure systemd service file for '{{ restic_job_name }}' is templated
template: