From 400fa843891d301b081bb948cf416c972be2282e Mon Sep 17 00:00:00 2001 From: patrick Date: Mon, 27 Jul 2026 14:33:04 -0400 Subject: [PATCH] reduce backup downtime: restart before rsync transfer --- galaxy.yml | 1 + roles/app/README.md | 7 +++++++ roles/app/meta/main.yml | 1 + roles/app/tasks/backup.yml | 12 +++++++++--- 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/galaxy.yml b/galaxy.yml index cf43faf..3674e4c 100644 --- a/galaxy.yml +++ b/galaxy.yml @@ -14,5 +14,6 @@ tags: - containers dependencies: "community.docker": ">=1.10.0" + "ansible.posix": ">=1.4.0" repository: https://git.jaroszew.ski/ansible/ansible-collection-docker-compose-app issues: https://git.jaroszew.ski/ansible/ansible-collection-docker-compose-app/issues diff --git a/roles/app/README.md b/roles/app/README.md index 2a905b9..9f48f87 100644 --- a/roles/app/README.md +++ b/roles/app/README.md @@ -6,6 +6,11 @@ A flexible Ansible role for deploying and managing Docker Compose applications w This role provides a standardized way to deploy containerized applications using Docker Compose. It handles directory creation, template rendering, and container lifecycle management. +## Requirements + +- Collections: `community.docker` (>=1.10.0) and `ansible.posix` (>=1.4.0). Both are declared as collection dependencies and installed automatically with the collection. +- The backup workflow copies archives to the controller with `ansible.posix.synchronize`, so **`rsync` must be installed on both the controller and the managed host**. + ## Features - **Template-based deployment** with Jinja2 templating @@ -120,6 +125,8 @@ dependencies: app_backup_stop_services: true # Stop containers during backup ``` +When `app_backup_stop_services` is enabled the containers are stopped only while the tarball is created on the managed host, then restarted immediately — the transfer of the archive back to the controller (via rsync) happens after the application is running again, so downtime is limited to the snapshot itself. + #### Automated Backups For scheduled backups: diff --git a/roles/app/meta/main.yml b/roles/app/meta/main.yml index 2a3f39a..47b1bce 100644 --- a/roles/app/meta/main.yml +++ b/roles/app/meta/main.yml @@ -7,3 +7,4 @@ galaxy_info: collections: - community.docker + - ansible.posix diff --git a/roles/app/tasks/backup.yml b/roles/app/tasks/backup.yml index 43d99b4..9692542 100644 --- a/roles/app/tasks/backup.yml +++ b/roles/app/tasks/backup.yml @@ -51,11 +51,17 @@ mode: "{{ app_permission_mode | default('0644') }}" register: remote_backup + - name: Restart application after snapshot + community.docker.docker_compose_v2: + project_src: "{{ app_dir }}" + state: present + when: app_backup_stop_services | default(true) + - name: Copy backup to controller - ansible.builtin.fetch: + ansible.posix.synchronize: src: "{{ remote_backup.dest }}" dest: "{{ app_backup_path }}/{{ app_name }}-{{ ansible_date_time.iso8601_basic_short }}.tar.gz" - flat: true + mode: pull when: remote_backup is succeeded - name: Clean old backups on controller @@ -87,7 +93,7 @@ loop: "{{ old_backups_remote.files | default([]) }}" always: - - name: Restart application after backup + - name: Ensure application is running (safety net) community.docker.docker_compose_v2: project_src: "{{ app_dir }}" state: present