Merging Watari-Ansible and Homelab-Backups

This commit is contained in:
tolerryan
2026-07-28 00:28:35 +00:00
parent 3eef168b17
commit b460cf8d6b
12 changed files with 842 additions and 126 deletions

135
restore-backups.yml Normal file
View File

@@ -0,0 +1,135 @@
---
- name: Centralized Restic Fleet Restore (Latest Snapshot)
hosts: all
become: true
serial: 1 # Restores are riskier than backups; do one host at a time by default
# Same source/location mapping as run-backups.yaml, kept in sync intentionally.
vars:
# Set to false (default) to require an explicit opt-in before anything destructive happens.
# Run with: ansible-playbook restore-backups.yaml -e "confirm_restore=true"
confirm_restore: false
# Defaults to "latest" per host+tag. Override with -e "restore_snapshot_id=abcd1234"
# to restore a specific snapshot instead.
restore_snapshot_id: "latest"
backup_source_label: >-
{%- if 'app_data' in group_names -%}
apps_data
{%- elif 'app_docker' in group_names -%}
apps_docker
{%- elif 'home_grail' in group_names -%}
home_grail
{%- endif -%}
backup_location: >-
{%- if 'app_data' in group_names -%}
/apps/data/
{%- elif 'app_docker' in group_names -%}
/apps/docker/
{%- elif 'home_grail' in group_names -%}
/home/grail/
{%- endif -%}
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: Refuse to proceed without explicit confirmation
ansible.builtin.assert:
that:
- confirm_restore | bool
fail_msg: >-
This playbook will overwrite {{ backup_location }} on {{ inventory_hostname }}
with data from a restic snapshot. Re-run with -e "confirm_restore=true" to proceed,
optionally with -e "restore_snapshot_id=<id>" to pick a snapshot other than latest.
success_msg: "Confirmed. Proceeding with restore on {{ inventory_hostname }}."
- name: Fail fast if backup_location/backup_source_label could not be resolved
ansible.builtin.assert:
that:
- backup_location | trim | length > 0
- backup_source_label | trim | length > 0
fail_msg: >-
Host {{ inventory_hostname }} did not match any known inventory group
(app_data, app_docker, home_grail). Check group membership.
- name: List available snapshots for this host/tag (for the log)
community.docker.docker_container:
name: restic_snapshots_list
image: restic/restic:latest
state: started
detach: false
cleanup: true
output_logs: true
command: >-
snapshots
--host "{{ inventory_hostname }}"
--tag "{{ backup_source_label }}"
env: *restic_env
register: restic_snapshots_result
- name: Show available snapshots
ansible.builtin.debug:
var: restic_snapshots_result.container.Output
- name: Restore Sequence with Safety Catch
block:
- name: Gracefully stop the application via Systemd
ansible.builtin.systemd:
name: app
state: stopped
- name: Ensure restore target exists with correct ownership before mounting
ansible.builtin.file:
path: "{{ backup_location }}"
state: directory
owner: docker
group: docker
mode: "0755"
- 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: Restore latest snapshot into place
community.docker.docker_container:
name: restic_restore
image: restic/restic:latest
state: started
detach: false
cleanup: true
timeout: 600
output_logs: true
command: >-
restore {{ restore_snapshot_id }}
--host "{{ inventory_hostname }}"
--tag "{{ backup_source_label }}"
--target /
volumes:
# NOTE: rw (no :ro) here — this is the whole point of a restore.
- "{{ backup_location }}:/backup_target/{{ backup_source_label }}"
env: *restic_env
register: restic_restore_result
failed_when: restic_restore_result.status != 0
- name: Show restore output
ansible.builtin.debug:
var: restic_restore_result.container.Output
always:
- name: Ensure the application is started back up
ansible.builtin.systemd:
name: app
state: started