move run-backups into playbook dir and update some syntax errors in setup-lxc
This commit is contained in:
129
playbooks/run-backups.yml
Normal file
129
playbooks/run-backups.yml
Normal file
@@ -0,0 +1,129 @@
|
||||
---
|
||||
- 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
|
||||
@@ -5,7 +5,7 @@
|
||||
gather_facts: true
|
||||
|
||||
|
||||
tasks:
|
||||
tasks:
|
||||
- name: Upgrade-packages.yml - update packages
|
||||
ansible.builtin.import_tasks: ./upgrade-packages.yml
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
|
||||
- name: Setup-restic.yml - setup restic
|
||||
ansible.builtin.import_tasks: ./setup-restic.yml
|
||||
|
||||
|
||||
- name: Check if /apps/docker exists
|
||||
ansible.builtin.stat:
|
||||
path: "/apps/docker"
|
||||
@@ -37,25 +37,29 @@
|
||||
-e confirm_restore=true
|
||||
delegate_to: localhost
|
||||
when: not apps_docker_dir.stat.exists or not apps_data_dir.stat.exists
|
||||
|
||||
when: not app_data.stat.exists or not app_data.stat.isdir
|
||||
|
||||
- name: Copy docker-compose.yml from repo
|
||||
ansible.builtin.copy:
|
||||
src: ./{{ ansible_hostname }}/docker-compose.yml
|
||||
dest: /apps/docker/docker-compose.yml
|
||||
owner: docker
|
||||
group: docker
|
||||
mode: '0644'
|
||||
|
||||
- name: Copy docker-compose.env from repo
|
||||
ansible.builtin.copy:
|
||||
src: ./{{ ansible_hostname }}/docker-compose.env
|
||||
dest: /apps/docker/docker-compose.env
|
||||
owner: docker
|
||||
group: docker
|
||||
mode: '0644'
|
||||
|
||||
- name: Reload systemd daemon
|
||||
systemd:
|
||||
daemon_reload: yes
|
||||
daemon_reload: true
|
||||
|
||||
- name: Start App Service
|
||||
systemd:
|
||||
name: app
|
||||
state: started
|
||||
enabled: yes
|
||||
enabled: true
|
||||
|
||||
Reference in New Issue
Block a user