The beginnings of a backup solution
This commit is contained in:
54
playbooks/roles/docker_backup/tasks/main.yml
Normal file
54
playbooks/roles/docker_backup/tasks/main.yml
Normal file
@@ -0,0 +1,54 @@
|
||||
---
|
||||
- name: Ensure backup rules contains the necessary fields
|
||||
ansible.builtin.fail:
|
||||
msg: "You must define {{ item }} in the backup ruleset"
|
||||
when: not backup_rules[item] is defined
|
||||
loop:
|
||||
- container_name
|
||||
- directories_to_backup
|
||||
- backup_dir
|
||||
- backup_name_prefix
|
||||
- max_backups_kept
|
||||
|
||||
- name: Stop the running container
|
||||
community.docker.docker_container:
|
||||
name: "{{ backup_rules.container_name }}"
|
||||
state: stopped
|
||||
|
||||
- name: Archive necessary directories
|
||||
community.general.archive:
|
||||
path: "{{ backup_rules.directories_to_backup }}"
|
||||
dest: >-
|
||||
{{ backup_rules.backup_dir }}/{{ backup_rules.backup_name_prefix }}_
|
||||
{{ now().strftime("%Y%m%d%H%M%S") }}.tar.gz
|
||||
format: gz
|
||||
async: 3600
|
||||
poll: 60
|
||||
|
||||
- name: Start the stopped container
|
||||
community.docker.docker_container:
|
||||
name: "{{ backup_rules.container_name }}"
|
||||
state: started
|
||||
|
||||
- name: Find all files that start with the backup_name_prefix
|
||||
ansible.builtin.find:
|
||||
paths: "{{ backup_rules.backup_dir }}"
|
||||
patterns: "{{ backup_rules.backup_name_prefix }}*"
|
||||
register: all_backup_files
|
||||
|
||||
- name: If too many backups kept
|
||||
when: all_backup_files.files | length > backup_rules.max_backups_kept
|
||||
block:
|
||||
- name: Get the oldest file paths
|
||||
ansible.builtin.set_fact:
|
||||
oldest_file_paths: >-
|
||||
{{ (all_backup_files.files | sort(attribute='mtime'))[:all_backup_files.files | length - backup_rules.max_backups_kept] |
|
||||
map(attribute=path) | list }}
|
||||
|
||||
- name: Remove the files
|
||||
ansible.builtin.file:
|
||||
path: "{{ item }}"
|
||||
state: absent
|
||||
loop: "{{ oldest_file_paths }}"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user