--- # Version 1.1.0 # Encrypt passwords with: "ansible-vault encrypt_string" # Start playbook with: "ansible-playbook -i localhost, -u chsteinle -k playbook-yaml.yml --ask-vault-pass" - name: Docker backup hosts: all gather_facts: True tasks: - name: Create container backup directories file: path: "{{ backup_location }}{{ item.name }}" state: directory mode: 0755 loop: "{{ containers }}" loop_control: label: "{{ item.name }}" - name: Create backup of mysql database and store to backup directory shell: docker exec {{ item[1].container }} mysqldump -u{{ item[1].user }} -p{{ item[1].pass }} {{ item[1].name }} > {{ backup_location }}{{ item[0].name }}/{{ item[1].container }}-{{ ansible_date_time.iso8601_basic_short }}.sql when: item[1].type == "mysql" loop: "{{ containers | subelements('database', skip_missing=True) }}" loop_control: label: "{{ item[1].name }}" - name: Create backup of postgres database and store to backup directory shell: docker exec {{ item[1].container }} pg_dump -U {{ item[1].user }} {{ item[1].name }} > {{ backup_location }}{{ item[0].name }}/{{ item[1].container }}-{{ ansible_date_time.iso8601_basic_short }}.sql when: item[1].type == "psql" loop: "{{ containers | subelements('database', skip_missing=True) }}" loop_control: label: "{{ item[1].name }}" - name: Compress all database backup files shell: gzip {{ backup_location }}{{ item[0].name }}/{{ item[1].container }}-{{ ansible_date_time.iso8601_basic_short }}.sql loop: "{{ containers | subelements('database', skip_missing=True) }}" loop_control: label: "{{ item[1].name }}" - name: Create backup of volume and store to backup directory shell: tar cfz {{ backup_location }}{{ item[0].name }}/{{ item[1] }}-volume-{{ ansible_date_time.iso8601_basic_short }}.tar.gz -C /var/lib/docker/volumes/{{ item[1] }}/ _data become: yes become_method: sudo become_user: root loop: "{{ containers | subelements('volumes', skip_missing=True) }}" loop_control: label: "{{ item[1] }}" # - name: Change ownership of volume backups # file: # path: "{{ backup_location }}{{ item[0].name }}/{{ item[1] }}-volume-{{ ansible_date_time.iso8601_basic_short }}.tar.gz" # owner: chris # group: chris # become: yes # become_method: sudo # become_user: root # loop: "{{ containers | subelements('volumes', skip_missing=True) }}" # loop_control: # label: "{{ item[1] }}"