summaryrefslogtreecommitdiff
path: root/roles/storage/zfs/syncoid/tasks/main.yml
blob: 738b704181c1d427fbc7e96bd283f1389fa92b33 (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
---
- name: install sanoid package
  apt:
    name: sanoid
    state: present

- name: create syncoid directory
  file:
    path: /var/lib/syncoid
    state: directory
    mode: 0700

- name: genarate ssh keypair for syncoid
  openssh_keypair:
    path: /var/lib/syncoid/id_ssh_ed25519
    type: ed25519
    comment: ZFS Backup syncoid@{{ host_name }}

- name: generate syncoid ssh config wrapper
  copy:
    content: |
      #!/bin/bash
      exec /usr/sbin/syncoid --sshoption "UserKnownHostsFile=/var/lib/syncoid/ssh.knownhosts" --sshoption "HashKnownHosts=no" --sshkey "/var/lib/syncoid/id_ssh_ed25519" --no-sync-snap --compress zstd-fast "$@"
    dest: /var/lib/syncoid/syncoid_wrapper
    mode: 0755

- name: configure lvm to ignore zfs volumes
  lineinfile:
    path: /etc/lvm/lvm.conf
    backrefs: yes
    regexp: '^\s*#?\s*global_filter\s*='
    line: '        global_filter = [ "r|/dev/zd[0-9]+|" ]'

- name: create target datasets
  loop: "{{ zfs_syncoid_sources | dict2items }}"
  loop_control:
    loop_var: source
    label: "{{ source.key }}"
  include_tasks: datasets.yml

- name: create systemd units
  loop: "{{ zfs_syncoid_sources | dict2items }}"
  loop_control:
    label: "{{ item.key }}"
  template:
    src: pull.service.j2
    dest: "/etc/systemd/system/syncoid-pull-{{ item.key }}.service"

- name: create systemd timer units for periodic backups
  loop: "{{ zfs_syncoid_sources | dict2items }}"
  loop_control:
    label: "{{ item.key }}"
  when: "'periodic' in item.value"
  template:
    src: pull.timer.j2
    dest: "/etc/systemd/system/syncoid-pull-{{ item.key }}.timer"

- name: make sure systemd timer units for periodic backups are enabled and started
  loop: "{{ zfs_syncoid_sources | dict2items }}"
  loop_control:
    label: "{{ item.key }}"
  when: "'periodic' in item.value"
  systemd:
    daemon_reload: yes
    name: "syncoid-pull-{{ item.key }}.timer"
    state: started
    enabled: yes

- name: enable autosuspend
  when: zfs_syncoid_autosuspend
  block:
  - name: create systemd override directory for sanoid timer
    file:
      path: /etc/systemd/system/sanoid.timer.d
      state: directory

  - name: disable persistence of sanoid timer
    copy:
      content: |
        [Timer]
        Persistent=false
      dest: /etc/systemd/system/sanoid.timer.d/no-persistence.conf

  - name: generate syncoid post script
    copy:
      content: |
        #!/bin/bash

        echo "running sanoid --prune to cleanup old snapshots"
        sleep 5
        systemctl start --wait sanoid-prune.service

        echo "wait 30s (autosuspend cooldown period)"
        sleep 30
        echo "done."
      dest: /var/lib/syncoid/syncoid_post
      mode: 0755

  - name: install python deps
    apt:
      name: "{{ python_basename }}-dbus"
      state: present

  - name: install autosuspend script
    template:
      src: autosuspend.py.j2
      dest: /usr/local/bin/syncoid-autosuspend.py
      mode: 0755

  - name: install autosuspend systemd units
    loop:
    - service
    - timer
    template:
      src: "autosuspend.{{ item }}.j2"
      dest: "/etc/systemd/system/syncoid-autosuspend.{{ item }}"

  - name: make sure autosuspend timer unit is enabled and started
    systemd:
      daemon_reload: yes
      name: syncoid-autosuspend.timer
      enabled: yes
      state: started