Skip to content

Commit

Permalink
only run the backup if restic_backup_sets and restic_backup_rotation …
Browse files Browse the repository at this point in the history
…is set on the host
  • Loading branch information
zefanjajobse committed Jan 11, 2025
1 parent 94695a4 commit a19a4c7
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
---
- name: Test the connection
ansible.builtin.ping:

- name: Include the secret file
ansible.builtin.include_vars:
file: secret.yml

- name: Update apt and install restic for backups
ansible.builtin.apt:
name: "{{ restic_client_package }}"
update_cache: true

- name: Check if repo is initialized
shell: restic snapshots
environment:
RESTIC_PASSWORD: "{{ restic_repo_password }}"
RESTIC_REPOSITORY: "rest:http://{{ restic_client_user }}:{{ restic_client_password }}@{{ restic_repo }}"
ignore_errors: yes
changed_when: false
register: repo_initalized

- name: Init restic repository
shell: restic init
environment:
RESTIC_PASSWORD: "{{ restic_repo_password }}"
RESTIC_REPOSITORY: "rest:http://{{ restic_client_user }}:{{ restic_client_password }}@{{ restic_repo }}"
when: repo_initalized.failed

- name: Ensure restic environment vars exists
lineinfile:
dest: "/etc/environment"
state: present
regexp: "^{{ item.key }}="
line: "{{ item.key }}={{ item.value}}"
loop:
- key: RESTIC_PASSWORD
value: "{{ restic_repo_password }}"
- key: RESTIC_REPOSITORY
value: "rest:http://{{ restic_client_user }}:{{ restic_client_password }}@{{ restic_repo }}"

- name: Register docker volume backup jobs
cron:
name: "Backup job {{ item.id }}"
hour: "{{ item.hour }}"
minute: "{{ item.minute }}"
job: ". /etc/environment; restic backup /var/lib/docker/volumes/{{ item.volume }}/_data/ --tag {{ item.tags | join(' --tag ')}}"
loop: "{{ restic_backup_sets }}"
when: item.type == "docker-volume"

- name: Register backup rotation job
cron:
name: "Backup rotation job"
hour: "23"
minute: "0"
job: ". /etc/environment; restic forget --keep-daily {{ restic_backup_rotation.daily }} --keep-weekly {{ restic_backup_rotation.weekly }} --keep-monthly {{ restic_backup_rotation.monthly }} --prune"
Original file line number Diff line number Diff line change
@@ -1,56 +1,4 @@
---
- name: Test the connection
ansible.builtin.ping:

- name: Include the secret file
ansible.builtin.include_vars:
file: secret.yml

- name: Update apt and install restic for backups
ansible.builtin.apt:
name: "{{ restic_client_package }}"
update_cache: true

- name: Check if repo is initialized
shell: restic snapshots
environment:
RESTIC_PASSWORD: "{{ restic_repo_password }}"
RESTIC_REPOSITORY: "rest:http://{{ restic_client_user }}:{{ restic_client_password }}@{{ restic_repo }}"
ignore_errors: yes
changed_when: false
register: repo_initalized

- name: Init restic repository
shell: restic init
environment:
RESTIC_PASSWORD: "{{ restic_repo_password }}"
RESTIC_REPOSITORY: "rest:http://{{ restic_client_user }}:{{ restic_client_password }}@{{ restic_repo }}"
when: repo_initalized.failed

- name: Ensure restic environment vars exists
lineinfile:
dest: "/etc/environment"
state: present
regexp: "^{{ item.key }}="
line: "{{ item.key }}={{ item.value}}"
loop:
- key: RESTIC_PASSWORD
value: "{{ restic_repo_password }}"
- key: RESTIC_REPOSITORY
value: "rest:http://{{ restic_client_user }}:{{ restic_client_password }}@{{ restic_repo }}"

- name: Register docker volume backup jobs
cron:
name: "Backup job {{ item.id }}"
hour: "{{ item.hour }}"
minute: "{{ item.minute }}"
job: ". /etc/environment; restic backup /var/lib/docker/volumes/{{ item.volume }}/_data/ --tag {{ item.tags | join(' --tag ')}}"
loop: "{{ restic_backup_sets }}"
when: item.type == "docker-volume"

- name: Register backup rotation job
cron:
name: "Backup rotation job"
hour: "23"
minute: "0"
job: ". /etc/environment; restic forget --keep-daily {{ restic_backup_rotation.daily }} --keep-weekly {{ restic_backup_rotation.weekly }} --keep-monthly {{ restic_backup_rotation.monthly }} --prune"
- name: Run main task if action is set
ansible.builtin.include_tasks: backup.yml
when: restic_backup_sets is defined and restic_backup_rotation is defined

0 comments on commit a19a4c7

Please sign in to comment.