From 93b9de9826e4c65450f26fc8ecb302b40d503260 Mon Sep 17 00:00:00 2001 From: L3D Date: Thu, 29 Apr 2021 14:58:12 +0200 Subject: [PATCH] implement systemd timer --- tasks/distribution/Linux.yml | 53 ++++++++++++++++++++++++++++++++---- templates/restic.service.j2 | 8 ++++++ templates/restic.timer.j2 | 10 +++++++ 3 files changed, 65 insertions(+), 6 deletions(-) create mode 100644 templates/restic.service.j2 create mode 100644 templates/restic.timer.j2 diff --git a/tasks/distribution/Linux.yml b/tasks/distribution/Linux.yml index 167e5aa..29a7af6 100644 --- a/tasks/distribution/Linux.yml +++ b/tasks/distribution/Linux.yml @@ -1,6 +1,4 @@ --- -# tasks file for skeleton - - name: reformat dict if necessary set_fact: restic_backups: "{{ restic_backups|dict2items|json_query('[*].value') }}" @@ -10,7 +8,7 @@ - name: Create backup credentials template: src: restic_access_Linux.j2 - dest: '{{ restic_script_dir }}/access-{{ item.name }}.sh' + dest: "{{ restic_script_dir }}/access-{{ item.name | replace(' ', '') }}.sh" mode: '0700' owner: '{{ restic_dir_owner }}' group: '{{ restic_dir_group }}' @@ -25,7 +23,7 @@ - name: Create backup script template: src: restic_script_Linux.j2 - dest: '{{ restic_script_dir }}/backup-{{ item.name }}.sh' + dest: "{{ restic_script_dir }}/backup-{{ item.name | replace(' ', '') }}.sh" mode: '0700' owner: '{{ restic_dir_owner }}' group: '{{ restic_dir_group }}' @@ -37,7 +35,7 @@ - item.src is defined or item.stdin and item.stdin_cmd is defined - item.repo in restic_repos -- name: Setup CRON jobs +- name: cleanup CRON jobs cron: name: 'arillso.restic backup {{ item.name }}' job: 'CRON=true {{ restic_script_dir }}/backup-{{ item.name }}.sh' @@ -45,7 +43,7 @@ hour: '{{ item.schedule_hour | default("*") }}' weekday: '{{ item.schedule_weekday | default("*") }}' month: '{{ item.schedule_month | default("*") }}' - state: present + state: absent become: true no_log: true with_items: '{{ restic_backups }}' @@ -53,3 +51,46 @@ - restic_create_cron - item.name is defined - item.scheduled | default(false) + +- 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: Enable 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) diff --git a/templates/restic.service.j2 b/templates/restic.service.j2 new file mode 100644 index 0000000..98c59e2 --- /dev/null +++ b/templates/restic.service.j2 @@ -0,0 +1,8 @@ +[Unit] +Description=Backup {{ item.name }} using restic + +[Service] +Type=oneshot +ExecStart={{ restic_script_dir }}/backup-{{ item.name }}.sh +TimeoutStartSec=300min +Environment="CRON=true" diff --git a/templates/restic.timer.j2 b/templates/restic.timer.j2 new file mode 100644 index 0000000..79d80a4 --- /dev/null +++ b/templates/restic.timer.j2 @@ -0,0 +1,10 @@ +[Unit] +Description=Run restic backup {{ item.name }} every night + +[Timer] +OnCalendar=*-*-* 02:00:00 +RandomizedDelaySec=3h +Persistent=yes + +[Install] +WantedBy=timers.target