You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.9 KiB
YAML
55 lines
1.9 KiB
YAML
---
|
|
# tasks file for chatton.docker_backup
|
|
# https://docs.ansible.com/ansible/latest/collections/community/docker/docker_container_module.html#ansible-collections-community-docker-docker-container-module
|
|
# https://docs.docker.com/storage/volumes/#backup-restore-or-migrate-data-volumes
|
|
|
|
|
|
- set_fact: backup_time="{{ ansible_date_time.iso8601 }}"
|
|
|
|
- name: Stop a container
|
|
community.docker.docker_container:
|
|
name: "{{ container_backup }}"
|
|
state: stopped
|
|
|
|
- name: Get container details
|
|
docker_container_info:
|
|
name: "{{ container_backup }}"
|
|
register: result
|
|
|
|
- name: Extract only the volume mounts (not bind mounts)
|
|
set_fact: volume_mounts="{{ result.container.Mounts | selectattr("Type", "equalto", "volume")}}"
|
|
|
|
- name: Create Backup of Container Volumes
|
|
community.docker.docker_container:
|
|
name: "backup-container-{{ item.Name }}-{{ 10 | random }}"
|
|
image: ubuntu
|
|
command: "tar cvf /backups/{{ item.Name }}-{{ backup_time }}.tar.gz {{ item.Destination }}"
|
|
auto_remove: true
|
|
detach: false # block until this container exists.
|
|
state: started
|
|
volumes:
|
|
- /mnt/mergerfs/backups:/backups
|
|
volumes_from:
|
|
- "{{ container_backup }}"
|
|
with_items: "{{ volume_mounts }}"
|
|
|
|
- name: Start the container
|
|
community.docker.docker_container:
|
|
name: "{{ container_backup }}"
|
|
state: started
|
|
|
|
- name: Upload backups to S3
|
|
register: upload_result
|
|
amazon.aws.aws_s3:
|
|
s3_url: "https://{{aws_s3.s3_url}}"
|
|
bucket: "{{ aws_s3.bucket }}"
|
|
object: "{{ item.Name }}/{{ item.Name }}-{{ backup_time }}.tar.gz"
|
|
src: /mnt/mergerfs/backups/{{ item.Name }}-{{ backup_time }}.tar.gz
|
|
aws_access_key: "{{ aws_s3.aws_access_key }}"
|
|
aws_secret_key: "{{ aws_s3.aws_secret_key }}"
|
|
region: "{{ aws_s3.region }}"
|
|
mode: put
|
|
# empty permissions makes it work with IAM vs ACL, see: https://github.com/ansible/ansible/issues/48050
|
|
permission: []
|
|
with_items: "{{ volume_mounts }}"
|