Author SHA1 Message Date
alessandrovitali 617883a9a7 style: rewrite comments 2026-07-12 23:56:06 +02:00
alessandrovitali 71a02e0f86 refactor: harden restic configuration
move secrets to .env file so they work on their own, initialize restic repo on fresh deployments
2026-07-12 23:46:29 +02:00
alessandrovitali c4c8d6de7f feat: make backup role universal by passing paths as variables 2026-07-09 16:42:10 +02:00
alessandrovitali 8d96529d8f feat: add backup role using restic 2026-07-09 15:51:45 +02:00
12 changed files with 141 additions and 59 deletions
+1 -3
View File
@@ -1,5 +1,3 @@
# Ansible Collection # Ansible Collection
Opinionated set of roles to configure reasonable defaults for newly provisioned virtual machines. The repository provides a ready-to-use Ansible Collection (`studio.ansible`) containing reusable roles. Opinionated set of roles to configure reasonable defaults for newly provisioned Debian / Ubuntu VMs running Docker. The repository provides a ready-to-use Ansible Collection (`studio.ansible`) containing reusable roles.
Currently only supports Debian-based systems.
+2 -3
View File
@@ -3,9 +3,8 @@
namespace: studio namespace: studio
name: ansible name: ansible
readme: README.md readme: README.md
version: 0.0.1 version: 1.0.0
authors: authors:
- Studio Vitali - Studio Vitali
description: Studio Ansible collection containing common roles description: Studio Ansible collection containing common roles
license: license: MIT
- MIT
+14
View File
@@ -0,0 +1,14 @@
# ansible/roles/backup/defaults/main.yml
---
## Directories to include in the backup
backup_include_paths:
- /etc
- /opt/docker
## Directories to exclude from the backup
backup_exclude_paths: [] ## Nothing to exclude
## Restic retention policy
backup_retention_daily: 7
backup_retention_weekly: 4
backup_retention_monthly: 6
+61
View File
@@ -0,0 +1,61 @@
---
- name: Install restic
ansible.builtin.apt:
name: restic
state: present
- 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
- name: Deploy restic backup script
ansible.builtin.template:
src: restic-backup.sh.j2
dest: /usr/local/bin/restic-backup.sh
owner: root
group: root
mode: "0700" ## only root can execute the 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
ansible.builtin.systemd:
name: restic-backup.timer
enabled: yes
state: started
daemon_reload: yes
@@ -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 }}"
@@ -0,0 +1,8 @@
[Unit]
Description=Run Restic Backup
After=network-online.target
[Service]
Type=oneshot
EnvironmentFile=/etc/restic-backup.env
ExecStart=/usr/local/bin/restic-backup.sh
@@ -0,0 +1,30 @@
#!/bin/bash
## Ansible managed
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 as group_vars
restic backup \
{% for path in backup_include_paths %}
"{{ path }}" \
{% endfor %}
{% for path in backup_exclude_paths %}
--exclude "{{ path }}" \
{% endfor %}
--cleanup-cache
restic forget \
--prune \
--keep-daily {{ backup_retention_daily }} \
--keep-weekly {{ backup_retention_weekly }} \
--keep-monthly {{ backup_retention_monthly }}
echo "Restic backup completed at $(date)"
@@ -0,0 +1,10 @@
[Unit]
Description=Run Restic Backup
[Timer]
OnCalendar=*-*-* 04:00:00
Persistent=true
RandomizedDelaySec=1800
[Install]
WantedBy=timers.target
+2
View File
@@ -10,6 +10,7 @@
update_cache: true update_cache: true
upgrade: dist upgrade: dist
cache_valid_time: 3600 cache_valid_time: 3600
when: ansible_facts['os_family'] == "Debian" ## only on Debian / Ubuntu systems
become: true become: true
- name: Install essential system utilities - name: Install essential system utilities
@@ -25,6 +26,7 @@
- unattended-upgrades - unattended-upgrades
state: present state: present
update_cache: true update_cache: true
when: ansible_facts['os_family'] == "Debian"
become: true become: true
# #
# - name: Enable unattended-upgrades # - name: Enable unattended-upgrades
+5 -52
View File
@@ -1,58 +1,18 @@
--- ---
- name: Install Docker prerequisites - name: Install necessary Docker packages
ansible.builtin.apt: ansible.builtin.apt:
name: name:
- ca-certificates - docker.io
- curl - docker-compose
state: present state: present
update_cache: true when: ansible_facts['os_family'] == "Debian" ## Debian / Ubuntu
become: true become: true
- name: Ensure APT keyrings directory exists - name: Ensure Docker service is running
ansible.builtin.file:
path: /etc/apt/keyrings
state: directory
mode: "0755"
become: true
- name: Download Docker official GPG key
ansible.builtin.get_url:
url: "https://download.docker.com/linux/{{ ansible_distribution | lower }}/gpg"
dest: /etc/apt/keyrings/docker.asc
mode: "0644"
become: true
- name: Get system architecture
ansible.builtin.command: dpkg --print-architecture
register: dpkg_architecture
changed_when: false
become: true
- name: Add Docker official APT repository
ansible.builtin.apt_repository:
repo: "deb [arch={{ dpkg_architecture.stdout }} signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/{{ ansible_distribution | lower }} {{ ansible_distribution_release }} stable"
state: present
filename: docker
update_cache: yes
become: true
- name: Install modern Docker CE packages
ansible.builtin.apt:
name:
- docker-ce
- docker-ce-cli
- containerd.io
- docker-buildx-plugin
- docker-compose-plugin
state: present
become: true
- name: Ensure Docker services are running and enabled
ansible.builtin.service: ansible.builtin.service:
name: docker name: docker
state: started state: started
enabled: true enabled: true
become: true
- name: Create docker user for container execution - name: Create docker user for container execution
ansible.builtin.user: ansible.builtin.user:
@@ -65,13 +25,6 @@
create_home: false create_home: false
become: true become: true
- name: Add deployment user to docker group
ansible.builtin.user:
name: deployment
groups: docker
append: true
become: true
- name: Create target for docker compose - name: Create target for docker compose
ansible.builtin.file: ansible.builtin.file:
path: /opt/docker path: /opt/docker
+1 -1
View File
@@ -1,5 +1,5 @@
--- ---
- name: Restart sshd - name: Restart sshd
ansible.builtin.service: ansible.builtin.service:
name: ssh name: sshd
state: restarted state: restarted
+2
View File
@@ -24,6 +24,7 @@
name: name:
- fail2ban - fail2ban
state: present state: present
when: ansible_facts['os_family'] == "Debian"
become: true become: true
- name: Start fail2ban - name: Start fail2ban
@@ -31,3 +32,4 @@
name: fail2ban name: fail2ban
state: started state: started
enabled: yes enabled: yes
when: ansible_facts['os_family'] == "Debian"