diff --git a/rotate-backup.yaml b/rotate-backup.yaml index aae4d96..1bce46c 100644 --- a/rotate-backup.yaml +++ b/rotate-backup.yaml @@ -1,82 +1,88 @@ - --- +--- # This playbook rotates backups -- name: Backup Strato Production +- name: Do backup rotation hosts: all - gather_facts: true + gather_facts: True + vars: + directories: + - /media/backup/docker + - /media/backup/strato-production tasks: - - name: Register new backup directories - find: - paths: "{{ backup_dir }}" - file_type: directory - recurse: false - register: backup_dirs + - name: Check directories variable + ansible.builtin.debug: + msg: "{{ item }}" + loop: "{{ directories }}" - - name: Check logrotate directories - file: - path: "{{ item[0].path }}/{{ item[1] }}" - state: directory - with_nested: - - "{{ backup_dirs.files }}" - - ['daily', 'weekly', 'monthly', 'yearly'] - loop_control: - label: "{{ item[0].path }} {{ item[1] }}" + - name: Register new backup directories + find: + paths: "{{ item }}" + file_type: directory + recurse: false + register: backup_dirs + loop: "{{ directories }}" - - name: Search for the created backups - find: - paths: "{{ item.path }}" - file_type: file - patterns: '*.gz' - recurse: false - register: backup_files - loop: "{{ backup_dirs.files }}" - loop_control: - label: "{{ item.path }}" + - name: Create the logrotate directories + file: + path: "{{ item[1].path }}/{{ item[2] }}" + state: directory + with_nested: + - "{{ backup_dirs.results | subelements('files') }}" + - ['daily', 'weekly', 'monthly', 'yearly'] + loop_control: + label: "{{ item[1].path }} {{ item[2] }}" - - name: Store the yearly backups - copy: - remote_src: true - src: "{{ item.path }}" - dest: "{{ item.path | dirname }}/yearly/{{ item.path | basename }}" - with_items: "{{ backup_files.results | map(attribute='files') | list }}" - when: (ansible_date_time.day == "01" and ansible_date_time.month == "01") - loop_control: - label: "{{ item.path }}" + - name: Search for new container backups + find: + paths: "{{ item[1].path }}" + file_type: file + patterns: '*.gz' + recurse: false + register: backup_files + loop: "{{ backup_dirs.results | subelements('files') }}" + loop_control: + label: "{{ item[1].path }}" - - name: Store the monthly backups - copy: - remote_src: true - src: "{{ item.path }}" - dest: "{{ item.path | dirname }}/monthly/{{ item.path | basename }}" - with_items: "{{ backup_files.results | map(attribute='files') | list }}" - when: (ansible_date_time.day == "01" and ansible_date_time.month != "01") - loop_control: - label: "{{ item.path }}" + - name: Store the yearly backups + copy: + remote_src: true + src: "{{ item[1].path }}" + dest: "{{ item[1].path | dirname }}/yearly/{{ item[1].path | basename }}" + loop: "{{ backup_files.results | subelements('files') if (ansible_date_time.day == '01' and ansible_date_time.month == '01') else [] }}" + loop_control: + label: "{{ item[1].path }}" - - name: Store the weekly backups - copy: - remote_src: true - src: "{{ item.path }}" - dest: "{{ item.path | dirname }}/weekly/{{ item.path | basename }}" - with_items: "{{ backup_files.results | map(attribute='files') | list }}" - when: (ansible_date_time.weekday_number == "1") - loop_control: - label: "{{ item.path }}" + - name: Store the monthly backups + copy: + remote_src: true + src: "{{ item[1].path }}" + dest: "{{ item[1].path | dirname }}/monthly/{{ item[1].path | basename }}" + loop: "{{ backup_files.results | subelements('files') if (ansible_date_time.day == '01' and ansible_date_time.month != '01') else [] }}" + loop_control: + label: "{{ item[1].path }}" - - name: Store the daily backup - copy: - remote_src: true - src: "{{ item.path }}" - dest: "{{ item.path | dirname }}/daily/{{ item.path | basename }}" - with_items: "{{ backup_files.results | map(attribute='files') | list }}" - when: (ansible_date_time.weekday_number != "1" and ansible_date_time.day != "01") - loop_control: - label: "{{ item.path }}" - - - name: Cleanup original backup files - file: - path: "{{ item.path }}" - state: absent - with_items: "{{ backup_files.results | map(attribute='files') | list }}" - loop_control: - label: "{{ item.path }}" + - name: Store the weekly backups + copy: + remote_src: true + src: "{{ item[1].path }}" + dest: "{{ item[1].path | dirname }}/weekly/{{ item[1].path | basename }}" + loop: "{{ backup_files.results | subelements('files') if (ansible_date_time.weekday_number == '1' and ansible_date_time.day != '01') else [] }}" + loop_control: + label: "{{ item[1].path }}" + + - name: Store the daily backup + copy: + remote_src: true + src: "{{ item[1].path }}" + dest: "{{ item[1].path | dirname }}/daily/{{ item[1].path | basename }}" + loop: "{{ backup_files.results | subelements('files') if (ansible_date_time.weekday_number != '1' and ansible_date_time.day != '01') else [] }}" + loop_control: + label: "{{ item[1].path }}" + + - name: Cleanup original backup files + file: + path: "{{ item[1].path }}" + state: absent + loop: "{{ backup_files.results | subelements('files') }}" + loop_control: + label: "{{ item[1].path }}"