From 4740ebb5d300f48577fb2c58c10f05285ad509b5 Mon Sep 17 00:00:00 2001 From: Ben Date: Thu, 31 Aug 2023 08:53:46 -0400 Subject: [PATCH 1/6] Added option to run backup script immediately --- README.md | 43 ++++++++++++++++++++++--------------------- tasks/main.yml | 4 ++++ tasks/run_backup.yml | 4 ++++ 3 files changed, 30 insertions(+), 21 deletions(-) create mode 100644 tasks/run_backup.yml diff --git a/README.md b/README.md index 09f5f44..f88c0a4 100644 --- a/README.md +++ b/README.md @@ -65,27 +65,28 @@ ansible-galaxy install do1jlr.restic ## Role Variables -| Name | Default | Description | -| ---------------------- | ----------------------------------- | --------------------------------------------------------------------------- | -| `restic_url` | `undefined` | The URL to download restic from. Use this variable to overwrite the default | -| `restic_version` | `'0.15.1'` | The version of Restic to install | -| `restic_download_path` | `'/opt/restic'` | Download location for the restic binary | -| `restic_install_path` | `'/usr/local/bin'` | Install location for the restic binary | -| `restic_script_dir` | `'/opt/restic'` | Location of the generated backup scripts | -| `restic_log_dir` | `'{{ restic_script_dir }}/log'` | Location of the logs of the backup scripts | -| `restic_repos` | `{}` | A dictionary of repositories where snapshots are stored. *(More Info: [Repos](#Repos))* | -| `restic_backups` | `{}` (or `[]`) | A list of dictionaries specifying the files and directories to be backed up *(More Infos: [Backups](#Backups))* | -|`restic_create_schedule` | `false` | Should we schedule each backup? Either via cronjob or via systemd timer. | -| `restic_schedule_type` | `systemd` | Here you can define if we create a ``cronjob`` or a ``systemd`` timer. If it fails to create a systemd timer, a cronjob will be created. | -| `restic_dir_owner` | `'{{ansible_user}}'` | The owner of all created dirs | -| `restic_dir_group` | `'{{ansible_user}}'` | The group of all created dirs | -| `restic_no_log` | `true` | Set to false to see hidden ansible logs | -| `restic_do_not_cleanup_cron ` | `false` | We changed the cron location and clean up the old one. You can skip the cleanup here | -| `restic__cache_config` | `false` | Configure custom cache directory | -| `restic__cache_dir` | `'~/.cache/restic'` | Define custom cache directory | -|`submodules_versioncheck` | `false` | If you set this variable to true, the role will run a [simple versionscheck](tasks/versioncheck.yml) to prevent running older versions of this role. | -| `restic__limit_cpu_usage` | `false` | Should CPU usage be limited? | -| `restic__max_cpus` | `1` | Maximum number of CPUs that can be used simultaneously | +| Name | Default | Description | +|-------------------------------|---------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------| +| `restic_url` | `undefined` | The URL to download restic from. Use this variable to overwrite the default | +| `restic_version` | `'0.15.1'` | The version of Restic to install | +| `restic_download_path` | `'/opt/restic'` | Download location for the restic binary | +| `restic_install_path` | `'/usr/local/bin'` | Install location for the restic binary | +| `restic_script_dir` | `'/opt/restic'` | Location of the generated backup scripts | +| `restic_log_dir` | `'{{ restic_script_dir }}/log'` | Location of the logs of the backup scripts | +| `restic_repos` | `{}` | A dictionary of repositories where snapshots are stored. *(More Info: [Repos](#Repos))* | +| `restic_backups` | `{}` (or `[]`) | A list of dictionaries specifying the files and directories to be backed up *(More Infos: [Backups](#Backups))* | +| `restic_create_schedule` | `false` | Should we schedule each backup? Either via cronjob or via systemd timer. | +| `restic_backup_now` | `false` | Whether or not the backup script should be run immediately | +| `restic_schedule_type` | `systemd` | Here you can define if we create a ``cronjob`` or a ``systemd`` timer. If it fails to create a systemd timer, a cronjob will be created. | +| `restic_dir_owner` | `'{{ansible_user}}'` | The owner of all created dirs | +| `restic_dir_group` | `'{{ansible_user}}'` | The group of all created dirs | +| `restic_no_log` | `true` | Set to false to see hidden ansible logs | +| `restic_do_not_cleanup_cron ` | `false` | We changed the cron location and clean up the old one. You can skip the cleanup here | +| `restic__cache_config` | `false` | Configure custom cache directory | +| `restic__cache_dir` | `'~/.cache/restic'` | Define custom cache directory | +| `submodules_versioncheck` | `false` | If you set this variable to true, the role will run a [simple versionscheck](tasks/versioncheck.yml) to prevent running older versions of this role. | +| `restic__limit_cpu_usage` | `false` | Should CPU usage be limited? | +| `restic__max_cpus` | `1` | Maximum number of CPUs that can be used simultaneously | ### Repos Restic stores data in repositories. You have to specify at least one repository diff --git a/tasks/main.yml b/tasks/main.yml index 9852851..041aff6 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -33,3 +33,7 @@ distribution: '{{ ansible_distribution }}' distribution_version: '{{ ansible_distribution_version }}' distribution_major_version: '{{ ansible_distribution_major_version }}' + +- name: Run backups now + ansible.builtin.include_tasks: 'run_backup.yml' + when: restic_backup_now | bool diff --git a/tasks/run_backup.yml b/tasks/run_backup.yml new file mode 100644 index 0000000..1368d22 --- /dev/null +++ b/tasks/run_backup.yml @@ -0,0 +1,4 @@ +--- +- name: (RUN BACKUP) Run backup script + ansible.builtin.shell: + cmd: "{{ restic_script_dir }}/backup-{{ item.name | replace(' ', '') }}.sh" From 31a0197256129e3dc8ced5b93e8f8bb85c598bee Mon Sep 17 00:00:00 2001 From: Ben Date: Thu, 31 Aug 2023 08:57:54 -0400 Subject: [PATCH 2/6] Set restic_backup_now variable to false by default --- vars/defaults.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/vars/defaults.yml b/vars/defaults.yml index 52a34ce..c9eacf1 100644 --- a/vars/defaults.yml +++ b/vars/defaults.yml @@ -21,3 +21,4 @@ restic_system: '{{ ansible_system | lower }}' restic_url_v: 'v{{ restic_version }}/restic_{{ restic_version }}_' restic_file: '{{ restic_system }}_{{ restic_platform }}.bz2' restic_url_default: '{{ restic_url_r }}{{ restic_url_v }}{{ restic_file }}' +restic_backup_now: false From 35d890bb1e5736cb80aa75fd21d21d93a72ae950 Mon Sep 17 00:00:00 2001 From: Ben Date: Thu, 31 Aug 2023 09:00:19 -0400 Subject: [PATCH 3/6] Run backup with loop of restic_backups --- tasks/run_backup.yml | 4 ++++ tasks/run_backup.yml~ | 4 ++++ 2 files changed, 8 insertions(+) create mode 100644 tasks/run_backup.yml~ diff --git a/tasks/run_backup.yml b/tasks/run_backup.yml index 1368d22..9bc5eea 100644 --- a/tasks/run_backup.yml +++ b/tasks/run_backup.yml @@ -2,3 +2,7 @@ - name: (RUN BACKUP) Run backup script ansible.builtin.shell: cmd: "{{ restic_script_dir }}/backup-{{ item.name | replace(' ', '') }}.sh" + no_log: "{{ restic_no_log }}" + with_items: '{{ restic_backups }}' + when: + - item.name is defined diff --git a/tasks/run_backup.yml~ b/tasks/run_backup.yml~ new file mode 100644 index 0000000..1368d22 --- /dev/null +++ b/tasks/run_backup.yml~ @@ -0,0 +1,4 @@ +--- +- name: (RUN BACKUP) Run backup script + ansible.builtin.shell: + cmd: "{{ restic_script_dir }}/backup-{{ item.name | replace(' ', '') }}.sh" From 9ee20b675a994e743671844c0d88516c2aa2983d Mon Sep 17 00:00:00 2001 From: Ben Date: Thu, 31 Aug 2023 09:03:05 -0400 Subject: [PATCH 4/6] Remove tmp emacs file that was accidentally added --- tasks/run_backup.yml~ | 4 ---- 1 file changed, 4 deletions(-) delete mode 100644 tasks/run_backup.yml~ diff --git a/tasks/run_backup.yml~ b/tasks/run_backup.yml~ deleted file mode 100644 index 1368d22..0000000 --- a/tasks/run_backup.yml~ +++ /dev/null @@ -1,4 +0,0 @@ ---- -- name: (RUN BACKUP) Run backup script - ansible.builtin.shell: - cmd: "{{ restic_script_dir }}/backup-{{ item.name | replace(' ', '') }}.sh" From dec516c514d936529dc00529e473e947cb2c6815 Mon Sep 17 00:00:00 2001 From: Ben Date: Thu, 31 Aug 2023 09:11:12 -0400 Subject: [PATCH 5/6] Had restic_backup_now default assignment in the wrong file I think --- defaults/main.yml | 1 + vars/defaults.yml | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/defaults/main.yml b/defaults/main.yml index 5e33c73..fa24ba8 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -9,6 +9,7 @@ restic_repos: {} restic_backups: [] restic_create_schedule: "{{ restic_create_cron }}" restic_schedule_type: "systemd" +restic_backup_now: false # restic_schedule_type: "cronjob" restic_no_log: true diff --git a/vars/defaults.yml b/vars/defaults.yml index c9eacf1..52a34ce 100644 --- a/vars/defaults.yml +++ b/vars/defaults.yml @@ -21,4 +21,3 @@ restic_system: '{{ ansible_system | lower }}' restic_url_v: 'v{{ restic_version }}/restic_{{ restic_version }}_' restic_file: '{{ restic_system }}_{{ restic_platform }}.bz2' restic_url_default: '{{ restic_url_r }}{{ restic_url_v }}{{ restic_file }}' -restic_backup_now: false From 1bed8d3ae6d1b3d91835cd27842bc775d345caa7 Mon Sep 17 00:00:00 2001 From: Ben Date: Mon, 4 Sep 2023 22:25:13 -0400 Subject: [PATCH 6/6] Added changed_when and converted to builtin.command --- tasks/run_backup.yml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tasks/run_backup.yml b/tasks/run_backup.yml index 9bc5eea..abbcce9 100644 --- a/tasks/run_backup.yml +++ b/tasks/run_backup.yml @@ -1,8 +1,10 @@ --- - name: (RUN BACKUP) Run backup script - ansible.builtin.shell: - cmd: "{{ restic_script_dir }}/backup-{{ item.name | replace(' ', '') }}.sh" + ansible.builtin.command: + cmd: "/usr/bin/env sh {{ restic_script_dir }}/backup-{{ item.name | replace(' ', '') }}.sh" no_log: "{{ restic_no_log }}" with_items: '{{ restic_backups }}' when: - item.name is defined + register: result + changed_when: result.rc != 0