duplicity_playbooks/backup_ovh1.yml

52 lines
2.1 KiB
YAML
Executable File

---
- name: backup_ovh1
hosts: localhost
gather_facts: false
vars_files: main.yml
tasks:
- name: Assert environment variable is not empty
ansible.builtin.include_tasks: "tasks/assert_env_var_not_empty.yml"
with_items: "{{ BACKUP_OVH1_REQUIRED_ENV_VARS }}"
- name: Create backup directory
ansible.builtin.file:
path: "{{ WORKDIR }}/backup"
state: directory
- name: Archive volumes
ansible.builtin.command: "tar -czf {{ WORKDIR }}/backup/{{ item }}.tar.gz -C /mnt/volumes {{ item }}"
with_items: "{{ BACKUP_OVH1_VOLUMES }}"
- name: Find lastest MySQL DB dump
ansible.builtin.shell: "ls -tr /mnt/volumes/mysql-server_dumps/data/mysql_dump-mysql_*"
register: MySQL_dump
- name: Find lastest applications DB dump
ansible.builtin.shell: "ls -tr /mnt/volumes/mysql-server_dumps/data/mysql_dump_*"
register: DBs_dump
- debug: var=MySQL_dump
- debug: var=DBs_dump
- ansible.builtin.meta: end_play
- name: Archive DB dumps
ansible.builtin.command: "tar -czf {{ WORKDIR }}/backup/mysql-server_dumps.tar.gz -C /mnt/volumes ${name_mysql_dump} ${name_dbs_dump}"
with_items: "{{ BACKUP_OVH1_VOLUMES }}"
#name_mysql_dump=$(ls -tr ${name}/data/mysql_dump-mysql_*|tail -n1)
#name_dbs_dump=$(ls -tr ${name}/data/mysql_dump_*|tail -n1)
# python3-swiftclient is a requirement of duplicity
- name: Install python3-swiftclient
ansible.builtin.package:
name: python3-swiftclient
state: present
- name: Backup with duplicity
ansible.builtin.command: "duplicity --num-retries 3 --full-if-older-than 1M --progress --archive-dir {{ ARCHIVE_DIR }} --name {{ lookup('env','BACKUP_WORKFLOW') }} --allow-source-mismatch '{{ WORKDIR }}/backup' swift://{{ lookup('env','BACKUP_WORKFLOW') }}"
environment: "{{ DUPLICITY_ENVIRONMENT }}"
- name: Clean old duplicity backups
ansible.builtin.command: "duplicity remove-older-than 2M --archive-dir {{ ARCHIVE_DIR }} --name {{ lookup('env','BACKUP_WORKFLOW') }} --allow-source-mismatch --force swift://{{ lookup('env','BACKUP_WORKFLOW') }}"
environment: "{{ DUPLICITY_ENVIRONMENT }}"