Files
ansible/strato-production.yaml
2024-02-24 11:58:30 +01:00

108 lines
4.0 KiB
YAML

---
# This playbook backups the odroid docker containers.
- name: Backup Strato Production
hosts: all
gather_facts: true
tasks:
- name: Backup strato-production
block:
- include_vars: vars/mattermost.yaml
- include_vars: vars/strato-production.yaml
- name: Create backup directory
file:
path: "{{ backup_dir }}"
state: directory
- name: Create customer directories
file:
path: "{{ backup_dir }}/{{ item.key }}"
state: directory
loop: "{{ lookup('dict', customers) }}"
loop_control:
label: "{{ item.key }}"
- name: Backup web directories
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
loop: "{{ lookup('dict', customers) }}"
loop_control:
label: "{{ item.key }}"
- name: Fetch web backups
fetch:
src: "{{ backup_dir }}/{{ item.key }}/web-{{ ansible_date_time.iso8601_basic_short }}.tar.gz"
dest: "{{ local_backup }}/{{ item.key }}/"
flat: yes
when: item.value.storage is defined
loop: "{{ lookup('dict', customers) }}"
loop_control:
label: "{{ item.key }}"
- name: Backup mysql databases
shell: docker exec {{ item }}-db mysqldump -u{{ item }} -p{{ lookup('vars', item ~ '-password') }} {{ item }} > {{ backup_dir }}/{{ item }}/db-{{ ansible_date_time.iso8601_basic_short }}.sql
loop: "{{ mysql_databases }}"
- name: Compress database backup files
shell: gzip {{ backup_dir }}/{{ item }}/db-{{ ansible_date_time.iso8601_basic_short }}.sql
loop: "{{ databases }}"
- name: Fetch database backups
fetch:
src: "{{ backup_dir }}/{{ item.key }}/db-{{ ansible_date_time.iso8601_basic_short }}.sql.gz"
dest: "{{ local_backup }}/{{ item.key }}/"
flat: yes
loop: "{{ lookup('dict', customers) }}"
loop_control:
label: "{{ item.key }}"
- name: Send Email
community.general.mail:
host: mail.steinle-computer.de
port: 465
from: kontakt@steinle-computer.de (Christian Steinle)
username: kontakt@steinle-computer.de
password: "{{ mail_password }}"
to: "{{ item.value.email }}"
subject: Datenbank Backup
body: Backup der Anwendung wurde erstellt.
attach:
- "{{ backup_dir }}/{{ item.key }}/db-{{ ansible_date_time.iso8601_basic_short }}.sql.gz"
when: item.value.email is defined
loop: "{{ lookup('dict', customers) }}"
loop_control:
label: "{{ item.key }}"
- name: Clean backup directory
file:
path: "{{ backup_dir }}"
state: absent
- name: Inform Mattermost about success
uri:
url: "{{ mattermost_url }}"
method: POST
body_format: json
body:
channel_id: "{{ channel_id }}"
message: "{{ ansible_date_time.date }} {{ ansible_date_time.time }}: Playbook ran successful ({{ ansible_play_name }})"
headers:
Content-Type: application/json
Authorization: "Bearer {{ semaphore_token }}"
status_code: [200, 201]
rescue:
- name: Inform Mattermost about error
uri:
url: "{{ mattermost_url }}"
method: POST
body_format: json
body:
channel_id: "{{ channel_id }}"
message: "{{ ansible_date_time.date }} {{ ansible_date_time.time }}: Playbook ran with error ({{ ansible_play_name }})"
headers:
Content-Type: application/json
Authorization: "Bearer {{ error_token }}"
status_code: [200, 201]