diff --git a/defaults/main.yml b/defaults/main.yml index aad7302..a9b721f 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -22,6 +22,7 @@ restic_systemd_timer_default_OnCalendar: '*-*-* 02:00:00' # perform simple version check for this role? (true is recomended) submodules_versioncheck: false +restic_do_not_cleanup_cron: false # outdated variables because of irritating names, but kept for compatibility restic_create_cron: false diff --git a/tasks/delete_legacy_cron_entry.yml b/tasks/delete_legacy_cron_entry.yml new file mode 100644 index 0000000..c6f20ef --- /dev/null +++ b/tasks/delete_legacy_cron_entry.yml @@ -0,0 +1,48 @@ +--- +- name: "check if ansible version is under 2.12.0" + ansible.builtin.assert: + that: + - "ansible_version.full is version_compare('2.12.0', '<')" + fail_msg: "[ERROR] Youre ansible version is above 2.12.0" + success_msg: "Congratulations. You are using ansible version {{ ansible_version.full }}" + delegate_to: localhost + +- name: try to remove entries from /etc/crontab + ansible.builtin.cron: + name: "do1jlr.restic backup {{ item.name }}" + job: "CRON=true {{ restic_script_dir }}/backup-{{ item.name | replace(' ', '') }}.sh" + minute: '{{ item.schedule_minute | default("*") }}' + hour: '{{ item.schedule_hour | default("2") }}' + weekday: '{{ item.schedule_weekday | default("*") }}' + month: '{{ item.schedule_month | default("*") }}' + state: absent + cron_file: '/etc/crontab' + user: 'root' + become: true + no_log: "{{ restic_no_log }}" + with_items: '{{ restic_backups }}' + when: + - restic_create_schedule | bool + - item.name is defined + - item.scheduled | default(false) + - ansible_service_mgr != 'systemd' or restic_force_cron | default(false) or restic_schedule_type == "cronjob" + ignore_error: true + register: cron_delete + +- name: "make sure 'do1jlr.restic backup {{ item.name }}' is not in /etc/crontab" + become: true + ansible.builtin.lineinfile: + path: '/etc/crontab' + state: absent + search_string: "do1jlr.restic backup {{ item.name }}" + when: cron_delete.failed + with_items: '{{ restic_backups }}' + +- name: "make sure '{{ restic_script_dir }}/backup-{{ item.name | replace(' ', '') }}.sh' is not in /etc/crontab" + become: true + ansible.builtin.lineinfile: + path: '/etc/crontab' + state: absent + search_string: "{{ restic_script_dir }}/backup-{{ item.name | replace(' ', '') }}.sh" + when: cron_delete.failed + with_items: '{{ restic_backups }}' diff --git a/tasks/restic_create_cron.yml b/tasks/restic_create_cron.yml new file mode 100644 index 0000000..6f63526 --- /dev/null +++ b/tasks/restic_create_cron.yml @@ -0,0 +1,18 @@ +--- +- name: install restic backup cronjob + ansible.builtin.cron: + name: "do1jlr.restic backup {{ item.name }}" + job: "CRON=true {{ restic_script_dir }}/backup-{{ item.name | replace(' ', '') }}.sh" + minute: '{{ item.schedule_minute | default("*") }}' + hour: '{{ item.schedule_hour | default("2") }}' + weekday: '{{ item.schedule_weekday | default("*") }}' + month: '{{ item.schedule_month | default("*") }}' + state: present + user: 'root' + cron_file: restic_backup + become: true + no_log: "{{ restic_no_log }}" + with_items: '{{ restic_backups }}' + when: + - item.name is defined + - item.scheduled | default(false) diff --git a/tasks/restic_create_systemd.yml b/tasks/restic_create_systemd.yml new file mode 100644 index 0000000..009c2bb --- /dev/null +++ b/tasks/restic_create_systemd.yml @@ -0,0 +1,66 @@ +--- +- name: create systemd timer + block: + - name: copy systemd timer + become: true + ansible.builtin.template: + src: templates/restic.timer.j2 + dest: "/lib/systemd/system/restic-{{ item.name | replace(' ', '') }}.timer" + owner: 'root' + group: 'root' + mode: '0644' + no_log: "{{ restic_no_log }}" + with_items: '{{ restic_backups }}' + notify: systemctl restart restic.timer + when: + - item.name is defined + - item.scheduled | default(false) + + - name: copy systemd service + become: true + ansible.builtin.template: + src: templates/restic.service.j2 + dest: "/lib/systemd/system/restic-{{ item.name | replace(' ', '') }}.service" + owner: 'root' + group: 'root' + mode: '0644' + no_log: "{{ restic_no_log }}" + with_items: '{{ restic_backups }}' + when: + - item.name is defined + - item.scheduled | default(false) + + - name: Enable restic service + become: true + ansible.builtin.systemd: + name: "restic-{{ item.name | replace(' ', '') | string }}.service" + enabled: true + daemon_reload: true + masked: false + with_items: '{{ restic_backups }}' + notify: systemctl restart restic.timer + when: + - item.name is defined + - item.scheduled | default(false) + + - name: Enable and start restic timer + become: true + ansible.builtin.systemd: + name: "restic-{{ item.name | replace(' ', '') | string }}.timer" + enabled: true + state: started + daemon_reload: true + masked: false + with_items: '{{ restic_backups }}' + notify: systemctl restart restic.timer + when: + - item.name is defined + - item.scheduled | default(false) + when: + - ansible_service_mgr == 'systemd' + - restic_schedule_type == "systemd" + - restic_create_schedule | bool + rescue: + - name: set cronjob intead of systemd + set_fact: + restic_force_cron: true diff --git a/tasks/restic_delete_systemd.yml b/tasks/restic_delete_systemd.yml new file mode 100644 index 0000000..dec3363 --- /dev/null +++ b/tasks/restic_delete_systemd.yml @@ -0,0 +1,58 @@ +--- +- name: remove systemd timer + block: + - name: mask restic timer + become: true + ansible.builtin.systemd: + name: "restic-{{ item.name | replace(' ', '') | string }}.timer" + enabled: false + state: stopped + daemon_reload: true + masked: false + with_items: '{{ restic_backups }}' + ignore_errors: true + failed_when: false + changed_when: false + when: + - item.name is defined + - item.scheduled | default(false) + - restic_schedule_type == "cronjob" or restic_force_cron | default(false) + + - name: mask restic service + become: true + ansible.builtin.systemd: + name: "restic-{{ item.name | replace(' ', '') | string }}.service" + enabled: false + state: stopped + daemon_reload: true + masked: true + with_items: '{{ restic_backups }}' + when: + - item.name is defined + - item.scheduled | default(false) + - restic_schedule_type == "cronjob" or restic_force_cron | default(false) + ignore_errors: true + failed_when: false + changed_when: false + + - name: delete systemd .timer file + become: true + ansible.builtin.file: + path: "/lib/systemd/system/restic-{{ item.name | replace(' ', '') }}.timer" + state: absent + with_items: '{{ restic_backups }}' + when: + - item.name is defined + - item.scheduled | default(false) + - restic_schedule_type == "cronjob" or restic_force_cron | default(false) + + - name: delete systemd .service file + become: true + ansible.builtin.file: + path: "/lib/systemd/system/restic-{{ item.name | replace(' ', '') }}.service" + state: absent + with_items: '{{ restic_backups }}' + when: + - item.name is defined + - item.scheduled | default(false) + - restic_schedule_type == "cronjob" or restic_force_cron | default(false) diff --git a/tasks/schedule.yml b/tasks/schedule.yml index 54a02cc..244aa69 100644 --- a/tasks/schedule.yml +++ b/tasks/schedule.yml @@ -1,146 +1,24 @@ --- -- name: create systemd timer - block: - - name: copy systemd timer - become: true - ansible.builtin.template: - src: templates/restic.timer.j2 - dest: "/lib/systemd/system/restic-{{ item.name | replace(' ', '') }}.timer" - owner: 'root' - group: 'root' - mode: '0644' - no_log: "{{ restic_no_log }}" - with_items: '{{ restic_backups }}' - notify: systemctl restart restic.timer - when: - - item.name is defined - - item.scheduled | default(false) - - - name: copy systemd service - become: true - ansible.builtin.template: - src: templates/restic.service.j2 - dest: "/lib/systemd/system/restic-{{ item.name | replace(' ', '') }}.service" - owner: 'root' - group: 'root' - mode: '0644' - no_log: "{{ restic_no_log }}" - with_items: '{{ restic_backups }}' - when: - - item.name is defined - - item.scheduled | default(false) - - - name: copy systemd service - become: true - ansible.builtin.template: - src: templates/restic.service.j2 - dest: "/lib/systemd/system/restic-{{ item.name | replace(' ', '') }}.service" - owner: 'root' - group: 'root' - mode: '0644' - no_log: "{{ restic_no_log }}" - with_items: '{{ restic_backups }}' - when: - - item.name is defined - - item.scheduled | default(false) - - - name: Enable restic service - become: true - ansible.builtin.systemd: - name: "restic-{{ item.name | replace(' ', '') | string }}.service" - enabled: true - daemon_reload: true - masked: false - with_items: '{{ restic_backups }}' - notify: systemctl restart restic.timer - when: - - item.name is defined - - item.scheduled | default(false) - - - name: Enable and start restic timer - become: true - ansible.builtin.systemd: - name: "restic-{{ item.name | replace(' ', '') | string }}.timer" - enabled: true - state: started - daemon_reload: true - masked: false - with_items: '{{ restic_backups }}' - notify: systemctl restart restic.timer - when: - - item.name is defined - - item.scheduled | default(false) - - - name: delete old cronjob entry if available - ansible.builtin.cron: - name: "do1jlr.restic backup {{ item.name }}" - job: "CRON=true {{ restic_script_dir }}/backup-{{ item.name | replace(' ', '') }}.sh" - minute: '{{ item.schedule_minute | default("*") }}' - hour: '{{ item.schedule_hour | default("2") }}' - weekday: '{{ item.schedule_weekday | default("*") }}' - month: '{{ item.schedule_month | default("*") }}' - state: absent - cron_file: '/etc/crontab' - user: 'root' - become: true - no_log: "{{ restic_no_log }}" - with_items: '{{ restic_backups }}' - when: - - item.name is defined - - item.scheduled | default(false) +- name: create restic systemd timer + ansible.builtin.include_tasks: restic_create_systemd.yml when: - ansible_service_mgr == 'systemd' - restic_schedule_type == "systemd" - restic_create_schedule | bool - rescue: - - name: set cronjob intead of systemd - set_fact: - restic_force_cron: true -- name: install cronjob instead of systemd - ansible.builtin.cron: - name: "do1jlr.restic backup {{ item.name }}" - job: "CRON=true {{ restic_script_dir }}/backup-{{ item.name | replace(' ', '') }}.sh" - minute: '{{ item.schedule_minute | default("*") }}' - hour: '{{ item.schedule_hour | default("2") }}' - weekday: '{{ item.schedule_weekday | default("*") }}' - month: '{{ item.schedule_month | default("*") }}' - state: present - cron_file: '/etc/crontab' - user: 'root' - become: true - no_log: "{{ restic_no_log }}" - with_items: '{{ restic_backups }}' +- name: delete systemd timers if available + ansible.builtin.include_tasks: restic_delete_systemd.yml + when: + - ansible_service_mgr == 'systemd' + - restic_schedule_type == "cronjob" or restic_force_cron | default(false) + - restic_create_schedule | bool + +- name: delete old cron entry from previous versions of this role + ansible.builtin.include_tasks: delete_legacy_cron_entry.yml + when: restic_do_not_cleanup_cron | bool + +- name: install restic via cronjob + ansible.builtin.include_tasks: restic_create_cron.yml when: - restic_create_schedule | bool - - item.name is defined - - item.scheduled | default(false) - - ansible_service_mgr != 'systemd' or restic_force_cron | default(false) or restic_schedule_type == "cronjob" - -- name: make sure no unwanted systemd timer is available - ansible.builtin.systemd: - name: "restic-{{ item.name | replace(' ', '') | string }}.timer" - state: 'stopped' - enabled: false - masked: true - with_items: '{{ restic_backups }}' - ignore_errors: true - when: - - restic_create_schedule | bool - - item.name is defined - - item.scheduled | default(false) - - ansible_service_mgr != 'systemd' or restic_force_cron | default(false) or restic_schedule_type == "cronjob" - -- name: mask systemd service - ansible.builtin.systemd: - name: "restic-{{ item.name | replace(' ', '') | string }}.service" - state: 'stopped' - enabled: false - masked: true - with_items: '{{ restic_backups }}' - ignore_errors: true - when: - - restic_create_schedule | bool - - item.name is defined - - item.scheduled | default(false) - ansible_service_mgr != 'systemd' or restic_force_cron | default(false) or restic_schedule_type == "cronjob"