aboutsummaryrefslogtreecommitdiffstats
path: root/roles
diff options
context:
space:
mode:
authorEugene Triguba <hello@eugenetriguba.com>2026-10-17 23:26:00 -0700
committerEugene Triguba <hello@eugenetriguba.com>2026-10-17 23:26:00 -0700
commit97c5e0b0ff579ec44ca2b19c9458c44074c6eeba (patch)
tree5a0fa510b972577b7d9d6b96e613646074c9e755 /roles
parentAdd preserve modification time to hushlogin (diff)
downloadsite-configs-97c5e0b0ff579ec44ca2b19c9458c44074c6eeba.zip
Add automated backups via restic to backblaze's b2 service
Diffstat (limited to 'roles')
-rw-r--r--roles/restic_backups/tasks/main.yml77
-rw-r--r--roles/restic_backups/vars/main.yml6
2 files changed, 83 insertions, 0 deletions
diff --git a/roles/restic_backups/tasks/main.yml b/roles/restic_backups/tasks/main.yml
new file mode 100644
index 0000000..58ce67c
--- /dev/null
+++ b/roles/restic_backups/tasks/main.yml
@@ -0,0 +1,77 @@
+- name: Check mandatory variables are defined
+ assert:
+ that:
+ - restic_backups_b2_key_id is defined
+ - restic_backups_b2_key_secret is defined
+ - restic_backups_repository is defined
+ - restic_backups_password is defined
+
+- name: Install restic
+ community.general.openbsd_pkg:
+ name: "restic"
+ state: present
+
+- name: Check whether the restic repository exists
+ ansible.builtin.command: restic cat config --no-cache
+ environment: "{{ restic_env }}"
+ register: restic_repo_check
+ changed_when: false
+ # From `restic cat config --help`
+ # > Exit status is 0 if the command was successful.
+ # > Exit status is 1 if there was any error.
+ # > Exit status is 10 if the repository does not exist.
+ # > Exit status is 11 if the repository is already locked.
+ # > Exit status is 12 if the password is incorrect.
+ failed_when: restic_repo_check.rc not in [0, 10]
+ no_log: true
+
+- name: Initialize the restic repository
+ ansible.builtin.command: restic init --no-cache
+ environment: "{{ restic_env }}"
+ when: restic_repo_check.rc == 10
+ no_log: true
+
+- name: Create a 'backup' system user
+ ansible.builtin.user:
+ name: _backup
+ create_home: false
+ shell: /sbin/nologin
+ login_class: daemon
+ # /var/www/git is owned by git:git, so to backup that directory, we need
+ # the backup user to be in the git group.
+ groups: git
+ append: yes
+
+- name: Create restic cache directory
+ ansible.builtin.file:
+ path: "/var/cache/restic"
+ state: directory
+ owner: _backup
+ group: wheel
+ mode: "0750"
+
+- name: Create restic env entries in the crontab
+ ansible.builtin.cron:
+ name: "{{ item.key }}"
+ user: _backup
+ env: yes
+ job: "{{ item.value }}"
+ loop: "{{ restic_env | dict2items }}"
+ no_log: true
+
+- name: Add backup job crontab entry
+ ansible.builtin.cron:
+ name: "backup /var/www/git to backblaze"
+ minute: "0"
+ hour: "3"
+ user: _backup
+ job: "restic backup /var/www/git --tag git"
+
+- name: Add backup pruning job crontab entry
+ ansible.builtin.cron:
+ name: "prune /var/www/git backups in backblaze"
+ minute: "0"
+ hour: "4"
+ weekday: "0"
+ user: _backup
+ job: "restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 6 --prune"
diff --git a/roles/restic_backups/vars/main.yml b/roles/restic_backups/vars/main.yml
new file mode 100644
index 0000000..40ba77d
--- /dev/null
+++ b/roles/restic_backups/vars/main.yml
@@ -0,0 +1,6 @@
+restic_env:
+ AWS_ACCESS_KEY_ID: "{{ restic_backups_b2_key_id }}"
+ AWS_SECRET_ACCESS_KEY: "{{ restic_backups_b2_key_secret }}"
+ RESTIC_REPOSITORY: "{{ restic_backups_repository }}"
+ RESTIC_PASSWORD: "{{ restic_backups_password }}"
+ RESTIC_CACHE_DIR: "/var/cache/restic"