Add playbooks/borgmatic-backup.yml
This commit is contained in:
57
playbooks/borgmatic-backup.yml
Normal file
57
playbooks/borgmatic-backup.yml
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
---
|
||||||
|
- name: Configure Borgmatic backup with deterministic offset
|
||||||
|
hosts: all
|
||||||
|
become: true
|
||||||
|
|
||||||
|
vars:
|
||||||
|
borgmatic_bin: /usr/local/bin/borgmatic
|
||||||
|
backup_hour: 2 # Base hour to run backups
|
||||||
|
backup_window: 1800 # 30 min window (in seconds)
|
||||||
|
borgmatic_config_dir: /etc/borgmatic
|
||||||
|
|
||||||
|
tasks:
|
||||||
|
- name: Ensure Borg and dependencies are present
|
||||||
|
package:
|
||||||
|
name:
|
||||||
|
- borgbackup
|
||||||
|
- python3-pip
|
||||||
|
state: present
|
||||||
|
|
||||||
|
- name: Ensure borgmatic is installed via pip
|
||||||
|
pip:
|
||||||
|
name: borgmatic
|
||||||
|
executable: pip3
|
||||||
|
|
||||||
|
- name: Create borgmatic config directory
|
||||||
|
file:
|
||||||
|
path: "{{ borgmatic_config_dir }}"
|
||||||
|
state: directory
|
||||||
|
owner: root
|
||||||
|
group: root
|
||||||
|
mode: '0755'
|
||||||
|
|
||||||
|
- name: Generate deterministic backup offset
|
||||||
|
set_fact:
|
||||||
|
backup_offset: >-
|
||||||
|
{{ (inventory_hostname | hash('md5') | int(base=16)) % backup_window }}
|
||||||
|
|
||||||
|
- name: Split offset into minutes and seconds
|
||||||
|
set_fact:
|
||||||
|
backup_offset_minutes: "{{ backup_offset // 60 }}"
|
||||||
|
backup_offset_seconds: "{{ backup_offset % 60 }}"
|
||||||
|
|
||||||
|
- name: Deploy borgmatic config
|
||||||
|
template:
|
||||||
|
src: borgmatic-config.yaml.j2
|
||||||
|
dest: "{{ borgmatic_config_dir }}/config.yaml"
|
||||||
|
owner: root
|
||||||
|
group: root
|
||||||
|
mode: '0600'
|
||||||
|
|
||||||
|
- name: Create cron job for borgmatic with offset
|
||||||
|
cron:
|
||||||
|
name: "Nightly borgmatic backup"
|
||||||
|
user: root
|
||||||
|
hour: "{{ backup_hour }}"
|
||||||
|
minute: "{{ backup_offset_minutes }}"
|
||||||
|
job: "sleep {{ backup_offset_seconds }} && {{ borgmatic_bin }} --verbosity 1"
|
||||||
Reference in New Issue
Block a user