Build the backup rotation playbook.

This commit is contained in:
2025-04-25 15:25:25 +02:00
parent 7cc6abd942
commit f0ba691ff8

View File

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