-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
only run the backup if restic_backup_sets and restic_backup_rotation …
…is set on the host
- Loading branch information
1 parent
94695a4
commit a19a4c7
Showing
2 changed files
with
59 additions
and
55 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
collections/ansible_collections/gametools/setup/roles/backup/tasks/backup.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
58 changes: 3 additions & 55 deletions
58
collections/ansible_collections/gametools/setup/roles/backup/tasks/main.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |