/
/
/
Ansible role that can run restic backups and push it to a remote storage server.
1---
2# Back up everything on this host: /docker services, data paths, appliances.
3# Every item records its outcome in `backup_results` instead of failing the
4# host, so one broken service never stops the others from being backed up.
5# The maintenance stage (backup.yml) evaluates the results and fails the run.
6
7- name: Start with an empty result list
8 ansible.builtin.set_fact:
9 backup_results: []
10 backup_started: "{{ now(utc=true).isoformat() }}"
11
12- name: Find service directories
13 ansible.builtin.find:
14 paths: "{{ backup_docker_root }}"
15 file_type: directory
16 recurse: false
17 hidden: false
18 register: _backup_dirs
19
20- name: Decide which services to back up
21 ansible.builtin.set_fact:
22 _backup_targets: >-
23 {{ _backup_dirs.files | map(attribute='path') | map('basename') | sort
24 | reject('in', backup_skip_services + backup_host_skip_services) | list }}
25
26- name: Limit to the requested services
27 ansible.builtin.set_fact:
28 _backup_targets: "{{ _backup_targets | select('in', _backup_selection) | list }}"
29 when: _backup_selection | length > 0
30
31- name: Show the backup plan for this host
32 ansible.builtin.debug:
33 msg: |
34 Docker services ({{ _backup_targets | length }}): {{ _backup_targets | join(', ') }}
35 Skipped: {{ (backup_skip_services + backup_host_skip_services) | join(', ') }}
36 Data paths: {{ backup_data_paths | map(attribute='name') | join(', ') or 'none' }}
37 Appliances: {{ ('Home Assistant ' if backup_ha_enabled else '') ~ ('OPNsense' if backup_opnsense_enabled else '') if backup_appliance_enabled else 'none' }}
38
39- name: Back up each service
40 ansible.builtin.include_tasks: backup-service.yml
41 loop: "{{ _backup_targets }}"
42 loop_control:
43 loop_var: svc
44
45- name: Back up data paths
46 ansible.builtin.include_tasks: backup-path.yml
47 loop: "{{ backup_data_paths }}"
48 loop_control:
49 loop_var: dp
50 label: "{{ dp.name }}"
51 when: _backup_selection | length == 0 or dp.name in _backup_selection
52
53- name: Back up appliances
54 ansible.builtin.include_tasks: appliance-backup.yml
55 when:
56 - backup_appliance_enabled | bool
57 - _backup_selection | length == 0 or ('homeassistant' in _backup_selection or 'opnsense' in _backup_selection)
58
59- name: Summarize this host
60 ansible.builtin.debug:
61 msg: |
62 {{ inventory_hostname }}: {{ backup_results | selectattr('status', 'equalto', 'ok') | list | length }} ok,
63 {{ backup_results | rejectattr('status', 'equalto', 'ok') | list | length }} failed
64 {% for r in backup_results | rejectattr('status', 'equalto', 'ok') %}
65 - {{ r.name }}: {{ r.error }}
66 {% endfor %}
67