From 49f3c00885a8d688d1ce78d2a33864abcd44edc6 Mon Sep 17 00:00:00 2001 From: mg Date: Sat, 19 Mar 2022 13:28:41 +0100 Subject: [PATCH] ZFS: scrub: cron --> systemd-timer (#341) Co-authored-by: Michael Grote Reviewed-on: https://git.mgrote.net/mg/ansible/pulls/341 Co-authored-by: mg Co-committed-by: mg --- group_vars/all.yml | 2 +- host_vars/pve2-test2.grote.lan.yml | 12 +-- host_vars/pve2.grote.lan.yml | 12 +-- roles/mgrote.restic/defaults/main.yml | 6 +- roles/mgrote.zfs_scrub/README.md | 5 +- roles/mgrote.zfs_scrub/tasks/main.yml | 92 ++++++++++++++++--- .../templates/zpool-scrub-mail.service.j2 | 8 ++ .../templates/zpool-scrub@.service.j2 | 13 +++ .../templates/zpool-scrub@.timer.j2 | 13 +++ 9 files changed, 122 insertions(+), 41 deletions(-) create mode 100644 roles/mgrote.zfs_scrub/templates/zpool-scrub-mail.service.j2 create mode 100644 roles/mgrote.zfs_scrub/templates/zpool-scrub@.service.j2 create mode 100644 roles/mgrote.zfs_scrub/templates/zpool-scrub@.timer.j2 diff --git a/group_vars/all.yml b/group_vars/all.yml index 7bb0b0e5..c8e80988 100644 --- a/group_vars/all.yml +++ b/group_vars/all.yml @@ -21,7 +21,7 @@ # https://forum.restic.net/t/exclude-syntax-confusion/1531/12 restic_mount_timeout: "10 min" restic_failure_delay: "30 s" - restic_schedule: "0/6:00" + restic_schedule: "0/6:00" # alle 6 Stunden restic_folders_to_backup: "/" # --one-file-system ist gesetzt, also werden weitere Dateisysteme nicht eigeschlossen, es sei denn sie werden hier explizit angegeben; https://restic.readthedocs.io/en/latest/040_backup.html#excluding-files restic_repository: "//fileserver2.grote.lan/restic" restic_repository_password: "{{ lookup('keepass', 'restic_repository_password', 'password') }}" diff --git a/host_vars/pve2-test2.grote.lan.yml b/host_vars/pve2-test2.grote.lan.yml index e80ea38f..f5c8b0a2 100644 --- a/host_vars/pve2-test2.grote.lan.yml +++ b/host_vars/pve2-test2.grote.lan.yml @@ -42,14 +42,6 @@ zfs_extra_arc_max_size: "2147483648" # 2GB in Bytes zfs_extra_zfs_pools: - name: "rpool" - cron_minutes_zfs_scrub: "5" - cron_hour_zfs_scrub: "23" - cron_day_of_month_zfs_scrub: "14" - cron_day_of_week_zfs_scrub: "*" - cron_month_zfs_scrub: "*/2" + systemd_timer_schedule: "*-01,04,07,10-01 23:00" - name: "hdd_vm_mirror" - cron_minutes_zfs_scrub: "10" - cron_hour_zfs_scrub: "23" - cron_day_of_month_zfs_scrub: "14" - cron_day_of_week_zfs_scrub: "*" - cron_month_zfs_scrub: "*/2" + systemd_timer_schedule: "*-01,04,07,10-01 23:00" diff --git a/host_vars/pve2.grote.lan.yml b/host_vars/pve2.grote.lan.yml index 78d89192..dfaaafbb 100644 --- a/host_vars/pve2.grote.lan.yml +++ b/host_vars/pve2.grote.lan.yml @@ -177,17 +177,9 @@ zfs_extra_arc_max_size: "8589934592" # 8GB in Bytes zfs_extra_zfs_pools: - name: "rpool" - cron_minutes_zfs_scrub: "0" - cron_hour_zfs_scrub: "23" - cron_day_of_month_zfs_scrub: "14" - cron_day_of_week_zfs_scrub: "*" - cron_month_zfs_scrub: "*/2" + systemd_timer_schedule: "*-01,04,07,10-01 23:00" # jeden ersten eines jeden Quartals - name: "hdd_data_raidz" - cron_minutes_zfs_scrub: "0" - cron_hour_zfs_scrub: "23" - cron_day_of_month_zfs_scrub: "14" - cron_day_of_week_zfs_scrub: "*" - cron_month_zfs_scrub: "*/2" + systemd_timer_schedule: "*-01,04,07,10-01 23:00" ### mgrote.zfs_sanoid sanoid_datasets: diff --git a/roles/mgrote.restic/defaults/main.yml b/roles/mgrote.restic/defaults/main.yml index 2bd439f3..76e838fb 100644 --- a/roles/mgrote.restic/defaults/main.yml +++ b/roles/mgrote.restic/defaults/main.yml @@ -13,11 +13,11 @@ **/**cache***/** **/**Cache***/** **/**AppData***/** - # timeout for cifs mount; systemd notation + # timeout for cifs mount; systemd notation(systemd-analyze calendar "Sun *-0/2-* 23:00") restic_mount_timeout: "10 min" - # delay for restartung task; systemd notation + # delay for restartung task; systemd notation(systemd-analyze calendar "Sun *-0/2-* 23:00") restic_failure_delay: "30 s" - # when should restic run; systemd notation + # when should restic run; systemd notation(systemd-analyze calendar "Sun *-0/2-* 23:00") restic_schedule: "*:0/2" # welche ordner sollen gesichert werden restic_folders_to_backup: "/usr/local /etc /root /var/www /home" diff --git a/roles/mgrote.zfs_scrub/README.md b/roles/mgrote.zfs_scrub/README.md index 37e35843..8dcb49f9 100644 --- a/roles/mgrote.zfs_scrub/README.md +++ b/roles/mgrote.zfs_scrub/README.md @@ -1,13 +1,12 @@ ## mgrote.zfs_scrub ### Beschreibung -Richtet regelmaessige Scrubs(jeden Sonntag) ein. +Richtet regelmaessige Scrubs mit systemd-timer ein. ### getestet auf -- [x] ProxMox 6.1 +- [x] ProxMox 7* ### Variablen + Defaults - see [defaults](./defaults/main.yml) - Variablen für ``mgrote.zfs_health/trim/scrub/zed/arc_mem/`` sind zusammengefasst unter zfs_extra_* -- see [default-filter](./tasks/main.yml) diff --git a/roles/mgrote.zfs_scrub/tasks/main.yml b/roles/mgrote.zfs_scrub/tasks/main.yml index 40646678..18165bfc 100644 --- a/roles/mgrote.zfs_scrub/tasks/main.yml +++ b/roles/mgrote.zfs_scrub/tasks/main.yml @@ -1,20 +1,84 @@ --- - - name: remove system scrub job #https://forum.proxmox.com/threads/script-sequential-zfs-scrub-for-cron.25124/ + - name: template unit-template + become: true + ansible.builtin.template: + src: zpool-scrub@.service.j2 + dest: /etc/systemd/system/zpool-scrub@.service + owner: root + group: root + mode: 0644 + register: template_unit + + - name: template timer + become: true + ansible.builtin.template: + src: zpool-scrub@.timer.j2 + dest: "/etc/systemd/system/zpool-scrub@{{ item.name }}.timer" + owner: root + group: root + mode: 0644 + loop: "{{ zfs_extra_zfs_pools }}" + register: template_timer + + - name: template mail.service + become: true + ansible.builtin.template: + src: zpool-scrub-mail.service.j2 + dest: "/etc/systemd/system/zpool-scrub-mail.service" + owner: root + group: root + mode: 0644 + register: template_mail + + - name: systemctl daemon-reload + become: yes + ansible.builtin.systemd: + daemon_reload: yes + when: + - template_unit.changed + - template_timer.changed + + - name: systemctl enable units - timer + become: yes + ansible.builtin.systemd: + name: "zpool-scrub@{{ item.name }}.timer" + enabled: yes + masked: no + loop: "{{ zfs_extra_zfs_pools }}" + when: + - template_unit.changed + - template_timer.changed + + - name: systemctl enable units - mail + become: yes + ansible.builtin.systemd: + name: "zpool-scrub-mail.service" + enabled: yes + masked: no + when: + - template_mail + + - name: systemctl start units + become: yes + ansible.builtin.systemd: + name: "zpool-scrub@{{ item.name }}.timer" + state: restarted + enabled: yes + loop: "{{ zfs_extra_zfs_pools }}" + when: + - template_unit.changed + - template_timer.changed + + - name: remove old cronjobs + become: yes + ansible.builtin.cron: + name: zfs-scrub - "{{ item.name }}" + state: absent + with_items: "{{ zfs_extra_zfs_pools }}" + + - name: remove proxmox system scrub job #https://forum.proxmox.com/threads/script-sequential-zfs-scrub-for-cron.25124/ become: true ansible.builtin.lineinfile: path: /etc/cron.d/zfsutils-linux state: absent line: '24 0 8-14 * * root [ $(date +\%w) -eq 0 ] && [ -x /usr/lib/zfs-linux/scrub ] && /usr/lib/zfs-linux/scrub' - - - name: add cronjob "zfs-scrub" - become: yes - ansible.builtin.cron: - name: zfs-scrub - "{{ item.name }}" - state: present - job: "{{ zfs_extra_path_zpool }} scrub {{ item.name }}" - month: "{{ item.cron_month_zfs_scrub | default('*') }}" - weekday: "{{ item.cron_day_of_week_zfs_scrub | default('7') }}" - day: "{{ item.cron_day_of_month_zfs_scrub | default('*/21') }}" - hour: "{{ item.cron_hour_zfs_scrub | default('23') }}" - minute: "{{ item.cron_minutes_zfs_scrub | default('30') }}" - with_items: "{{ zfs_extra_zfs_pools }}" diff --git a/roles/mgrote.zfs_scrub/templates/zpool-scrub-mail.service.j2 b/roles/mgrote.zfs_scrub/templates/zpool-scrub-mail.service.j2 new file mode 100644 index 00000000..35f98275 --- /dev/null +++ b/roles/mgrote.zfs_scrub/templates/zpool-scrub-mail.service.j2 @@ -0,0 +1,8 @@ +{{ file_header | default () }} + +[Unit] +Description=Send a Mail in case of an error in zpool-scrub@.service. + +[Service] +Type=oneshot +ExecStart=/bin/bash -c '{{ zfs_extra_path_zpool }} status | mail -s "[ERROR] zpool scrub - %H" {{ empfaenger_mail }}' diff --git a/roles/mgrote.zfs_scrub/templates/zpool-scrub@.service.j2 b/roles/mgrote.zfs_scrub/templates/zpool-scrub@.service.j2 new file mode 100644 index 00000000..de54bcbe --- /dev/null +++ b/roles/mgrote.zfs_scrub/templates/zpool-scrub@.service.j2 @@ -0,0 +1,13 @@ +{{ file_header | default () }} + +[Unit] +Description=Scrub ZFS pool "%i" +Requires=zfs.target +After=zfs.target +OnFailure=zpool-scrub-mail.service + +[Service] +Type=simple +ExecStartPre=-{{ zfs_extra_path_zpool }} scrub -s %i +ExecStart={{ zfs_extra_path_zpool }} scrub -w %i +ExecStop={{ zfs_extra_path_zpool }} scrub -s %i diff --git a/roles/mgrote.zfs_scrub/templates/zpool-scrub@.timer.j2 b/roles/mgrote.zfs_scrub/templates/zpool-scrub@.timer.j2 new file mode 100644 index 00000000..e31177f2 --- /dev/null +++ b/roles/mgrote.zfs_scrub/templates/zpool-scrub@.timer.j2 @@ -0,0 +1,13 @@ +{{ file_header | default () }} + +[Unit] +Description=Scrub ZFS pool "%i" + +[Timer] +OnCalendar={{ item.systemd_timer_schedule }} +Persistent=true +Unit=zpool-scrub@%i.service +RandomizedDelaySec=30 min + +[Install] +WantedBy=timers.target