Introduce notification for other playbooks.

This commit is contained in:
2021-11-23 15:38:00 +01:00
parent 9a2e5afe01
commit 3efcd0797c
2 changed files with 264 additions and 226 deletions

View File

@@ -5,61 +5,80 @@
gather_facts: true gather_facts: true
tasks: tasks:
- include_vars: vars/hetzner/secret.yaml - name: Backup hetzner
- include_vars: vars/hetzner/variable.yaml block:
- include_vars: vars/hetzner/secret.yaml
- include_vars: vars/hetzner/variable.yaml
- name: Create backup directory - name: Create backup directory
file: file:
path: "{{ backup_dir }}" path: "{{ backup_dir }}"
state: directory state: directory
- name: Create customer directories - name: Create customer directories
file: file:
path: "{{ backup_dir }}/{{ item.key }}" path: "{{ backup_dir }}/{{ item.key }}"
state: directory state: directory
loop: "{{ lookup('dict', customers) }}" loop: "{{ lookup('dict', customers) }}"
loop_control: loop_control:
label: "{{ item.key }}" label: "{{ item.key }}"
- name: Backup web directories - name: Backup web directories
shell: tar cvfz {{ backup_dir }}/{{ item.key }}/web-{{ ansible_date_time.iso8601_basic_short }}.tar.gz {{ item. value.storage }} shell: tar cvfz {{ backup_dir }}/{{ item.key }}/web-{{ ansible_date_time.iso8601_basic_short }}.tar.gz {{ item. value.storage }}
when: item.value.storage is defined when: item.value.storage is defined
loop: "{{ lookup('dict', customers) }}" loop: "{{ lookup('dict', customers) }}"
loop_control: loop_control:
label: "{{ item.key }}" label: "{{ item.key }}"
- name: Fetch web backups - name: Fetch web backups
fetch: fetch:
src: "{{ backup_dir }}/{{ item.key }}/web-{{ ansible_date_time.iso8601_basic_short }}.tar.gz" src: "{{ backup_dir }}/{{ item.key }}/web-{{ ansible_date_time.iso8601_basic_short }}.tar.gz"
dest: "{{ local_backup }}/{{ item.key }}/" dest: "{{ local_backup }}/{{ item.key }}/"
flat: yes flat: yes
when: item.value.storage is defined when: item.value.storage is defined
loop: "{{ lookup('dict', customers) }}" loop: "{{ lookup('dict', customers) }}"
loop_control: loop_control:
label: "{{ item.key }}" label: "{{ item.key }}"
- name: Backup databases - name: Backup databases
shell: mysqldump -u{{ mysql_user }} -p{{ mysql_password }} {{ item.value.database }} > {{ backup_dir }}/{{ item.key }}/db-{{ ansible_date_time.iso8601_basic_short }}.sql shell: mysqldump -u{{ mysql_user }} -p{{ mysql_password }} {{ item.value.database }} > {{ backup_dir }}/{{ item.key }}/db-{{ ansible_date_time.iso8601_basic_short }}.sql
loop: "{{ lookup('dict', customers) }}" loop: "{{ lookup('dict', customers) }}"
loop_control: loop_control:
label: "{{ item.key }}" label: "{{ item.key }}"
- name: Compress database backups - name: Compress database backups
shell: gzip {{ backup_dir }}/{{ item.key }}/db-{{ ansible_date_time.iso8601_basic_short }}.sql shell: gzip {{ backup_dir }}/{{ item.key }}/db-{{ ansible_date_time.iso8601_basic_short }}.sql
loop: "{{ lookup('dict', customers) }}" loop: "{{ lookup('dict', customers) }}"
loop_control: loop_control:
label: "{{ item.key }}" label: "{{ item.key }}"
- name: Fetch database backups - name: Fetch database backups
fetch: fetch:
src: "{{ backup_dir }}/{{ item.key }}/db-{{ ansible_date_time.iso8601_basic_short }}.sql.gz" src: "{{ backup_dir }}/{{ item.key }}/db-{{ ansible_date_time.iso8601_basic_short }}.sql.gz"
dest: "{{ local_backup }}/{{ item.key }}/" dest: "{{ local_backup }}/{{ item.key }}/"
flat: yes flat: yes
loop: "{{ lookup('dict', customers) }}" loop: "{{ lookup('dict', customers) }}"
loop_control: loop_control:
label: "{{ item.key }}" label: "{{ item.key }}"
- name: Clean backup directory - name: Clean backup directory
file: file:
path: "{{ backup_dir }}" path: "{{ backup_dir }}"
state: absent state: absent
- name: Inform Mattermost about success
uri:
url: https://cs-mm.ddnss.de/hooks/og4ckuqet7fafb8kjuaigzuh7c
method: POST
body_format: json
body:
text: "{{ ansible_date_time.date }} {{ ansible_date_time.time }} (info): Playbook ran successful ({{ ansible_play_name }})"
rescue:
- name: Inform Mattermost about error
uri:
url: https://cs-mm.ddnss.de/hooks/og4ckuqet7fafb8kjuaigzuh7c
method: POST
body_format: json
body:
text: "{{ ansible_date_time.date }} {{ ansible_date_time.time }} (info): Playbook ran with error ({{ ansible_play_name }})"

View File

@@ -5,195 +5,214 @@
gather_facts: true gather_facts: true
tasks: tasks:
- include_vars: vars/odroid/secret.yaml - name: Backup odroid
- include_vars: vars/odroid/variable.yaml block:
- include_vars: vars/odroid/secret.yaml
- include_vars: vars/odroid/variable.yaml
- name: Check if directories exist - name: Check if directories exist
stat: stat:
path: "{{ backup_dir }}/{{ item }}" path: "{{ backup_dir }}/{{ item }}"
register: dirs register: dirs
loop: "{{ systems }}" loop: "{{ systems }}"
- name: Create not existing directories - name: Create not existing directories
file: file:
path: "{{ backup_dir }}/{{ item.item }}" path: "{{ backup_dir }}/{{ item.item }}"
state: directory state: directory
mode: 0755 mode: 0755
group: chris group: chris
owner: chris owner: chris
when: item.stat.exists == false when: item.stat.exists == false
with_items: "{{ dirs.results }}" with_items: "{{ dirs.results }}"
loop_control: loop_control:
label: "{{ item.item }}" label: "{{ item.item }}"
- name: Backup container volumes
shell: docker run --rm -v {{ backup_dir }}/{{ item.value.container }}:/backup --volumes-from {{ item.value.container }} busybox tar cvfz /backup/{{ item.key }}-{{ ansible_date_time.iso8601_basic_short }}.tar.gz {{ item.value.path }}
loop: "{{ lookup('dict', volumes) }}"
loop_control:
label: "{{ item.key }}"
- name: Change ownership of volume backups - name: Backup container volumes
file: shell: docker run --rm -v {{ backup_dir }}/{{ item.value.container }}:/backup --volumes-from {{ item.value.container }} busybox tar cvfz /backup/{{ item.key }}-{{ ansible_date_time.iso8601_basic_short }}.tar.gz {{ item.value.path }}
path: "{{ backup_dir }}/{{ item.value.container }}/{{ item.key }}-{{ ansible_date_time.iso8601_basic_short }}.tar.gz" loop: "{{ lookup('dict', volumes) }}"
owner: chris loop_control:
group: chris label: "{{ item.key }}"
become: yes
become_method: sudo
become_user: root
loop: "{{ lookup('dict', volumes) }}"
loop_control:
label: "{{ item.key }}"
- name: Backup mysql databases - name: Change ownership of volume backups
shell: docker exec mysql8 mysqldump -u{{ mysql_user }} -p{{ mysql_password }} {{ item }} > {{ backup_dir }}/{{ item }}/db-{{ ansible_date_time.iso8601_basic_short }}.sql file:
loop: "{{ mysql_databases }}" path: "{{ backup_dir }}/{{ item.value.container }}/{{ item.key }}-{{ ansible_date_time.iso8601_basic_short }}.tar.gz"
owner: chris
- name: Backup postgres databases group: chris
shell: docker exec psql14 pg_dump -U {{ postgres_user }} {{ item }} > {{ backup_dir }}/{{ item }}/db-{{ ansible_date_time.iso8601_basic_short }}.sql become: yes
loop: "{{ postgres_databases }}" become_method: sudo
become_user: root
- name: Compress database backup files loop: "{{ lookup('dict', volumes) }}"
shell: gzip {{ backup_dir }}/{{ item }}/db-{{ ansible_date_time.iso8601_basic_short }}.sql loop_control:
loop: "{{ databases }}" label: "{{ item.key }}"
- name: Check logrotate directories - name: Backup mysql databases
stat: shell: docker exec mysql8 mysqldump -u{{ mysql_user }} -p{{ mysql_password }} {{ item }} > {{ backup_dir }}/{{ item }}/db-{{ ansible_date_time.iso8601_basic_short }}.sql
path: "{{ backup_dir }}/{{ item[0] }}/{{ item[1] }}" loop: "{{ mysql_databases }}"
register: subdirs
with_nested:
- "{{ systems }}"
- ['daily', 'weekly', 'monthly', 'yearly']
loop_control:
label: "{{ item[0] }} {{ item[1] }}"
- name: Create not existing logrotate directories - name: Backup postgres databases
file: shell: docker exec psql14 pg_dump -U {{ postgres_user }} {{ item }} > {{ backup_dir }}/{{ item }}/db-{{ ansible_date_time.iso8601_basic_short }}.sql
path: "{{ backup_dir }}/{{ item.item[0] }}/{{ item.item[1] }}" loop: "{{ postgres_databases }}"
state: directory
mode: 0755
group: chris
owner: chris
when: item.stat.exists == false
with_items: "{{ subdirs.results }}"
loop_control:
label: "{{ item.item[0] }} {{ item.item[1] }}"
- name: Search for the created backups - name: Compress database backup files
find: shell: gzip {{ backup_dir }}/{{ item }}/db-{{ ansible_date_time.iso8601_basic_short }}.sql
paths: "{{ backup_dir }}/{{ item }}" loop: "{{ databases }}"
file_type: file
patterns: '*.gz'
recurse: false
register: backup_files
loop: "{{ systems }}"
- name: Store the yearly backups - name: Check logrotate directories
copy: stat:
remote_src: true path: "{{ backup_dir }}/{{ item[0] }}/{{ item[1] }}"
src: "{{ item.path }}" register: subdirs
dest: "{{ item.path | dirname }}/yearly/{{ item.path | basename }}" with_nested:
with_items: "{{ backup_files.results | map(attribute='files') | list }}" - "{{ systems }}"
when: (ansible_date_time.day == "01" and ansible_data_time.month == "01") - ['daily', 'weekly', 'monthly', 'yearly']
loop_control: loop_control:
label: "{{ item.path }}" label: "{{ item[0] }} {{ item[1] }}"
- 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_data_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 - name: Create not existing logrotate directories
copy: file:
remote_src: true path: "{{ backup_dir }}/{{ item.item[0] }}/{{ item.item[1] }}"
src: "{{ item.path }}" state: directory
dest: "{{ item.path | dirname }}/daily/{{ item.path | basename }}" mode: 0755
with_items: "{{ backup_files.results | map(attribute='files') | list }}" group: chris
when: (ansible_date_time.weekday_number != "1" and ansible_date_time.day != "01") owner: chris
loop_control: when: item.stat.exists == false
label: "{{ item.path }}" with_items: "{{ subdirs.results }}"
loop_control:
- name: Cleanup original backup files label: "{{ item.item[0] }} {{ item.item[1] }}"
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 - name: Search for the created backups
find: find:
paths: "{{ backup_dir }}/{{ item }}/daily" paths: "{{ backup_dir }}/{{ item }}"
file_type: file file_type: file
age: 8d patterns: '*.gz'
age_stamp: ctime recurse: false
patterns: '*.gz' register: backup_files
recurse: false loop: "{{ systems }}"
register: backup_files
loop: "{{ systems }}"
- name: Delete old daily backups - name: Store the yearly backups
file: copy:
path: "{{ item.path }}" remote_src: true
state: absent src: "{{ item.path }}"
with_items: "{{ backup_files.results | map(attribute='files') | list }}" dest: "{{ item.path | dirname }}/yearly/{{ item.path | basename }}"
loop_control: with_items: "{{ backup_files.results | map(attribute='files') | list }}"
label: "{{ item.path }}" when: (ansible_date_time.day == "01" and ansible_data_time.month == "01")
loop_control:
label: "{{ item.path }}"
- name: Find old weekly backups - name: Store the monthly backups
find: copy:
paths: "{{ backup_dir }}/{{ item }}/weekly" remote_src: true
file_type: file src: "{{ item.path }}"
age: 5w dest: "{{ item.path | dirname }}/monthly/{{ item.path | basename }}"
age_stamp: ctime with_items: "{{ backup_files.results | map(attribute='files') | list }}"
patterns: '*.gz' when: (ansible_date_time.day == "01" and ansible_data_time.month != "01")
recurse: false loop_control:
register: backup_files label: "{{ item.path }}"
loop: "{{ systems }}"
- name: Delete old weekly backups - name: Store the weekly backups
file: copy:
path: "{{ item.path }}" remote_src: true
state: absent src: "{{ item.path }}"
with_items: "{{ backup_files.results | map(attribute='files') | list }}" dest: "{{ item.path | dirname }}/weekly/{{ item.path | basename }}"
loop_control: with_items: "{{ backup_files.results | map(attribute='files') | list }}"
label: "{{ item.path }}" when: (ansible_date_time.weekday_number == "1")
loop_control:
label: "{{ item.path }}"
- name: Find old monthly backups - name: Store the daily backup
find: copy:
paths: "{{ backup_dir }}/{{ item }}/monthly" remote_src: true
file_type: file src: "{{ item.path }}"
age: 56w dest: "{{ item.path | dirname }}/daily/{{ item.path | basename }}"
age_stamp: ctime with_items: "{{ backup_files.results | map(attribute='files') | list }}"
patterns: '*.gz' when: (ansible_date_time.weekday_number != "1" and ansible_date_time.day != "01")
recurse: false loop_control:
register: backup_files label: "{{ item.path }}"
loop: "{{ systems }}"
- name: Delete old monthly backups - name: Cleanup original backup files
file: file:
path: "{{ item.path }}" path: "{{ item.path }}"
state: absent state: absent
with_items: "{{ backup_files.results | map(attribute='files') | list }}" with_items: "{{ backup_files.results | map(attribute='files') | list }}"
loop_control: loop_control:
label: "{{ item.path }}" label: "{{ item.path }}"
- name: Create nexcloud data backup - name: Find old daily backups
shell: rdiff-backup /var/lib/docker/volumes/nextcloud-data/_data /media/backup/docker/nextcloud/data find:
become: yes paths: "{{ backup_dir }}/{{ item }}/daily"
become_method: sudo file_type: file
become_user: root age: 8d
age_stamp: ctime
patterns: '*.gz'
recurse: false
register: backup_files
loop: "{{ systems }}"
- 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: Create nexcloud data backup
shell: rdiff-backup /var/lib/docker/volumes/nextcloud-data/_data /media/backup/docker/nextcloud/data
become: yes
become_method: sudo
become_user: root
- name: Inform Mattermost about success
uri:
url: https://cs-mm.ddnss.de/hooks/og4ckuqet7fafb8kjuaigzuh7c
method: POST
body_format: json
body:
text: "{{ ansible_date_time.date }} {{ ansible_date_time.time }} (info): Playbook ran successful ({{ ansible_play_name }})"
rescue:
- name: Inform Mattermost about error
uri:
url: https://cs-mm.ddnss.de/hooks/og4ckuqet7fafb8kjuaigzuh7c
method: POST
body_format: json
body:
text: "{{ ansible_date_time.date }} {{ ansible_date_time.time }} (info): Playbook ran with error ({{ ansible_play_name }})"