feat: backup #10
@@ -1,26 +1,56 @@
|
|||||||
---
|
---
|
||||||
- name: Install Restic
|
- name: Install restic
|
||||||
ansible.builtin.apt:
|
ansible.builtin.apt:
|
||||||
name: restic
|
name: restic
|
||||||
state: present
|
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
|
- name: Deploy restic backup script
|
||||||
ansible.builtin.template:
|
ansible.builtin.template:
|
||||||
src: restic-backup.sh.j2
|
src: restic-backup.sh.j2
|
||||||
dest: /usr/local/bin/restic-backup.sh
|
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
|
- name: Deploy restic systemd service
|
||||||
ansible.builtin.template:
|
ansible.builtin.template:
|
||||||
src: restic-backup.service.j2
|
src: restic-backup.service.j2
|
||||||
dest: /etc/systemd/system/restic-backup.service
|
dest: /etc/systemd/system/restic-backup.service
|
||||||
|
owner: root
|
||||||
|
group: root
|
||||||
mode: "0644"
|
mode: "0644"
|
||||||
|
|
||||||
- name: Deploy restic systemd timer
|
- name: Deploy restic systemd timer
|
||||||
ansible.builtin.template:
|
ansible.builtin.template:
|
||||||
src: restic-backup.timer.j2
|
src: restic-backup.timer.j2
|
||||||
dest: /etc/systemd/system/restic-backup.timer
|
dest: /etc/systemd/system/restic-backup.timer
|
||||||
|
owner: root
|
||||||
|
group: root
|
||||||
mode: "0644"
|
mode: "0644"
|
||||||
|
|
||||||
- name: Enable and start backup timer
|
- 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]
|
[Service]
|
||||||
Type=oneshot
|
Type=oneshot
|
||||||
|
EnvironmentFile=/etc/restic-backup.env
|
||||||
ExecStart=/usr/local/bin/restic-backup.sh
|
ExecStart=/usr/local/bin/restic-backup.sh
|
||||||
|
|||||||
@@ -1,17 +1,17 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
## Ansible managed
|
## 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
|
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)"
|
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 \
|
restic backup \
|
||||||
{% for path in backup_include_paths %}
|
{% for path in backup_include_paths %}
|
||||||
"{{ path }}" \
|
"{{ path }}" \
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
[Unit]
|
[Unit]
|
||||||
Description=Run Restic Backup Daily at 4:00 AM
|
Description=Run Restic Backup
|
||||||
|
|
||||||
[Timer]
|
[Timer]
|
||||||
OnCalendar=*-*-* 04:00:00
|
OnCalendar=*-*-* 04:00:00
|
||||||
|
|||||||
Reference in New Issue
Block a user