refactor: harden restic configuration

move secrets to .env file so they work on their own, initialize restic repo on fresh deployments
This commit is contained in:
2026-07-12 23:46:29 +02:00
parent c4c8d6de7f
commit 71a02e0f86
5 changed files with 47 additions and 11 deletions
+33 -3
View File
@@ -1,26 +1,56 @@
---
- name: Install Restic
- name: Install restic
ansible.builtin.apt:
name: restic
state: present
become: true
- name: Initialize restic repository
environment:
RESTIC_REPOSITORY: "{{ vault_restic_repo_url }}"
RESTIC_PASSWORD: "{{ vault_restic_password }}"
AWS_ACCESS_KEY_ID: "{{ vault_aws_access_key }}"
AWS_SECRET_ACCESS_KEY: "{{ vault_aws_secret_key }}"
block:
- name: Check if already initialized
ansible.builtin.command: restic snapshots
register: restic_check
failed_when: false
changed_when: false
- name: Initialize repository
ansible.builtin.command: restic init
when: restic_check.rc != 0
- name: Deploy restic environment file
ansible.builtin.template:
src: restic-backup.env.j2
dest: /etc/restic-backup.env
owner: root
group: root
mode: "0600" # Only root can read this file because it contains sensitive credentials
- name: Deploy restic backup script
ansible.builtin.template:
src: restic-backup.sh.j2
dest: /usr/local/bin/restic-backup.sh
mode: "0700" # Only root can read/execute this because it contains secrets
owner: root
group: root
mode: "0700" # Only root can execute this backup script
- name: Deploy restic systemd service
ansible.builtin.template:
src: restic-backup.service.j2
dest: /etc/systemd/system/restic-backup.service
owner: root
group: root
mode: "0644"
- name: Deploy restic systemd timer
ansible.builtin.template:
src: restic-backup.timer.j2
dest: /etc/systemd/system/restic-backup.timer
owner: root
group: root
mode: "0644"
- name: Enable and start backup timer
@@ -0,0 +1,5 @@
# Ansible managed
RESTIC_REPOSITORY="{{ vault_restic_repo_url }}"
RESTIC_PASSWORD="{{ vault_restic_password }}"
AWS_ACCESS_KEY_ID="{{ vault_aws_access_key }}"
AWS_SECRET_ACCESS_KEY="{{ vault_aws_secret_key }}"
@@ -4,4 +4,5 @@ After=network-online.target
[Service]
Type=oneshot
EnvironmentFile=/etc/restic-backup.env
ExecStart=/usr/local/bin/restic-backup.sh
+7 -7
View File
@@ -1,17 +1,17 @@
#!/bin/bash
## Ansible managed
## Secrets are passed via Ansible Vault variables
export RESTIC_REPOSITORY="{{ vault_restic_repo_url }}"
export RESTIC_PASSWORD="{{ vault_restic_password }}"
export AWS_ACCESS_KEY_ID="{{ vault_aws_access_key }}"
export AWS_SECRET_ACCESS_KEY="{{ vault_aws_secret_key }}"
set -e
# Source environment variables if running manually/outside Systemd
if [ -f /etc/restic-backup.env ]; then
# shellcheck source=/dev/null
. /etc/restic-backup.env
fi
echo "Starting Restic Backup at $(date)"
## Run the backup using paths and exclusions defined in group_vars
## Run the backup using paths and exclusions defined as group_vars
restic backup \
{% for path in backup_include_paths %}
"{{ path }}" \
@@ -1,5 +1,5 @@
[Unit]
Description=Run Restic Backup Daily at 4:00 AM
Description=Run Restic Backup
[Timer]
OnCalendar=*-*-* 04:00:00