aboutsummaryrefslogtreecommitdiff
path: root/roles/mysql_backup/tasks/main.yml
blob: 7ce7748080dbc6aafc69e388ed632f9c0aefac2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
---
- name: Check for BORG
  command: which borg
  register: borg_check
  ignore_errors: yes

# Kinda hacky but saves time
- name: Install BORG
  apt:
    name:
      - borgbackup
  become: yes
  when: not borg_check.rc == 0

- name: Create BORG repo
  file:
    path: '{{ sql_backup.borg_repo_dir }}'
    state: directory
    mode: 'u=rwx,g=,o='
  become: yes

- name: Initialize BORG repo
  command:
    cmd: borg init --encryption=repokey
    creates: '{{ sql_backup.borg_repo_dir }}/config'
  environment:
    BORG_REPO: '{{ sql_backup.borg_repo_dir }}'
    BORG_PASSPHRASE: '{{ sql_backup.borg_repo_key }}'
  become: yes

- name: Create tempdir
  file:
    path: /tmp/sql
    state: directory
    owner: server
    group: server
    mode: 'u=rwx,g=,o='
  become: yes

- name: Dump databases
  community.mysql.mysql_db:
    state: dump
    name: '{{ item }}'
    target: '/tmp/sql/{{ item }}.sql'
    login_unix_socket: /var/run/mysqld/mysqld.sock
  loop: '{{ dbs }}'
  become: yes

- name: Create BORG backup
  command: 'borg create --compression lz4 --verbose ::{hostname}-{now} /tmp/sql'
  environment:
    BORG_REPO: '{{ sql_backup.borg_repo_dir }}'
    BORG_PASSPHRASE: '{{ sql_backup.borg_repo_key }}'
  register: borg_output
  become: yes

- name: Borg Output
  debug:
    var: borg_output.stderr

- name: Delete TEMP files
  file:
    path: /tmp/sql
    state: absent
  become: yes

- name: Prune BORG backup
  command: 'borg prune --list
    --keep-last {{ sql_backup.keep_last }}
    --keep-daily {{ sql_backup.keep_daily }}
    --keep-weekly {{ sql_backup.keep_weekly }}
    --keep-monthly {{ sql_backup.keep_monthly }}'
  environment:
    BORG_REPO: '{{ sql_backup.borg_repo_dir }}'
    BORG_PASSPHRASE: '{{ sql_backup.borg_repo_key }}'
  register: borg_prune
  become: yes

- name: Prune Output
  debug:
    var: borg_prune.stderr