Fix cron job, add assert to prevent duplicate timers

This commit is contained in:
Manu 2023-03-19 19:46:29 +00:00
parent f9fc12be61
commit 761d628ca5
4 changed files with 24 additions and 8 deletions

View File

@ -26,7 +26,7 @@ Installs and configures the Borgmatic client and also initializes the repo on th
borg_user: "srv_backup" borg_user: "srv_backup"
borg_group: "srv_backup" borg_group: "srv_backup"
borg_ssh_key_file_path: "{{ backup_user_info.home }}/.ssh/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 borgmatic_timer: systemd
borg_source_directories: borg_source_directories:
- /srv/www - /srv/www

View File

@ -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). 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: Main features:
- Set up Borg and Borgmatic - Install Borg and Borgmatic from PyPi or distro package
- Schedule regular backups using Cron or Systemd timer. - Set up Borgmatic config
- Schedule regular backups using Cron or Systemd timer
## Example Playbook with root as backup user and Cron 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 ## 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! 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 borg_repository: ssh://m5vz9gp4@m5vz9gp4.repo.borgbase.com/./repo
borgmatic_timer: systemd borgmatic_timer: systemd
borg_ssh_key_file_path: "{{ backup_user_info.home }}/.ssh/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"
borg_user: "srv_backup" borg_user: "srv_backup"
borg_group: "srv_backup" borg_group: "srv_backup"
borg_source_directories: borg_source_directories:

View File

@ -6,7 +6,7 @@
- name: Add Cron job for borgmatic - name: Add Cron job for borgmatic
block: block:
- name: Add cron job for create, check and prune - name: Add single Cron job for borgmatic
cron: cron:
name: "{{ borgmatic_timer_cron_name }}" name: "{{ borgmatic_timer_cron_name }}"
hour: "{{ borgmatic_timer_hour }}" hour: "{{ borgmatic_timer_hour }}"
@ -18,6 +18,7 @@
- name: Ensure separate check cron job is absent - name: Ensure separate check cron job is absent
cron: cron:
name: "{{ borgmatic_timer_cron_name }}-check" name: "{{ borgmatic_timer_cron_name }}-check"
cron_file: "{{ borgmatic_timer_cron_name }}"
state: absent state: absent
- name: Set PATH for borgmatic cron job. - name: Set PATH for borgmatic cron job.

View File

@ -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 - name: Create borgbackup timer
block: block:
- name: Copy systemd files - name: Copy systemd files
@ -21,7 +35,7 @@
- name: Stop fresh installed borgmatic.timer and borgmatic.service - name: Stop fresh installed borgmatic.timer and borgmatic.service
when: "'borgmatic.service' not in ansible_facts.services" when: "'borgmatic.service' not in ansible_facts.services"
block: block:
- name: Set borgmatic services to stopped - fresh installed - name: Set borgmatic services to stopped - newly installed
ansible.builtin.systemd: ansible.builtin.systemd:
name: "{{ item }}" name: "{{ item }}"
state: stopped state: stopped
@ -33,7 +47,7 @@
- borgmatic.service - borgmatic.service
# bug: Need own section without masked else the timer are skipped # 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: ansible.builtin.systemd:
name: "{{ item }}" name: "{{ item }}"
state: stopped state: stopped