From 846496d618cd3fd8b850c4bd0cc8e8664ade736a Mon Sep 17 00:00:00 2001 From: Bradley Bickford Date: Wed, 20 Aug 2025 17:45:57 -0400 Subject: [PATCH] More backup stuff --- inventories/core_infrastructure.yml | 2 ++ playbooks/backup_protocol.yml | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/inventories/core_infrastructure.yml b/inventories/core_infrastructure.yml index 62226ef..ded1cc8 100644 --- a/inventories/core_infrastructure.yml +++ b/inventories/core_infrastructure.yml @@ -35,6 +35,7 @@ all: 3331636339356236330a333330373264306163393162386330393763613332376261373433303035 65633034366439343232356639346665326234666630633063616437376131396463 backup_location: "/backup/pfsense" + backup_number_to_keep: 10 boardwalk: ansible_host: "10.77.7.2" backup_url: "10.77.7.2" @@ -47,6 +48,7 @@ all: 6566663062336566320a323536303233393431363263313933643839303435356266656136343438 35306638356564333962656433323735656136386130373233393765616265306636 backup_location: "/backup/pfsense" + backup_number_to_keep: 10 children: docker_nodes: hosts: diff --git a/playbooks/backup_protocol.yml b/playbooks/backup_protocol.yml index 7a16b49..a5d6d01 100644 --- a/playbooks/backup_protocol.yml +++ b/playbooks/backup_protocol.yml @@ -82,6 +82,27 @@ dest: "{{ backup_location }}/{{ inventory_hostname }}_{{ now().strftime('%Y%m%d%H%M%S') }}.xml" changed_when: false no_log: "{{ pfsense_backup_disable_logging | default(false) }}" + + - name: Find all PFSense backups for the current host + ansible.builtin.find: + paths: "{{ backup_location }}" + patterns: "{{ inventory_hostname }}*" + register: all_pfsense_backups + + - name: If too many backups kept + when: all_pfsense_backups | length > backup_number_to_keep + block: + - name: Get the oldest file paths + ansible.builtin.set_fact: + oldest_file_paths: >- + {{ (all_pfsense_backups.files | sort(attribute='mtime'))[:all_pfsense_backups.files | length - backup_number_to_keep] | + map(attribute=path) | list }} + + - name: Remove the files + ansible.builtin.file: + path: "{{ item }}" + state: absent + loop: "{{ oldest_file_paths }}" - name: Docker Infrastructure Compose Backups hosts: docker_nodes