Extend odroid playbook to find old backups of remote servers.

This commit is contained in:
chsteinle
2021-12-21 08:10:26 +01:00
parent ef50bcbc43
commit 6ce464fc90

View File

@@ -11,190 +11,190 @@
- include_vars: vars/odroid/secret.yaml - include_vars: vars/odroid/secret.yaml
- include_vars: vars/odroid/variable.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 # - 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 }} # 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: "{{ lookup('dict', volumes) }}"
loop_control: # loop_control:
label: "{{ item.key }}" # label: "{{ item.key }}"
#
- name: Change ownership of volume backups # - name: Change ownership of volume backups
file: # file:
path: "{{ backup_dir }}/{{ item.value.container }}/{{ item.key }}-{{ ansible_date_time.iso8601_basic_short }}.tar.gz" # path: "{{ backup_dir }}/{{ item.value.container }}/{{ item.key }}-{{ ansible_date_time.iso8601_basic_short }}.tar.gz"
owner: chris # owner: chris
group: chris # group: chris
become: yes # become: yes
become_method: sudo # become_method: sudo
become_user: root # become_user: root
loop: "{{ lookup('dict', volumes) }}" # loop: "{{ lookup('dict', volumes) }}"
loop_control: # loop_control:
label: "{{ item.key }}" # label: "{{ item.key }}"
#
- name: Backup mysql databases # - 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 # 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 }}" # loop: "{{ mysql_databases }}"
#
- name: Backup postgres 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 # shell: docker exec psql14 pg_dump -U {{ postgres_user }} {{ item }} > {{ backup_dir }}/{{ item }}/db-{{ ansible_date_time.iso8601_basic_short }}.sql
loop: "{{ postgres_databases }}" # loop: "{{ postgres_databases }}"
#
- name: Compress database backup files # - name: Compress database backup files
shell: gzip {{ backup_dir }}/{{ item }}/db-{{ ansible_date_time.iso8601_basic_short }}.sql # shell: gzip {{ backup_dir }}/{{ item }}/db-{{ ansible_date_time.iso8601_basic_short }}.sql
loop: "{{ databases }}" # loop: "{{ databases }}"
#
- name: Check logrotate directories # - name: Check logrotate directories
stat: # stat:
path: "{{ backup_dir }}/{{ item[0] }}/{{ item[1] }}" # path: "{{ backup_dir }}/{{ item[0] }}/{{ item[1] }}"
register: subdirs # register: subdirs
with_nested: # with_nested:
- "{{ systems }}" # - "{{ systems }}"
- ['daily', 'weekly', 'monthly', 'yearly'] # - ['daily', 'weekly', 'monthly', 'yearly']
loop_control: # loop_control:
label: "{{ item[0] }} {{ item[1] }}" # label: "{{ item[0] }} {{ item[1] }}"
#
- name: Create not existing logrotate directories # - name: Create not existing logrotate directories
file: # file:
path: "{{ backup_dir }}/{{ item.item[0] }}/{{ item.item[1] }}" # path: "{{ backup_dir }}/{{ item.item[0] }}/{{ item.item[1] }}"
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: "{{ subdirs.results }}" # with_items: "{{ subdirs.results }}"
loop_control: # loop_control:
label: "{{ item.item[0] }} {{ item.item[1] }}" # label: "{{ item.item[0] }} {{ item.item[1] }}"
#
- name: Search for the created backups # - name: Search for the created backups
find: # find:
paths: "{{ backup_dir }}/{{ item }}" # paths: "{{ backup_dir }}/{{ item }}"
file_type: file # file_type: file
patterns: '*.gz' # patterns: '*.gz'
recurse: false # recurse: false
register: backup_files # register: backup_files
loop: "{{ systems }}" # loop: "{{ systems }}"
#
- name: Store the yearly backups # - name: Store the yearly backups
copy: # copy:
remote_src: true # remote_src: true
src: "{{ item.path }}" # src: "{{ item.path }}"
dest: "{{ item.path | dirname }}/yearly/{{ item.path | basename }}" # dest: "{{ item.path | dirname }}/yearly/{{ item.path | basename }}"
with_items: "{{ backup_files.results | map(attribute='files') | list }}" # with_items: "{{ backup_files.results | map(attribute='files') | list }}"
when: (ansible_date_time.day == "01" and ansible_date_time.month == "01") # when: (ansible_date_time.day == "01" and ansible_date_time.month == "01")
loop_control: # loop_control:
label: "{{ item.path }}" # label: "{{ item.path }}"
#
- name: Store the monthly backups # - name: Store the monthly backups
copy: # copy:
remote_src: true # remote_src: true
src: "{{ item.path }}" # src: "{{ item.path }}"
dest: "{{ item.path | dirname }}/monthly/{{ item.path | basename }}" # dest: "{{ item.path | dirname }}/monthly/{{ item.path | basename }}"
with_items: "{{ backup_files.results | map(attribute='files') | list }}" # with_items: "{{ backup_files.results | map(attribute='files') | list }}"
when: (ansible_date_time.day == "01" and ansible_date_time.month != "01") # when: (ansible_date_time.day == "01" and ansible_date_time.month != "01")
loop_control: # loop_control:
label: "{{ item.path }}" # label: "{{ item.path }}"
#
- name: Store the weekly backups # - name: Store the weekly backups
copy: # copy:
remote_src: true # remote_src: true
src: "{{ item.path }}" # src: "{{ item.path }}"
dest: "{{ item.path | dirname }}/weekly/{{ item.path | basename }}" # dest: "{{ item.path | dirname }}/weekly/{{ item.path | basename }}"
with_items: "{{ backup_files.results | map(attribute='files') | list }}" # with_items: "{{ backup_files.results | map(attribute='files') | list }}"
when: (ansible_date_time.weekday_number == "1") # when: (ansible_date_time.weekday_number == "1")
loop_control: # loop_control:
label: "{{ item.path }}" # label: "{{ item.path }}"
#
- name: Store the daily backup # - name: Store the daily backup
copy: # copy:
remote_src: true # remote_src: true
src: "{{ item.path }}" # src: "{{ item.path }}"
dest: "{{ item.path | dirname }}/daily/{{ item.path | basename }}" # dest: "{{ item.path | dirname }}/daily/{{ item.path | basename }}"
with_items: "{{ backup_files.results | map(attribute='files') | list }}" # with_items: "{{ backup_files.results | map(attribute='files') | list }}"
when: (ansible_date_time.weekday_number != "1" and ansible_date_time.day != "01") # when: (ansible_date_time.weekday_number != "1" and ansible_date_time.day != "01")
loop_control: # loop_control:
label: "{{ item.path }}" # label: "{{ item.path }}"
#
- name: Cleanup original backup files # - 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: Find old daily backups # - name: Find old daily backups
find: # find:
paths: "{{ backup_dir }}/{{ item }}/daily" # paths: "{{ backup_dir }}/{{ item }}/daily"
file_type: file # file_type: file
age: 8d # age: 8d
age_stamp: ctime # age_stamp: ctime
patterns: '*.gz' # patterns: '*.gz'
recurse: false # recurse: false
register: backup_files # register: backup_files
loop: "{{ systems }}" # loop: "{{ systems }}"
#
- name: Delete old daily backups # - name: Delete old daily backups
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: Find old weekly backups # - name: Find old weekly backups
find: # find:
paths: "{{ backup_dir }}/{{ item }}/weekly" # paths: "{{ backup_dir }}/{{ item }}/weekly"
file_type: file # file_type: file
age: 5w # age: 5w
age_stamp: ctime # age_stamp: ctime
patterns: '*.gz' # patterns: '*.gz'
recurse: false # recurse: false
register: backup_files # register: backup_files
loop: "{{ systems }}" # loop: "{{ systems }}"
#
- name: Delete old weekly backups # - name: Delete old weekly backups
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: Find old monthly backups # - name: Find old monthly backups
find: # find:
paths: "{{ backup_dir }}/{{ item }}/monthly" # paths: "{{ backup_dir }}/{{ item }}/monthly"
file_type: file # file_type: file
age: 56w # age: 56w
age_stamp: ctime # age_stamp: ctime
patterns: '*.gz' # patterns: '*.gz'
recurse: false # recurse: false
register: backup_files # register: backup_files
loop: "{{ systems }}" # loop: "{{ systems }}"
#
- name: Delete old monthly backups # - name: Delete old monthly backups
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: Create nexcloud data backup
# shell: rdiff-backup /var/lib/docker/volumes/nextcloud-data/_data /media/backup/docker/nextcloud/data # shell: rdiff-backup /var/lib/docker/volumes/nextcloud-data/_data /media/backup/docker/nextcloud/data
# become: yes # become: yes
@@ -210,7 +210,7 @@
loop: "{{ servers }}" loop: "{{ servers }}"
- name: Find old daily backups - name: Find old daily backups
file: find:
path: "{{ item.path }}/daily" path: "{{ item.path }}/daily"
file_type: file file_type: file
age: 8d age: 8d