Feat: borgmatic start after boot

This commit is contained in:
Bertrand Benjamin 2022-03-09 10:46:21 +01:00
parent 04d221778b
commit 19658ae596
7 changed files with 37 additions and 36 deletions

View File

@ -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 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 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 Example Playbook
---------------- ----------------
@ -27,12 +26,3 @@ Including an example of how to use your role (for instance, with variables passe
roles: roles:
- { role: username.rolename, x: 42 } - { 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).

View File

@ -1,8 +1,9 @@
--- ---
# defaults file for borgmatic # defaults file for borgmatic
borgmatic_name: default
borg_encryption_passphrase: '' borg_encryption_passphrase: ''
borg_exclude_patterns: [] borg_exclude_patterns: []
borgmatic_config_name: config.yaml borgmatic_config_file: "/etc/borgmatic/{{ borgmatic_name }}.yaml"
borgmatic_large_repo: true borgmatic_large_repo: true
borgmatic_hooks: borgmatic_hooks:
on_error: on_error:

View File

@ -8,12 +8,11 @@
- /media/test2/ - /media/test2/
- borg_repository: - borg_repository:
- /backup/ - /backup/
- borgmatic_config_name: config.yaml
pre_tasks: pre_tasks:
#- name: Update apt cache - name: Update apt cache
# apt: apt:
# update_cache: yes update_cache: yes
- name: Create sources directories - name: Create sources directories
file: file:

View File

@ -8,7 +8,13 @@ def test_installed_packages(host):
assert borgmatic.is_installed assert borgmatic.is_installed
def test_borgmatic_config(host): 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 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 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

View File

@ -1,5 +1,9 @@
--- ---
# tasks file for borgmatic # tasks file for borgmatic
- name: debug
debug:
msg: "{{ borgmatic_config_file }}"
- name: stat on sources - name: stat on sources
stat: stat:
path: "{{ item }}" path: "{{ item }}"
@ -27,12 +31,12 @@
- name: Borgmatic config - name: Borgmatic config
template: template:
src: borgmatic.config.yaml.j2 src: borgmatic.config.yaml.j2
dest: "/etc/borgmatic/{{ borgmatic_config_name }}" dest: "{{ borgmatic_config_file }}"
mode: 644 mode: 644
- name: Check borgmatic config - name: Check borgmatic config
command: command:
cmd: validate-borgmatic-config cmd: "validate-borgmatic-config -c {{ borgmatic_config_file}}"
register: validate_borgmatic_config register: validate_borgmatic_config
- name: Assert that config is validate - name: Assert that config is validate
@ -42,17 +46,17 @@
- name: copy systemd service - name: copy systemd service
template: template:
src: borgmatic.service.j2 src: borgmatic.service.j2
dest: /etc/systemd/system/borgmatic.service dest: "/lib/systemd/system/borgmatic_{{ borgmatic_name }}.service"
mode: 644 mode: 644
notify: reload systemd notify: reload systemd
- name: copy systemd timer - name: copy systemd timer for executing borgmatic after boot
copy: template:
src: borgmatic.timer src: borgmatic.afterboot.timer.j2
dest: /etc/systemd/system/borgmatic.timer dest: "/lib/systemd/system/borgmatic_{{ borgmatic_name }}.timer"
notify: "reload systemd" notify: "reload systemd"
- name: enable timer - name: enable timer
systemd: systemd:
name: borgmatic name: "borgmatic_{{ borgmatic_name }}"
enabled: yes enabled: yes

View File

@ -1,9 +1,8 @@
[Unit] [Unit]
Description=Run borgmatic backup Description=Run borgmatic {{ borgmatic_name }}backup
[Timer] [Timer]
OnBootSec=2min OnBootSec=2min
OnUnitActiveSec=1d
Persistent=true Persistent=true
[Install] [Install]

View File

@ -1,3 +1,5 @@
{{ ansible_managed | comment }}
[Unit] [Unit]
Description=borgmatic backup Description=borgmatic backup
Wants=network-online.target 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 # Delay start to prevent backups running during boot. Note that systemd-inhibit requires dbus and
# dbus-user-session to be installed. # dbus-user-session to be installed.
ExecStartPre=sleep 1m ExecStartPre=/usr/bin/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 ExecStart=/usr/bin/systemd-inhibit --who="borgmatic" --why="Prevent interrupting scheduled backup" /usr/bin/borgmatic -c {{ borgmatic_config_file }} --verbosity -1 --syslog-verbosity 1