Skip to content

Commit

Permalink
backup: rework crontab toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
saltydk committed Jul 5, 2023
1 parent 29fbd1d commit 2db13ff
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 27 deletions.
1 change: 0 additions & 1 deletion defaults/backup_config.yml.default
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
backup:
cron:
cron_time: weekly
enable: no
local:
destination: /mnt/local/Backups/Saltbox
enable: true
Expand Down
2 changes: 1 addition & 1 deletion roles/backup/tasks/cron.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@
job: >
sudo PATH='/usr/bin:/bin:/usr/local/bin' env ANSIBLE_CONFIG='{{ playbook_dir }}/ansible.cfg' '{{ playbook_binary.stdout }}'
{{ playbook_dir }}/backup.yml --tags backup >> '{{ playbook_dir }}/saltbox_backup.log' 2>&1
state: "{{ backup.cron.enable | ternary('present', 'absent') }}"
state: "{{ ('set-backup' in ansible_run_tags) | ternary('present', 'absent') }}"
user: "{{ user.name }}"
7 changes: 5 additions & 2 deletions roles/backup/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,16 @@
ansible.builtin.import_tasks: "variables.yml"
tags:
- set-backup
- unset-backup
- restore-service
- saltbox-restore-service

- name: Cron
ansible.builtin.import_tasks: "cron.yml"
when: ('set-backup' in ansible_run_tags) and not ('backup' in ansible_run_tags)
tags: set-backup
when: (('set-backup' in ansible_run_tags) or ('unset-backup' in ansible_run_tags)) and not ('backup' in ansible_run_tags)
tags:
- set-backup
- unset-backup

- name: Get Current Time
ansible.builtin.shell: "date \"+%s\""
Expand Down
1 change: 0 additions & 1 deletion roles/backup/tasks/variables.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
rsync: "{{ backup.rsync | combine(rsync_tmp) }}"
cron:
cron_time: "{{ backup.cron.cron_time }}"
enable: "{{ backup.cron.enable }}"
restore_service:
user: "{{ backup.restore_service.user }}"
pass: "{{ backup.restore_service.pass }}"
Expand Down
2 changes: 1 addition & 1 deletion roles/backup2/tasks/cron.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@
job: >
sudo PATH='/usr/bin:/bin:/usr/local/bin' env ANSIBLE_CONFIG='{{ playbook_dir }}/ansible.cfg' '{{ playbook_binary.stdout }}'
{{ playbook_dir }}/backup.yml --tags backup2 >> '{{ playbook_dir }}/saltbox_backup2.log' 2>&1
state: "{{ backup.cron.enable | ternary('present', 'absent') }}"
state: "{{ ('set-backup2' in ansible_run_tags) | ternary('present', 'absent') }}"
user: "{{ user.name }}"
7 changes: 5 additions & 2 deletions roles/backup2/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@
ansible.builtin.import_tasks: "variables.yml"
tags:
- set-backup2
- unset-backup2
- restore-service2
- saltbox-restore-service2

- name: Cron
ansible.builtin.import_tasks: "cron.yml"
when: ('set-backup2' in ansible_run_tags) and not ('backup2' in ansible_run_tags)
tags: set-backup2
when: (('set-backup2' in ansible_run_tags) or ('unset-backup2' in ansible_run_tags)) and not ('backup2' in ansible_run_tags)
tags:
- set-backup2
- unset-backup2

- name: Get Current Time
ansible.builtin.shell: "date \"+%s\""
Expand Down
1 change: 0 additions & 1 deletion roles/backup2/tasks/variables.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
rsync: "{{ backup.rsync | combine(rsync_tmp) }}"
cron:
cron_time: "{{ backup.cron.cron_time }}"
enable: "{{ backup.cron.enable }}"
restore_service:
user: "{{ backup.restore_service.user }}"
pass: "{{ backup.restore_service.pass }}"
Expand Down
1 change: 0 additions & 1 deletion roles/restore/tasks/variables.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
rsync: "{{ backup.rsync }}"
cron:
cron_time: "{{ backup.cron.cron_time }}"
enable: "{{ backup.cron.enable }}"
restore_service:
user: "{{ backup.restore_service.user }}"
pass: "{{ backup.restore_service.pass }}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,17 @@
---
- name: "Migrator | 'backup_config.yml' | Migration 01 | Set variables"
ansible.builtin.set_fact:
new_settings: "{{ not ((backup is undefined)
old_settings: "{{ (backup.cron.enable is defined)
or
(backup is none)
or
(backup | trim | length == 0)
or
(backup.cron is undefined)
or
(backup.cron is none)
or
(backup.cron | trim | length == 0)
or
(backup.cron.cron_state is defined)) }}"
(backup.cron.cron_state is defined) }}"

- name: Migrator | 'backup_config.yml' | Migration 01 | Rebuild 'backup.cron' dict
ansible.builtin.shell: |
yyq e -i 'del(.backup.cron.cron_state)' {{ playbook_dir }}/{{ file }}
yyq e -i '.backup.cron.enable = "{{
'yes'
if (backup is defined and backup.cron is defined and backup.cron.cron_state is defined and backup.cron.cron_state == "present")
else 'no' }}"' {{ playbook_dir }}/{{ file }}
yyq e -i 'del(.backup.cron.enable)' {{ playbook_dir }}/{{ file }}
become: true
become_user: "{{ saltbox_yml.stat.pw_name }}"
when: (not new_settings)
when: old_settings

- name: Migrator | 'backup_config.yml' | Migration 01 | Remove 'null' values
ansible.builtin.replace:
Expand Down

0 comments on commit 2db13ff

Please sign in to comment.