ansible_role_restic/tasks/timer.yml

116 lines
3.5 KiB
YAML
Raw Normal View History

2021-04-29 16:16:35 +02:00
---
- name: cleanup old CRON jobs from migration
ansible.builtin.cron:
name: 'arillso.restic backup {{ item.name }}'
job: 'CRON=true {{ restic_script_dir }}/backup-{{ item.name }}.sh'
minute: '{{ item.schedule_minute | default("*") }}'
hour: '{{ item.schedule_hour | default("*") }}'
weekday: '{{ item.schedule_weekday | default("*") }}'
month: '{{ item.schedule_month | default("*") }}'
state: absent
become: true
no_log: true
with_items: '{{ restic_backups }}'
when:
- restic_create_cron
- item.name is defined
- item.scheduled | default(false)
- 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: true
with_items: '{{ restic_backups }}'
when:
- restic_create_cron
- 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: true
with_items: '{{ restic_backups }}'
when:
- restic_create_cron
- 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: true
with_items: '{{ restic_backups }}'
when:
- restic_create_cron
- 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
with_items: '{{ restic_backups }}'
when:
- restic_create_cron
- 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
with_items: '{{ restic_backups }}'
when:
- restic_create_cron
- item.name is defined
- item.scheduled | default(false)
when: ansible_service_mgr == 'systemd'
rescue:
- name: set ceonjo 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("*") }}'
weekday: '{{ item.schedule_weekday | default("*") }}'
month: '{{ item.schedule_month | default("*") }}'
state: present
cron_file: '/etc/crontab'
user: 'root'
become: true
no_log: true
with_items: '{{ restic_backups }}'
when:
- restic_create_cron
- item.name is defined
- item.scheduled | default(false)
- ansible_service_mgr != 'systemd' or restic_force_cron | default(false)