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.7 KiB
YAML
55 lines
1.7 KiB
YAML
---
|
|
# The docker volume backup image utilizes these images
|
|
- name: Pull ubuntu
|
|
docker_image:
|
|
name: ubuntu
|
|
source: pull
|
|
- name: Pull busybox
|
|
docker_image:
|
|
name: busybox
|
|
source: pull
|
|
- name: Pull docker-volume-backup
|
|
docker_image:
|
|
name: {{docker_volume_backup_image}}
|
|
source: pull
|
|
|
|
# https://stackoverflow.com/questions/45237632/ansible-w-docker-show-current-container-state
|
|
- name: Get container info
|
|
docker_container_info:
|
|
name: "{{container_name}}"
|
|
register: result
|
|
# We find the volumes
|
|
- name: Find volumes
|
|
ansible.builtin.shell: docker run --rm
|
|
-v /var/run/docker.sock:/var/run/docker.sock
|
|
-v "{{backup_dir}}:{{backup_dir}}"
|
|
"{{docker_volume_backup_image}}"
|
|
list-backups
|
|
--host-path "{{backup_dir}}"
|
|
--volume-name-filter {{container_name}}
|
|
--newest-only
|
|
register: out
|
|
|
|
- name: Stop the container
|
|
# not out.stdout is search("null") occurs when there is no volume in the backups dir
|
|
when: result.exists and not out.stdout is search("null")
|
|
docker_container:
|
|
name: "{{container_name}}"
|
|
state: stopped
|
|
- name: Restore Volume
|
|
ansible.builtin.command: docker run --rm
|
|
-v /var/run/docker.sock:/var/run/docker.sock
|
|
{{docker_volume_backup_image}}
|
|
restore-volume
|
|
--volume {{item.volumeName}}
|
|
--archive {{item.absoluteFilePath}}
|
|
with_items: "{{ out.stdout }}"
|
|
# the output will be "null" if there are no backups. We simply skip if this is the case
|
|
when: not out.stdout is search("null")
|
|
- name: Start the container
|
|
# not out.stdout is search("null") occurs when there is no volume in the backups dir
|
|
when: result.exists and not out.stdout is search("null")
|
|
docker_container:
|
|
name: "{{container_name}}"
|
|
state: started
|