From dfc0dbbffa13430f4bb938be52c9d49f85af22af Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Sat, 4 Nov 2023 14:36:55 +0100 Subject: [PATCH] Implemented cleanup procedure for failed backups --- roles/backups-consumer/handlers/main.yml | 3 +-- roles/backups-consumer/meta/main.yml | 1 + .../templates/pull-remote-backups.service.j2 | 2 +- roles/docker-volume-backup-cleanup/README.md | 3 +++ .../handlers/main.yml | 5 +++++ .../docker-volume-backup-cleanup/meta/main.yml | 3 +++ .../tasks/main.yml | 18 ++++++++++++++++++ .../docker-volume-backup-cleanup.service.j2 | 7 +++++++ .../docker-volume-backup-cleanup/vars/main.yml | 1 + roles/docker-volume-backup/handlers/main.yml | 4 ++-- roles/docker-volume-backup/meta/main.yml | 1 + .../templates/docker-volume-backup.service.j2 | 2 +- 12 files changed, 44 insertions(+), 6 deletions(-) create mode 100644 roles/docker-volume-backup-cleanup/README.md create mode 100644 roles/docker-volume-backup-cleanup/handlers/main.yml create mode 100644 roles/docker-volume-backup-cleanup/meta/main.yml create mode 100644 roles/docker-volume-backup-cleanup/tasks/main.yml create mode 100644 roles/docker-volume-backup-cleanup/templates/docker-volume-backup-cleanup.service.j2 create mode 100644 roles/docker-volume-backup-cleanup/vars/main.yml diff --git a/roles/backups-consumer/handlers/main.yml b/roles/backups-consumer/handlers/main.yml index 40b514e7..06ec2607 100644 --- a/roles/backups-consumer/handlers/main.yml +++ b/roles/backups-consumer/handlers/main.yml @@ -1,12 +1,11 @@ - name: "reload pull-remote-backups service" systemd: name: pull-remote-backups.service - state: reloaded enabled: yes daemon_reload: yes - name: "restart pull-remote-backups timer" systemd: name: pull-remote-backups.timer - state: restarted + state: started enabled: yes daemon_reload: yes diff --git a/roles/backups-consumer/meta/main.yml b/roles/backups-consumer/meta/main.yml index d92407dc..9117c4cc 100644 --- a/roles/backups-consumer/meta/main.yml +++ b/roles/backups-consumer/meta/main.yml @@ -2,3 +2,4 @@ dependencies: - git - systemd_notifier - backups-cleanup-timer + - docker-volume-backup-cleanup diff --git a/roles/backups-consumer/templates/pull-remote-backups.service.j2 b/roles/backups-consumer/templates/pull-remote-backups.service.j2 index e64a86d4..3b091a46 100644 --- a/roles/backups-consumer/templates/pull-remote-backups.service.j2 +++ b/roles/backups-consumer/templates/pull-remote-backups.service.j2 @@ -1,6 +1,6 @@ [Unit] Description=pull remote backups -OnFailure=systemd-notifier@%n.service +OnFailure=systemd-notifier@%n.service docker-volume-backup-cleanup.service [Service] Type=oneshot diff --git a/roles/docker-volume-backup-cleanup/README.md b/roles/docker-volume-backup-cleanup/README.md new file mode 100644 index 00000000..949b269e --- /dev/null +++ b/roles/docker-volume-backup-cleanup/README.md @@ -0,0 +1,3 @@ +# Docker Volume Backup Cleanup +This script cleans up failed docker backups. +It uses https://github.com/kevinveenbirkenbach/docker-volume-backup-cleanup as base. \ No newline at end of file diff --git a/roles/docker-volume-backup-cleanup/handlers/main.yml b/roles/docker-volume-backup-cleanup/handlers/main.yml new file mode 100644 index 00000000..367266d2 --- /dev/null +++ b/roles/docker-volume-backup-cleanup/handlers/main.yml @@ -0,0 +1,5 @@ +- name: "reload docker-volume-backup-cleanup.service daemon" + systemd: + name: docker-volume-backup-cleanup.service + enabled: yes + daemon_reload: yes diff --git a/roles/docker-volume-backup-cleanup/meta/main.yml b/roles/docker-volume-backup-cleanup/meta/main.yml new file mode 100644 index 00000000..c4fbe772 --- /dev/null +++ b/roles/docker-volume-backup-cleanup/meta/main.yml @@ -0,0 +1,3 @@ +dependencies: + - git + - systemd_notifier diff --git a/roles/docker-volume-backup-cleanup/tasks/main.yml b/roles/docker-volume-backup-cleanup/tasks/main.yml new file mode 100644 index 00000000..33f8ac10 --- /dev/null +++ b/roles/docker-volume-backup-cleanup/tasks/main.yml @@ -0,0 +1,18 @@ +- name: pull docker-volume-backup-cleanup.git + git: + repo: "https://github.com/kevinveenbirkenbach/docker-volume-backup-cleanup.git" + dest: "{{docker_volume_backup_cleanup_folder}}" + update: yes + register: git_result + ignore_errors: true + +- name: Warn if repo is not reachable + debug: + msg: "Warning: Repository is not reachable." + when: git_result.failed + +- name: configure docker-volume-backup-cleanup.service + template: + src: docker-volume-backup-cleanup.service.j2 + dest: /etc/systemd/system/docker-volume-backup-cleanup.service + notify: reload docker-volume-backup-cleanup.service daemon \ No newline at end of file diff --git a/roles/docker-volume-backup-cleanup/templates/docker-volume-backup-cleanup.service.j2 b/roles/docker-volume-backup-cleanup/templates/docker-volume-backup-cleanup.service.j2 new file mode 100644 index 00000000..f069f5a7 --- /dev/null +++ b/roles/docker-volume-backup-cleanup/templates/docker-volume-backup-cleanup.service.j2 @@ -0,0 +1,7 @@ +[Unit] +Description=Cleans up failed docker volume backups +OnFailure=systemd-notifier@%n.service + +[Service] +Type=oneshot +ExecStart=/bin/sh -c '/usr/bin/yes | /usr/bin/bash {{docker_volume_backup_cleanup_folder}}cleanup.sh {{docker_volume_backup_cleanup_machine_id}} {{docker_volume_backup_cleanup_trigger_directory}}' \ No newline at end of file diff --git a/roles/docker-volume-backup-cleanup/vars/main.yml b/roles/docker-volume-backup-cleanup/vars/main.yml new file mode 100644 index 00000000..68b958e8 --- /dev/null +++ b/roles/docker-volume-backup-cleanup/vars/main.yml @@ -0,0 +1 @@ +docker_volume_backup_cleanup_folder: "{{path_administrator_scripts}}docker-volume-backup-cleanup/" \ No newline at end of file diff --git a/roles/docker-volume-backup/handlers/main.yml b/roles/docker-volume-backup/handlers/main.yml index 2c442dd3..217414e2 100644 --- a/roles/docker-volume-backup/handlers/main.yml +++ b/roles/docker-volume-backup/handlers/main.yml @@ -1,12 +1,12 @@ - name: "reload docker-volume-backup.service" systemd: name: docker-volume-backup.service - state: reloaded enabled: yes daemon_reload: yes + - name: "restart docker-volume-backup.timer" systemd: name: docker-volume-backup.timer - state: restarted + state: started enabled: yes daemon_reload: yes diff --git a/roles/docker-volume-backup/meta/main.yml b/roles/docker-volume-backup/meta/main.yml index b83c5c92..36e85f8f 100644 --- a/roles/docker-volume-backup/meta/main.yml +++ b/roles/docker-volume-backup/meta/main.yml @@ -2,3 +2,4 @@ dependencies: - git - backups-provider - systemd_notifier + - docker-volume-backup-cleanup \ No newline at end of file diff --git a/roles/docker-volume-backup/templates/docker-volume-backup.service.j2 b/roles/docker-volume-backup/templates/docker-volume-backup.service.j2 index 342207ea..f5a82176 100644 --- a/roles/docker-volume-backup/templates/docker-volume-backup.service.j2 +++ b/roles/docker-volume-backup/templates/docker-volume-backup.service.j2 @@ -1,6 +1,6 @@ [Unit] Description=docker volume backup -OnFailure=systemd-notifier@%n.service +OnFailure=systemd-notifier@%n.service docker-volume-backup-cleanup.service [Service] Type=oneshot