205 lines
6.6 KiB
YAML
205 lines
6.6 KiB
YAML
---
|
|
# This playbook cleans up its own backup directory.
|
|
- name: Cleanup semaphore
|
|
hosts: all
|
|
gather_facts: true
|
|
|
|
tasks:
|
|
- name: Cleanup semaphore
|
|
block:
|
|
- include_vars: vars/common/variable.yaml
|
|
- include_vars: vars/semaphore/variable.yaml
|
|
|
|
- name: Search for volume's backup directories
|
|
become: yes
|
|
find:
|
|
paths: "{{ local_backup }}"
|
|
file_type: directory
|
|
recurse: false
|
|
register: backup_dirs
|
|
|
|
- name: Copy backup files
|
|
become: yes
|
|
copy:
|
|
remote_src: true
|
|
src: "{{ item.path }}"
|
|
dest: "{{ backup_dir }}"
|
|
loop: "{{ backup_dirs.files }}"
|
|
loop_control:
|
|
label: "{{ item.path }}"
|
|
|
|
- name: Cleanup volume's backup directories
|
|
become: yes
|
|
file:
|
|
path: "{{ item.path }}"
|
|
state: absent
|
|
loop: "{{ backup_dirs.files }}"
|
|
loop_control:
|
|
label: "{{ item.path }}"
|
|
|
|
- name: Correct directory owner
|
|
become: yes
|
|
file:
|
|
path: "{{ backup_dir }}"
|
|
owner: chris
|
|
group: chris
|
|
recurse: yes
|
|
|
|
- name: Register new backup directories
|
|
find:
|
|
paths: "{{ backup_dir }}"
|
|
file_type: directory
|
|
recurse: false
|
|
register: backup_dirs
|
|
|
|
- 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: 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: 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: 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 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 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: Find old daily backups
|
|
find:
|
|
paths: "{{ item.path | dirname }}/daily"
|
|
file_type: file
|
|
age: 8d
|
|
age_stamp: ctime
|
|
patterns: '*.gz'
|
|
recurse: false
|
|
register: old_files
|
|
with_items: "{{ backup_files.results | map(attribute='files') | list }}"
|
|
loop_control:
|
|
label: "{{ item.path }}"
|
|
|
|
- name: Print old files
|
|
debug:
|
|
var: old_files
|
|
|
|
# - name: Delete old daily backups
|
|
# file:
|
|
# path: "{{ item.path }}"
|
|
# state: absent
|
|
# with_items: "{{ backup_files.results | map(attribute='files') | list }}"
|
|
# loop_control:
|
|
# label: "{{ item.path }}"
|
|
#
|
|
# - name: Find old weekly backups
|
|
# find:
|
|
# paths: "{{ backup_dir }}/{{ item }}/weekly"
|
|
# file_type: file
|
|
# age: 5w
|
|
# age_stamp: ctime
|
|
# patterns: '*.gz'
|
|
# recurse: false
|
|
# register: backup_files
|
|
# loop: "{{ systems }}"
|
|
#
|
|
# - name: Delete old weekly backups
|
|
# file:
|
|
# path: "{{ item.path }}"
|
|
# state: absent
|
|
# with_items: "{{ backup_files.results | map(attribute='files') | list }}"
|
|
# loop_control:
|
|
# label: "{{ item.path }}"
|
|
#
|
|
# - name: Find old monthly backups
|
|
# find:
|
|
# paths: "{{ backup_dir }}/{{ item }}/monthly"
|
|
# file_type: file
|
|
# age: 56w
|
|
# age_stamp: ctime
|
|
# patterns: '*.gz'
|
|
# recurse: false
|
|
# register: backup_files
|
|
# loop: "{{ systems }}"
|
|
#
|
|
# - name: Delete old monthly backups
|
|
# file:
|
|
# path: "{{ item.path }}"
|
|
# state: absent
|
|
# with_items: "{{ backup_files.results | map(attribute='files') | list }}"
|
|
# loop_control:
|
|
# label: "{{ item.path }}"
|
|
|
|
- name: Inform Mattermost about success
|
|
uri:
|
|
url: "{{ mattermost_url }}"
|
|
method: POST
|
|
body_format: json
|
|
body:
|
|
username: "{{ mattermost_user }}"
|
|
text: "{{ ansible_date_time.date }} {{ ansible_date_time.time }} (info): Playbook ran successful ({{ ansible_play_name }})"
|
|
|
|
rescue:
|
|
- name: Inform Mattermost about error
|
|
uri:
|
|
url: "{{ mattermost_url }}"
|
|
method: POST
|
|
body_format: json
|
|
body:
|
|
username: "{{ mattermost_user }}"
|
|
text: "{{ ansible_date_time.date }} {{ ansible_date_time.time }} (info): Playbook ran with error ({{ ansible_play_name }})"
|