From 761d628ca5602b95d29dab88bfa9aa5deeba0cb2 Mon Sep 17 00:00:00 2001 From: Manu Date: Sun, 19 Mar 2023 19:46:29 +0000 Subject: [PATCH] Fix cron job, add assert to prevent duplicate timers --- EXAMPLES.md | 2 +- README.md | 9 +++++---- tasks/noauto_create_timer_cron.yml | 3 ++- tasks/noauto_create_timer_systemd.yml | 18 ++++++++++++++++-- 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/EXAMPLES.md b/EXAMPLES.md index 1f94294..852c09a 100644 --- a/EXAMPLES.md +++ b/EXAMPLES.md @@ -26,7 +26,7 @@ Installs and configures the Borgmatic client and also initializes the repo on th borg_user: "srv_backup" borg_group: "srv_backup" borg_ssh_key_file_path: "{{ backup_user_info.home }}/.ssh/backup" - borg_ssh_command: "ssh -i {{ borg_ssh_key_file_path }} -o StrictHostKeyChecking=no" + borg_ssh_command: "ssh -i {{ borg_ssh_key_file_path }} -o StrictHostKeyChecking=accept-new" borgmatic_timer: systemd borg_source_directories: - /srv/www diff --git a/README.md b/README.md index 37fd481..242677f 100644 --- a/README.md +++ b/README.md @@ -7,8 +7,9 @@ Set up encrypted, compressed and deduplicated backups using [BorgBackup](https:/ Works great with [BorgBase.com](https://www.borgbase.com) - Simple and Secure Hosting for your Borg Repositories. To manage BorgBase repos via Ansible, also see Andy Hawkins' [BorgBase Collection](https://galaxy.ansible.com/adhawkins/borgbase). Main features: -- Set up Borg and Borgmatic -- Schedule regular backups using Cron or Systemd timer. +- Install Borg and Borgmatic from PyPi or distro package +- Set up Borgmatic config +- Schedule regular backups using Cron or Systemd timer ## Example Playbook with root as backup user and Cron timer @@ -40,7 +41,7 @@ Main features: ``` ## Example Playbook with service user and Systemd timer -`` Attention: `` The following implementation leads to problems. +**Attention**: The following implementation leads to problems. If you already use this role and use the user: "root" or the SSH key id_ed25519! ``` @@ -51,7 +52,7 @@ If you already use this role and use the user: "root" or the SSH key id_ed25519! borg_repository: ssh://m5vz9gp4@m5vz9gp4.repo.borgbase.com/./repo borgmatic_timer: systemd borg_ssh_key_file_path: "{{ backup_user_info.home }}/.ssh/backup" - borg_ssh_command: "ssh -i {{ borg_ssh_key_file_path }} -o StrictHostKeyChecking=no" + borg_ssh_command: "ssh -i {{ borg_ssh_key_file_path }} -o StrictHostKeyChecking=accept-new" borg_user: "srv_backup" borg_group: "srv_backup" borg_source_directories: diff --git a/tasks/noauto_create_timer_cron.yml b/tasks/noauto_create_timer_cron.yml index 0afa70e..6a557a4 100644 --- a/tasks/noauto_create_timer_cron.yml +++ b/tasks/noauto_create_timer_cron.yml @@ -6,7 +6,7 @@ - name: Add Cron job for borgmatic block: - - name: Add cron job for create, check and prune + - name: Add single Cron job for borgmatic cron: name: "{{ borgmatic_timer_cron_name }}" hour: "{{ borgmatic_timer_hour }}" @@ -18,6 +18,7 @@ - name: Ensure separate check cron job is absent cron: name: "{{ borgmatic_timer_cron_name }}-check" + cron_file: "{{ borgmatic_timer_cron_name }}" state: absent - name: Set PATH for borgmatic cron job. diff --git a/tasks/noauto_create_timer_systemd.yml b/tasks/noauto_create_timer_systemd.yml index 047c94d..53e0b6b 100644 --- a/tasks/noauto_create_timer_systemd.yml +++ b/tasks/noauto_create_timer_systemd.yml @@ -1,4 +1,18 @@ --- +- name: Register existence of Borgmatic cron file. + cron: + name: "{{ borgmatic_timer_cron_name }}" + cron_file: "{{ borgmatic_timer_cron_name }}" + state: absent + check_mode: true + register: cron_file_exists + +- name: Ensure no Borgmatic Cron file exists. + ansible.builtin.assert: + that: + - not cron_file_exists.changed + fail_msg: Found an existing Borgmatic Cron job. Please remove before using Systemd timer. + - name: Create borgbackup timer block: - name: Copy systemd files @@ -21,7 +35,7 @@ - name: Stop fresh installed borgmatic.timer and borgmatic.service when: "'borgmatic.service' not in ansible_facts.services" block: - - name: Set borgmatic services to stopped - fresh installed + - name: Set borgmatic services to stopped - newly installed ansible.builtin.systemd: name: "{{ item }}" state: stopped @@ -33,7 +47,7 @@ - borgmatic.service # bug: Need own section without masked else the timer are skipped - - name: Set borgmatic timers to stopped - fresh installed + - name: Set borgmatic timers to stopped - newly installed ansible.builtin.systemd: name: "{{ item }}" state: stopped