140 lines
4.9 KiB
YAML
140 lines
4.9 KiB
YAML
---
|
|
- name: Centralized Restic Fleet Restore (Latest Snapshot)
|
|
hosts: all
|
|
become: true
|
|
connection: local
|
|
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:
|
|
confirm_restore: false
|
|
ansible_ssh_common_args: '-o StrictHostKeyChecking=no'
|
|
# Defaults to "latest" per host+tag. Override with -e "restore_snapshot_id=abcd1234"
|
|
# to restore a specific snapshot instead. Applied to every target in the loop.
|
|
restore_snapshot_id: "latest"
|
|
|
|
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/
|
|
|
|
restore_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: Refuse to proceed without explicit confirmation
|
|
ansible.builtin.assert:
|
|
that:
|
|
- confirm_restore | bool
|
|
fail_msg: >-
|
|
This playbook will overwrite backup_targets paths on {{ inventory_hostname }}
|
|
with data from restic snapshots. 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 this host matches no known group
|
|
ansible.builtin.assert:
|
|
that:
|
|
- restore_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: List available snapshots (per target, for the log)
|
|
community.docker.docker_container:
|
|
name: "restic_snapshots_list_{{ item.label }}"
|
|
image: restic/restic:latest
|
|
state: started
|
|
detach: false
|
|
cleanup: true
|
|
output_logs: true
|
|
command: >-
|
|
snapshots
|
|
--host "{{ inventory_hostname }}"
|
|
--tag "{{ item.label }}"
|
|
env: *restic_env
|
|
register: restic_snapshots_result
|
|
loop: "{{ restore_targets }}"
|
|
loop_control:
|
|
label: "{{ item.label }}"
|
|
|
|
- name: Show available snapshots
|
|
ansible.builtin.debug:
|
|
var: restic_snapshots_result.results
|
|
|
|
- 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: "{{ item.path }}"
|
|
state: directory
|
|
owner: docker
|
|
group: docker
|
|
mode: "0755"
|
|
loop: "{{ restore_targets }}"
|
|
loop_control:
|
|
label: "{{ item.label }}"
|
|
|
|
- name: Check for and remove stale locks (per target)
|
|
community.docker.docker_container:
|
|
name: "restic_unlock_{{ item.label }}"
|
|
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
|
|
loop: "{{ restore_targets }}"
|
|
loop_control:
|
|
label: "{{ item.label }}"
|
|
|
|
- name: Restore latest snapshot into place (per target)
|
|
community.docker.docker_container:
|
|
name: "restic_restore_{{ item.label }}"
|
|
image: restic/restic:latest
|
|
state: started
|
|
detach: false
|
|
cleanup: true
|
|
timeout: 600
|
|
output_logs: true
|
|
command: >-
|
|
restore {{ restore_snapshot_id }}
|
|
--host "{{ inventory_hostname }}"
|
|
--tag "{{ item.label }}"
|
|
--target /
|
|
volumes:
|
|
# NOTE: rw (no :ro) here — this is the whole point of a restore.
|
|
- "{{ item.path }}:/backup_target/{{ item.label }}"
|
|
env: *restic_env
|
|
register: restic_restore_result
|
|
failed_when: restic_restore_result.status != 0
|
|
loop: "{{ restore_targets }}"
|
|
loop_control:
|
|
label: "{{ item.label }}"
|
|
|
|
- name: Show restore output
|
|
ansible.builtin.debug:
|
|
var: restic_restore_result.results
|
|
always:
|
|
- name: Ensure the application is started back up
|
|
ansible.builtin.systemd:
|
|
name: app
|
|
state: started |