diff --git a/hetzner.yaml b/hetzner.yaml index 3ad5f22..e38022d 100644 --- a/hetzner.yaml +++ b/hetzner.yaml @@ -5,61 +5,80 @@ gather_facts: true tasks: - - include_vars: vars/hetzner/secret.yaml - - include_vars: vars/hetzner/variable.yaml + - name: Backup hetzner + block: + - include_vars: vars/hetzner/secret.yaml + - include_vars: vars/hetzner/variable.yaml - - name: Create backup directory - file: - path: "{{ backup_dir }}" - state: directory + - 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: 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: 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: 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 databases - 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_control: - label: "{{ item.key }}" + - 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 + loop: "{{ lookup('dict', customers) }}" + loop_control: + label: "{{ item.key }}" - - name: Compress database backups - shell: gzip {{ backup_dir }}/{{ item.key }}/db-{{ ansible_date_time.iso8601_basic_short }}.sql - loop: "{{ lookup('dict', customers) }}" - loop_control: - label: "{{ item.key }}" + - name: Compress database backups + shell: gzip {{ backup_dir }}/{{ item.key }}/db-{{ ansible_date_time.iso8601_basic_short }}.sql + loop: "{{ lookup('dict', customers) }}" + loop_control: + label: "{{ item.key }}" - - 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: 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: Clean backup directory - file: - path: "{{ backup_dir }}" - state: absent + - name: Clean backup directory + file: + path: "{{ backup_dir }}" + 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 }})" diff --git a/odroid.yaml b/odroid.yaml index 2609992..85e0478 100644 --- a/odroid.yaml +++ b/odroid.yaml @@ -5,195 +5,214 @@ gather_facts: true tasks: - - include_vars: vars/odroid/secret.yaml - - include_vars: vars/odroid/variable.yaml + - name: Backup odroid + block: + - include_vars: vars/odroid/secret.yaml + - include_vars: vars/odroid/variable.yaml - - name: Check if directories exist - stat: - path: "{{ backup_dir }}/{{ item }}" - register: dirs - loop: "{{ systems }}" + - name: Check if directories exist + stat: + path: "{{ backup_dir }}/{{ item }}" + register: dirs + loop: "{{ systems }}" - - name: Create not existing directories - file: - path: "{{ backup_dir }}/{{ item.item }}" - state: directory - mode: 0755 - group: chris - owner: chris - when: item.stat.exists == false - with_items: "{{ dirs.results }}" - loop_control: - 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: Create not existing directories + file: + path: "{{ backup_dir }}/{{ item.item }}" + state: directory + mode: 0755 + group: chris + owner: chris + when: item.stat.exists == false + with_items: "{{ dirs.results }}" + loop_control: + label: "{{ item.item }}" - - name: Change ownership of volume backups - file: - path: "{{ backup_dir }}/{{ item.value.container }}/{{ item.key }}-{{ ansible_date_time.iso8601_basic_short }}.tar.gz" - owner: chris - group: chris - become: yes - become_method: sudo - become_user: root - loop: "{{ lookup('dict', volumes) }}" - loop_control: - label: "{{ item.key }}" + - 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: Backup mysql databases - shell: docker exec mysql8 mysqldump -u{{ mysql_user }} -p{{ mysql_password }} {{ item }} > {{ backup_dir }}/{{ item }}/db-{{ ansible_date_time.iso8601_basic_short }}.sql - loop: "{{ mysql_databases }}" - - - name: Backup postgres databases - shell: docker exec psql14 pg_dump -U {{ postgres_user }} {{ item }} > {{ backup_dir }}/{{ item }}/db-{{ ansible_date_time.iso8601_basic_short }}.sql - loop: "{{ postgres_databases }}" - - - name: Compress database backup files - shell: gzip {{ backup_dir }}/{{ item }}/db-{{ ansible_date_time.iso8601_basic_short }}.sql - loop: "{{ databases }}" + - name: Change ownership of volume backups + file: + path: "{{ backup_dir }}/{{ item.value.container }}/{{ item.key }}-{{ ansible_date_time.iso8601_basic_short }}.tar.gz" + owner: chris + group: chris + become: yes + become_method: sudo + become_user: root + loop: "{{ lookup('dict', volumes) }}" + loop_control: + label: "{{ item.key }}" - - name: Check logrotate directories - stat: - path: "{{ backup_dir }}/{{ item[0] }}/{{ item[1] }}" - register: subdirs - with_nested: - - "{{ systems }}" - - ['daily', 'weekly', 'monthly', 'yearly'] - loop_control: - label: "{{ item[0] }} {{ item[1] }}" + - name: Backup mysql databases + shell: docker exec mysql8 mysqldump -u{{ mysql_user }} -p{{ mysql_password }} {{ item }} > {{ backup_dir }}/{{ item }}/db-{{ ansible_date_time.iso8601_basic_short }}.sql + loop: "{{ mysql_databases }}" - - name: Create not existing logrotate directories - file: - path: "{{ backup_dir }}/{{ item.item[0] }}/{{ item.item[1] }}" - 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: Backup postgres databases + shell: docker exec psql14 pg_dump -U {{ postgres_user }} {{ item }} > {{ backup_dir }}/{{ item }}/db-{{ ansible_date_time.iso8601_basic_short }}.sql + loop: "{{ postgres_databases }}" - - name: Search for the created backups - find: - paths: "{{ backup_dir }}/{{ item }}" - file_type: file - patterns: '*.gz' - recurse: false - register: backup_files - loop: "{{ systems }}" + - name: Compress database backup files + shell: gzip {{ backup_dir }}/{{ item }}/db-{{ ansible_date_time.iso8601_basic_short }}.sql + loop: "{{ databases }}" - - 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_data_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_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: Check logrotate directories + stat: + path: "{{ backup_dir }}/{{ item[0] }}/{{ item[1] }}" + register: subdirs + with_nested: + - "{{ systems }}" + - ['daily', 'weekly', 'monthly', 'yearly'] + loop_control: + label: "{{ item[0] }} {{ item[1] }}" - - 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: Create not existing logrotate directories + file: + path: "{{ backup_dir }}/{{ item.item[0] }}/{{ item.item[1] }}" + 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: Find old daily backups - find: - paths: "{{ backup_dir }}/{{ item }}/daily" - file_type: file - age: 8d - age_stamp: ctime - patterns: '*.gz' - recurse: false - register: backup_files - loop: "{{ systems }}" + - name: Search for the created backups + find: + paths: "{{ backup_dir }}/{{ item }}" + file_type: file + 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: 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_data_time.month == "01") + 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: 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: 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: 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: 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: 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: 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: 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: 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: Find old daily backups + find: + paths: "{{ backup_dir }}/{{ item }}/daily" + file_type: file + 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 }})"