backups: archive volumes > copy to localhost > rm remote > extract in local

This commit is contained in:
2025-08-20 22:14:41 +10:00
parent 4f682e449f
commit 064b6dd7b5
3 changed files with 41 additions and 13 deletions

View File

@@ -1,17 +1,40 @@
---
- name: Backup Docker volumes from remote server
hosts: all
# become: yes
tasks:
- name: Ensure local backup directory exists
local_action: file
path=./backups
state=directory
mode=0755
become: yes
vars:
backup_dir: "./backups"
backup_name: "volumes_{{ ansible_date_time.iso8601_basic_short }}"
archive_path: "/tmp/docker_volumes_backup_{{ ansible_date_time.iso8601_basic_short }}.tar.gz"
- name: Synchronize /var/lib/docker/volumes to local ./backups
synchronize:
src: /var/lib/docker/volumes
tasks:
- name: Create archive of docker volumes
become: yes
archive:
path: /opt/docker/.
dest: /tmp/{{ backup_name }}.tar.gz
format: gz
- name: Fetch archive to local machine
become: yes
fetch:
src: /tmp/{{ backup_name }}.tar.gz
dest: ./backups/archive/
flat: yes
- name: Clean up archive on remote
become: yes
file:
path: /tmp/{{ backup_name }}.tar.gz
state: absent
- name: Extract archive locally
become: no
delegate_to: localhost
run_once: true
ansible.builtin.unarchive:
src: ./backups/archive/{{ backup_name }}.tar.gz
dest: ./backups/
mode: pull
delete: no
remote_src: no
# extra_opts: [ "--strip-components=3" ]