- 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"