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.
52 lines
1.9 KiB
YAML
52 lines
1.9 KiB
YAML
---
|
|
# 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
|
|
|
|
- name: Determine backup timestamp.
|
|
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 -czvf /backups/{{ item.Name }}-{{ backup_time }}.tar.gz /data"
|
|
auto_remove: true
|
|
detach: false # block until this container exists.
|
|
state: started
|
|
volumes:
|
|
- "{{ item.Name }}:/data"
|
|
- "{{ docker_s3_backup_host_backup_directory }}:/backups"
|
|
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: "{{ docker_s3_backup_aws_s3_url }}"
|
|
bucket: "{{ docker_s3_backup_aws_s3_bucket }}"
|
|
object: "{{ item.Name }}/{{ item.Name }}-{{ backup_time }}.tar.gz"
|
|
src: "{{ docker_s3_backup_host_backup_directory }}/{{ item.Name }}-{{ backup_time }}.tar.gz"
|
|
aws_access_key: "{{ docker_s3_backup_aws_s3_aws_access_key }}"
|
|
aws_secret_key: "{{ docker_s3_backup_aws_s3_aws_secret_key }}"
|
|
region: "{{ docker_s3_backup_aws_s3_region }}"
|
|
mode: put
|
|
permission: "{{ docker_s3_backup_aws_s3_permissions }}"
|
|
with_items: "{{ volume_mounts }}"
|