Files
Watari-Ansible/run-backups.yaml
2026-07-28 00:28:35 +00:00

129 lines
4.4 KiB
YAML

---
- name: Centralized Restic Fleet Backup with Systemd Hooks
hosts: all
become: true
serial: 5
vars:
# Map of group -> {label, path}. A host can belong to more than one of
# these groups (e.g. semaphore, which spans apps_data and apps_docker);
# backup_targets below collects ALL matching entries for this host, so
# everything gets backed up under a single stop/start cycle.
group_paths:
app_data:
label: apps_data
path: /apps/data/
app_docker:
label: apps_docker
path: /apps/docker/
home_grail:
label: home_grail
path: /home/grail/
backup_targets: "{{ group_paths | dict2items | selectattr('key', 'in', group_names) | map(attribute='value') | list }}"
restic_env: &restic_env
RESTIC_REPOSITORY: "s3:http://misamisa.duckdns.org:9000/resticbackup"
RESTIC_PASSWORD: "{{ vault_restic_password }}"
AWS_ACCESS_KEY_ID: "{{ vault_aws_access_key_id }}"
AWS_SECRET_ACCESS_KEY: "{{ vault_aws_secret_access_key }}"
tasks:
- name: Fail fast if this host matches no known group
ansible.builtin.assert:
that:
- backup_targets | length > 0
fail_msg: >-
Host {{ inventory_hostname }} is not in any group listed in group_paths
(app_data, app_docker, home_grail). Check group membership.
- name: Execute Backup Sequence with Safety Catch
block:
- name: Gracefully stop the application via Systemd
ansible.builtin.systemd:
name: app
state: stopped
- name: Check for and remove stale locks
community.docker.docker_container:
name: restic_unlock
image: restic/restic:latest
state: started
detach: false
cleanup: true
command: "unlock"
env: *restic_env
register: restic_unlock_result
failed_when: restic_unlock_result.status != 0
- name: Init restic repo (per target)
community.docker.docker_container:
name: "restic_init_{{ item.label }}"
image: restic/restic:latest
state: started
detach: false
cleanup: true
command: "init"
volumes:
- "{{ item.path }}:/backup_target/{{ item.label }}:ro"
env: *restic_env
register: restic_init_result
failed_when:
- restic_init_result.status != 0
- "'already initialized' not in restic_init_result.container.Output"
loop: "{{ backup_targets }}"
loop_control:
label: "{{ item.label }}"
- name: Run restic backup (per target)
community.docker.docker_container:
name: "restic_backup_{{ item.label }}"
image: restic/restic:latest
state: started
detach: false
cleanup: true
timeout: 600
output_logs: true
command: >-
backup /backup_target/{{ item.label }}
--host "{{ inventory_hostname }}"
--tag "{{ item.label }}"
volumes:
- "{{ item.path }}:/backup_target/{{ item.label }}:ro"
env: *restic_env
register: restic_backup_result
loop: "{{ backup_targets }}"
loop_control:
label: "{{ item.label }}"
- name: Show backup output
ansible.builtin.debug:
msg: "{{ item.container.Output }}"
loop: "{{ restic_backup_result.results }}"
loop_control:
label: "{{ item.item.label }}"
- name: Prune old snapshots (per target)
community.docker.docker_container:
name: "restic_prune_{{ item.label }}"
image: restic/restic:latest
state: started
detach: false
cleanup: true
command: >-
forget
--host "{{ inventory_hostname }}"
--tag "{{ item.label }}"
--keep-daily 7 --keep-weekly 4 --keep-monthly 12 --prune
volumes:
- "{{ item.path }}:/backup_target/{{ item.label }}:ro"
env: *restic_env
register: restic_prune_result
loop: "{{ backup_targets }}"
loop_control:
label: "{{ item.label }}"
always:
- name: Ensure the application is started back up
ansible.builtin.systemd:
name: app
state: started