aboutsummaryrefslogtreecommitdiffstats
path: root/roles/restic_backups/tasks/main.yml
blob: 58ce67c1a2aa7ceba7488477e01894123d325d09 (plain) (blame)
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
- name: Check mandatory variables are defined
  assert:
    that:
      - restic_backups_b2_key_id is defined
      - restic_backups_b2_key_secret is defined
      - restic_backups_repository is defined
      - restic_backups_password is defined

- name: Install restic
  community.general.openbsd_pkg:
    name: "restic"
    state: present

- name: Check whether the restic repository exists
  ansible.builtin.command: restic cat config --no-cache
  environment: "{{ restic_env }}"
  register: restic_repo_check
  changed_when: false
  # From `restic cat config --help`
  # > Exit status is 0 if the command was successful.
  # > Exit status is 1 if there was any error.
  # > Exit status is 10 if the repository does not exist.
  # > Exit status is 11 if the repository is already locked.
  # > Exit status is 12 if the password is incorrect.
  failed_when: restic_repo_check.rc not in [0, 10]
  no_log: true

- name: Initialize the restic repository
  ansible.builtin.command: restic init --no-cache
  environment: "{{ restic_env }}"
  when: restic_repo_check.rc == 10
  no_log: true

- name: Create a 'backup' system user
  ansible.builtin.user:
    name: _backup
    create_home: false
    shell: /sbin/nologin
    login_class: daemon
    # /var/www/git is owned by git:git, so to backup that directory, we need
    # the backup user to be in the git group.
    groups: git
    append: yes

- name: Create restic cache directory
  ansible.builtin.file:
    path: "/var/cache/restic"
    state: directory
    owner: _backup
    group: wheel
    mode: "0750"

- name: Create restic env entries in the crontab
  ansible.builtin.cron:
    name: "{{ item.key }}"
    user: _backup
    env: yes
    job: "{{ item.value }}"
  loop: "{{ restic_env | dict2items }}"
  no_log: true

- name: Add backup job crontab entry
  ansible.builtin.cron:
    name: "backup /var/www/git to backblaze"
    minute: "0"
    hour: "3"
    user: _backup
    job: "restic backup /var/www/git --tag git"

- name: Add backup pruning job crontab entry
  ansible.builtin.cron:
    name: "prune /var/www/git backups in backblaze"
    minute: "0"
    hour: "4"
    weekday: "0"
    user: _backup
    job: "restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 6 --prune"