From 19658ae596138d399db2cbb1f87061a4cf23256e Mon Sep 17 00:00:00 2001 From: Bertrand Benjamin Date: Wed, 9 Mar 2022 10:46:21 +0100 Subject: [PATCH] Feat: borgmatic start after boot --- roles/borgmatic/README.md | 24 ++++++------------- roles/borgmatic/defaults/main.yml | 3 ++- roles/borgmatic/molecule/default/converge.yml | 7 +++--- .../molecule/default/tests/test_default.py | 10 ++++++-- roles/borgmatic/tasks/main.yml | 20 +++++++++------- .../borgmatic.afterboot.timer.j2} | 3 +-- .../borgmatic/templates/borgmatic.service.j2 | 6 +++-- 7 files changed, 37 insertions(+), 36 deletions(-) rename roles/borgmatic/{files/borgmatic.timer => templates/borgmatic.afterboot.timer.j2} (60%) diff --git a/roles/borgmatic/README.md b/roles/borgmatic/README.md index 225dd44..f3edb10 100644 --- a/roles/borgmatic/README.md +++ b/roles/borgmatic/README.md @@ -1,22 +1,21 @@ -Role Name +Borgmatic ========= -A brief description of the role goes here. +Deploy borgmatic and configure it. -Requirements ------------- - -Any pre-requisites that may not be covered by Ansible itself or the role should be mentioned here. For instance, if the role uses the EC2 module, it may be a good idea to mention in this section that the boto package is required. Role Variables -------------- -A description of the settable variables for this role should go here, including any variables that are in defaults/main.yml, vars/main.yml, and any variables that can/should be set via parameters to the role. Any variables that are read from other roles and/or the global scope (ie. hostvars, group vars, etc.) should be mentioned here as well. +Available variables are listed below, along with default values (see defaults/main.yml): + + borg_source_directories + borg_repositories Dependencies ------------ -A list of other roles hosted on Galaxy should go here, plus any details in regards to parameters that may need to be set for other roles, or variables that are used from other roles. +None Example Playbook ---------------- @@ -27,12 +26,3 @@ Including an example of how to use your role (for instance, with variables passe roles: - { role: username.rolename, x: 42 } -License -------- - -BSD - -Author Information ------------------- - -An optional section for the role authors to include contact information, or a website (HTML is not allowed). diff --git a/roles/borgmatic/defaults/main.yml b/roles/borgmatic/defaults/main.yml index 935012d..530bb85 100644 --- a/roles/borgmatic/defaults/main.yml +++ b/roles/borgmatic/defaults/main.yml @@ -1,8 +1,9 @@ --- # defaults file for borgmatic +borgmatic_name: default borg_encryption_passphrase: '' borg_exclude_patterns: [] -borgmatic_config_name: config.yaml +borgmatic_config_file: "/etc/borgmatic/{{ borgmatic_name }}.yaml" borgmatic_large_repo: true borgmatic_hooks: on_error: diff --git a/roles/borgmatic/molecule/default/converge.yml b/roles/borgmatic/molecule/default/converge.yml index 7e3a920..a79e4e4 100644 --- a/roles/borgmatic/molecule/default/converge.yml +++ b/roles/borgmatic/molecule/default/converge.yml @@ -8,12 +8,11 @@ - /media/test2/ - borg_repository: - /backup/ - - borgmatic_config_name: config.yaml pre_tasks: - #- name: Update apt cache -# apt: -# update_cache: yes + - name: Update apt cache + apt: + update_cache: yes - name: Create sources directories file: diff --git a/roles/borgmatic/molecule/default/tests/test_default.py b/roles/borgmatic/molecule/default/tests/test_default.py index edf962b..518f2d6 100644 --- a/roles/borgmatic/molecule/default/tests/test_default.py +++ b/roles/borgmatic/molecule/default/tests/test_default.py @@ -8,7 +8,13 @@ def test_installed_packages(host): assert borgmatic.is_installed def test_borgmatic_config(host): - config = host.file("/etc/borgmatic/config.yaml") + config_file = "/etc/borgmatic/default.yaml" + config = host.file(config_file) assert config.exists - valid_config = host.run("sudo validate-borgmatic-config") + valid_config = host.run(f"sudo validate-borgmatic-config -c {config_file}") assert valid_config.succeeded + +def test_borgmatic_service(host): + borgmatic_service = host.service("borgmatic_default") + assert borgmatic_service.is_valid + assert borgmatic_service.is_enabled diff --git a/roles/borgmatic/tasks/main.yml b/roles/borgmatic/tasks/main.yml index c7b3e95..c9587bf 100644 --- a/roles/borgmatic/tasks/main.yml +++ b/roles/borgmatic/tasks/main.yml @@ -1,5 +1,9 @@ --- # tasks file for borgmatic +- name: debug + debug: + msg: "{{ borgmatic_config_file }}" + - name: stat on sources stat: path: "{{ item }}" @@ -27,12 +31,12 @@ - name: Borgmatic config template: src: borgmatic.config.yaml.j2 - dest: "/etc/borgmatic/{{ borgmatic_config_name }}" + dest: "{{ borgmatic_config_file }}" mode: 644 - name: Check borgmatic config command: - cmd: validate-borgmatic-config + cmd: "validate-borgmatic-config -c {{ borgmatic_config_file}}" register: validate_borgmatic_config - name: Assert that config is validate @@ -42,17 +46,17 @@ - name: copy systemd service template: src: borgmatic.service.j2 - dest: /etc/systemd/system/borgmatic.service + dest: "/lib/systemd/system/borgmatic_{{ borgmatic_name }}.service" mode: 644 notify: reload systemd -- name: copy systemd timer - copy: - src: borgmatic.timer - dest: /etc/systemd/system/borgmatic.timer +- name: copy systemd timer for executing borgmatic after boot + template: + src: borgmatic.afterboot.timer.j2 + dest: "/lib/systemd/system/borgmatic_{{ borgmatic_name }}.timer" notify: "reload systemd" - name: enable timer systemd: - name: borgmatic + name: "borgmatic_{{ borgmatic_name }}" enabled: yes diff --git a/roles/borgmatic/files/borgmatic.timer b/roles/borgmatic/templates/borgmatic.afterboot.timer.j2 similarity index 60% rename from roles/borgmatic/files/borgmatic.timer rename to roles/borgmatic/templates/borgmatic.afterboot.timer.j2 index d6d6dd2..91ad378 100644 --- a/roles/borgmatic/files/borgmatic.timer +++ b/roles/borgmatic/templates/borgmatic.afterboot.timer.j2 @@ -1,9 +1,8 @@ [Unit] -Description=Run borgmatic backup +Description=Run borgmatic {{ borgmatic_name }}backup [Timer] OnBootSec=2min -OnUnitActiveSec=1d Persistent=true [Install] diff --git a/roles/borgmatic/templates/borgmatic.service.j2 b/roles/borgmatic/templates/borgmatic.service.j2 index 419002b..38719e4 100644 --- a/roles/borgmatic/templates/borgmatic.service.j2 +++ b/roles/borgmatic/templates/borgmatic.service.j2 @@ -1,3 +1,5 @@ +{{ ansible_managed | comment }} + [Unit] Description=borgmatic backup Wants=network-online.target @@ -50,5 +52,5 @@ LogRateLimitIntervalSec=0 # Delay start to prevent backups running during boot. Note that systemd-inhibit requires dbus and # dbus-user-session to be installed. -ExecStartPre=sleep 1m -ExecStart=systemd-inhibit --who="borgmatic" --why="Prevent interrupting scheduled backup" /usr/bin/borgmatic -c /etc/borgmatic/{{ borgmatic_config_name }} --verbosity -1 --syslog-verbosity 1 +ExecStartPre=/usr/bin/sleep 1m +ExecStart=/usr/bin/systemd-inhibit --who="borgmatic" --why="Prevent interrupting scheduled backup" /usr/bin/borgmatic -c {{ borgmatic_config_file }} --verbosity -1 --syslog-verbosity 1