diff --git a/clean-backup.yaml b/clean-backup.yaml new file mode 100644 index 0000000..eceb570 --- /dev/null +++ b/clean-backup.yaml @@ -0,0 +1,98 @@ +--- +# This playbook removes ol backups +- name: Cleanup old backups + hosts: all + gather_facts: True + vars: + directories: + - /media/backup/docker + - /media/backup/strato-production + + tasks: + - name: Register all backup directories + find: + paths: "{{ item }}" + file_type: directory + recurse: false + register: backup_dirs + loop: "{{ directories }}" + + - name: Find old daily backups + find: + paths: "{{ item[1].path }}/daily" + file_type: file + age: 7d + age_stamp: mtime + recurse: false + register: backup_files + loop: "{{ backup_dirs.results | subelements('files') }}" + loop_control: + label: "{{ item[1].path }}" + + - name: Delete old daily backups + file: + path: "{{ item[1].path }}" + state: absent + loop: "{{ backup_files.results | subelements('files') }}" + loop_control: + label: "{{ item[1].path }}" + + - name: Find old weekly backups + find: + paths: "{{ item[1].path }}/weekly" + file_type: file + age: 4w + age_stamp: mtime + recurse: false + register: backup_files + loop: "{{ backup_dirs.results | subelements('files') }}" + loop_control: + label: "{{ item[1].path }}" + + - name: Delete old weekly backups + file: + path: "{{ item[1].path }}" + state: absent + loop: "{{ backup_files.results | subelements('files') }}" + loop_control: + label: "{{ item[1].path }}" + + - name: Find old monthly backups + find: + paths: "{{ item[1].path }}/monthly" + file_type: file + age: 48w + age_stamp: mtime + recurse: false + register: backup_files + loop: "{{ backup_dirs.results | subelements('files') }}" + loop_control: + label: "{{ item[1].path }}" + + - name: Delete old monthly backups + file: + path: "{{ item[1].path }}" + state: absent + loop: "{{ backup_files.results | subelements('files') }}" + loop_control: + label: "{{ item[1].path }}" + + - name: Find old yearly backups + find: + paths: "{{ item[1].path }}/yearly" + file_type: file + age: 156w + age_stamp: mtime + recurse: false + register: backup_files + loop: "{{ backup_dirs.results | subelements('files') }}" + loop_control: + label: "{{ item[1].path }}" + + - name: Delete old yearly backups + file: + path: "{{ item[1].path }}" + state: absent + loop: "{{ backup_files.results | subelements('files') }}" + loop_control: + label: "{{ item[1].path }}"