Files
Random_Ansible_Stuff/playbooks/backup_protocol.yml
T

93 lines
3.0 KiB
YAML

---
- name: Docker Infrastructure Compose Backups
hosts: docker_nodes
become: true
become_method: sudo
gather_facts: false
collections:
- community.docker
- community.general
- ansible.mysql
vars:
env_backups_to_keep: 10
tasks:
- name: "Log first stage start time"
ansible.builtin.debug:
msg: "{{ now() }}"
run_once: true
delegate_to: blacktide
- name: Run mount to ensure the /backup directory is present
ansible.builtin.shell:
cmd: "mount -a"
changed_when: false
- name: Run container mounts backup
ansible.builtin.include_role:
name: docker_backup
vars:
docker_backup_rules: "{{ item }}"
when: docker_backup is defined and docker_backup | length != 0
loop: "{{ docker_backup }}"
- name: Stat the /root/infrastructure-compose folder
ansible.builtin.stat:
path: "/root/infrastructure-compose"
register: infra_compose_stat
- name: Find all .env files
ansible.builtin.find:
paths: "/root/infrastructure-compose"
hidden: true
patterns: ".*.env"
when: infra_compose_stat.stat.exists
register: all_env_files
- name: .env Backup block
when: infra_compose_stat.stat.exists and all_env_files.files is defined and all_env_files.files | length != 0
block:
- name: Show file list
ansible.builtin.debug:
msg: "{{ all_env_files.files | map(attribute='path') | list }}"
- name: Archive .env files
community.general.archive:
path: "{{ all_env_files.files | map(attribute='path') | list }}"
dest: >-
/backup/{{ inventory_hostname }}_{{ now().strftime("%Y%m%d%H%M%S") }}.tar.gz
format: gz
force_archive: true
- name: Find all .env backup files for the current host
ansible.builtin.find:
paths: "/backup"
patterns: "{{ inventory_hostname }}*"
register: backup_env_files
- name: If too many backups kept
when: backup_env_files.files | length > env_backups_to_keep
block:
- name: Get the oldest file paths
ansible.builtin.set_fact:
oldest_file_paths: >-
{{ (backup_env_files.files | sort(attribute='mtime'))[:backup_env_files.files | length - env_backups_to_keep] |
map(attribute='path') | list }}
- name: Remove the files
ansible.builtin.file:
path: "{{ item }}"
state: absent
loop: "{{ oldest_file_paths }}"
- name: Trigger rclone backup
hosts: nfsserver
become: true
become_method: sudo
gather_facts: false
tasks:
- name: Run rclone
ansible.builtin.shell:
cmd: "rclone sync /backups dropbox:Backup/infrastructure --progress --dropbox-chunk-size 128M --dropbox-batch-mode sync --dropbox-batch-size 100 --tpslimit 12 --tpslimit-burst 0"
changed_when: false
async: 3600
poll: 60