1
0
Fork 0
mirror of https://github.com/roles-ansible/ansible_role_restic.git synced 2024-12-11 23:41:32 +01:00

only forget if keep_* is defined, require src, stdin_cmd, or include to build script

This commit is contained in:
Karl DeBisschop 2024-01-28 08:50:49 -05:00
parent 928d73d268
commit 4fe4a2a7ac
3 changed files with 38 additions and 22 deletions

View file

@ -153,14 +153,15 @@ Available variables:
| ------------------ |:-----------------------------:| ------------ | | ------------------ |:-----------------------------:| ------------ |
| `name` | yes | The name of this backup. Used together with pruning and scheduling and needs to be unique. | | `name` | yes | The name of this backup. Used together with pruning and scheduling and needs to be unique. |
| `repo` | yes | The name of the repository to backup to. | | `repo` | yes | The name of the repository to backup to. |
| `src` | no (see Note) | The source directory or file | | `src` | no [see Note] | The source directory or file |
| `stdin` | no (see Note) | Is this backup created from a [stdin](https://restic.readthedocs.io/en/stable/040_backup.html#reading-data-from-stdin)? | | `stdin` | no [see Note] | Is this backup created from a [stdin](https://restic.readthedocs.io/en/stable/040_backup.html#reading-data-from-stdin)? |
| `stdin_cmd` | no (yes if `stdin` == `true`) | The command to produce the stdin. | | `stdin_cmd` | no (yes if `stdin` == `true`) | The command to produce the stdin. |
| `stdin_filename` | no | The filename used in the repository. | | `stdin_filename` | no | The filename used in the repository. |
| `include` | no (`{}`) [see Note] | If set, used to specify --files-from, --files-from-verbatim, and/or --files-from-raw. |
| `exclude` | no (`{}`) | Allows you to specify files to exclude. See [Exclude](#exclude) for reference. |
| `pre_backup_cmd` | no | A command to run before backup, typically used to dump databases to disk | | `pre_backup_cmd` | no | A command to run before backup, typically used to dump databases to disk |
| `tags` | no | Array of default tags | | `tags` | no | Array of default tags |
| `options` | no (see Note) | Array of additional options to restic backup | | `options` | no | Array of additional options to restic backup |
| `forget_snapshots` | no | If set to false, do not forget snapshots with each run (
| `keep_last` | no | If set, only keeps the last n snapshots. | | `keep_last` | no | If set, only keeps the last n snapshots. |
| `keep_hourly` | no | If set, only keeps the last n hourly snapshots. | | `keep_hourly` | no | If set, only keeps the last n hourly snapshots. |
| `keep_daily` | no | If set, only keeps the last n daily snapshots. | | `keep_daily` | no | If set, only keeps the last n daily snapshots. |
@ -168,7 +169,7 @@ Available variables:
| `keep_monthly` | no | If set, only keeps the last n monthly snapshots. | | `keep_monthly` | no | If set, only keeps the last n monthly snapshots. |
| `keep_yearly ` | no | If set, only keeps the last n yearly snapshots. | | `keep_yearly ` | no | If set, only keeps the last n yearly snapshots. |
| `keep_within` | no | If set, only keeps snapshots in this time period. | | `keep_within` | no | If set, only keeps snapshots in this time period. |
| `keep_tag` | no | If set, keep snapshots with this tags. Make sure to specify a list. | | `keep_tag` | no | If set, keep snapshots with these tags. Make sure to specify a list. |
| `prune` | no (`false`) | If `true`, the `restic forget` command in the script has the [`--prune` option](https://restic.readthedocs.io/en/stable/060_forget.html#removing-backup-snapshots) appended. | | `prune` | no (`false`) | If `true`, the `restic forget` command in the script has the [`--prune` option](https://restic.readthedocs.io/en/stable/060_forget.html#removing-backup-snapshots) appended. |
| `scheduled` | no (`false`) | If `restic_create_schedule` is set to `true`, this backup is scheduled and tries to create a systemd timer unit. If it fails, it is creating a cronjob. | | `scheduled` | no (`false`) | If `restic_create_schedule` is set to `true`, this backup is scheduled and tries to create a systemd timer unit. If it fails, it is creating a cronjob. |
| `schedule_oncalendar` | ``'*-*-* 02:00:00'`` | The time for the systemd timer. Please notice the randomDelaySec option. By Default the backup is done every night at 2 am (+0-4h). But only if scheduled is true. | | `schedule_oncalendar` | ``'*-*-* 02:00:00'`` | The time for the systemd timer. Please notice the randomDelaySec option. By Default the backup is done every night at 2 am (+0-4h). But only if scheduled is true. |
@ -176,7 +177,6 @@ Available variables:
| `schedule_hour` | no (`2`) | Hour when the job is run. ( 0-23, *, */2, etc ) | | `schedule_hour` | no (`2`) | Hour when the job is run. ( 0-23, *, */2, etc ) |
| `schedule_weekday` | no (`*`) | Weekday when the job is run. ( 0-6 for Sunday-Saturday, *, etc ) | | `schedule_weekday` | no (`*`) | Weekday when the job is run. ( 0-6 for Sunday-Saturday, *, etc ) |
| `schedule_month` | no (`*`) | Month when the job is run. ( 1-12, *, */2, etc ) | | `schedule_month` | no (`*`) | Month when the job is run. ( 1-12, *, */2, etc ) |
| `exclude` | no (`{}`) | Allows you to specify files to exclude. See [Exclude](#exclude) for reference. |
| `disable_logging` | no | Optionally disable logging | | `disable_logging` | no | Optionally disable logging |
| `log_to_journald` | no | Optionally switch logging to journald with the name of the backup job as the tag | | `log_to_journald` | no | Optionally switch logging to journald with the name of the backup job as the tag |
| `mail_on_error` | no | Optionally send a mail if the backupjob will fail *(mailx is required)* | | `mail_on_error` | no | Optionally send a mail if the backupjob will fail *(mailx is required)* |
@ -184,7 +184,7 @@ Available variables:
| `monitoring_call` | no | A command that will be called if the backup is *successful*. Useful for heartbeat monitoring systems that warn when no heartbeat is received. Use the full command, you need to run. Example: `curl https://monitoring.example.com/api/push/E9Wzm4lJ2O?status=up&msg=OK&ping=` | | `monitoring_call` | no | A command that will be called if the backup is *successful*. Useful for heartbeat monitoring systems that warn when no heartbeat is received. Use the full command, you need to run. Example: `curl https://monitoring.example.com/api/push/E9Wzm4lJ2O?status=up&msg=OK&ping=` |
| `niceness` | no | If set, runs any scheduled backup with given [niceness-value](https://en.wikipedia.org/wiki/Nice_(Unix)). On Linux -20 is highest priority, 0 default and 19 is lowest priority. 10 is a common low priority assigned to backup routines on production systems. | | `niceness` | no | If set, runs any scheduled backup with given [niceness-value](https://en.wikipedia.org/wiki/Nice_(Unix)). On Linux -20 is highest priority, 0 default and 19 is lowest priority. 10 is a common low priority assigned to backup routines on production systems. |
Note: One of src or stdin_cmd must be defined; or one of --files-from, --files-from-verbatim, or --files-from-raw must be provided in options. Note: One of src, stdin_cmd, or include must be defined. If include is used, you are responsible for providing one or more of of files_from, files_rom_verbatim, or files_from_raw.
Example: Example:
```yaml ```yaml

View file

@ -16,8 +16,7 @@
with_items: '{{ restic_backups }}' with_items: '{{ restic_backups }}'
when: when:
- item.name is defined - item.name is defined
- item.src is defined or item.stdin is defined - item.src is defined or item.include is defined or item.stdin and item.stdin_cmd is defined
- item.src is defined or item.stdin and item.stdin_cmd is defined
- item.repo in restic_repos - item.repo in restic_repos
- name: (BACKUP) Create backup script - name: (BACKUP) Create backup script
@ -31,6 +30,5 @@
with_items: '{{ restic_backups }}' with_items: '{{ restic_backups }}'
when: when:
- item.name is defined - item.name is defined
- item.src is defined or item.stdin is defined - item.src is defined or item.include is defined or item.stdin and item.stdin_cmd is defined
- item.src is defined or item.stdin and item.stdin_cmd is defined
- item.repo in restic_repos - item.repo in restic_repos

View file

@ -186,20 +186,20 @@ set -uxo pipefail
Define paths Define paths
#} #}
{% macro paths(repo) -%} {% macro paths(repo) -%}
{% if repo.src is defined and repo.src != None and (repo.src is not string) %}{%for path in repo.src %} --path {{ path }}{% endfor %}{%elif repo.src is string %}--path {{repo.src}} {% else %} --path {{ repo.stdin_filename }}{% endif %} {% if repo.src is defined and repo.src != None and (repo.src is not string) %}{%for path in repo.src %} --path {{ path }}{% endfor %}{%elif repo.src is string %}--path {{repo.src}} {% elif repo.stdin_filename is defined %} --path {{ repo.stdin_filename }}{% endif %}
{%- endmacro %} {%- endmacro %}
{# {#
Define retention pattern Define retention pattern (if non-empty, `restic forget` will run)
#} #}
{% macro retention_pattern(repo) -%} {% macro retention_pattern(repo) -%}
{% if repo.keep_last is defined and repo.keep_last != None %}--keep-last {{ item.keep_last }}{% endif %} \ {% if repo.keep_last is defined and repo.keep_last != None %}--keep-last {{ item.keep_last }} {% endif %}
{% if repo.keep_hourly is defined and repo.keep_hourly != None %}--keep-hourly {{ item.keep_hourly }}{% endif %} \ {% if repo.keep_hourly is defined and repo.keep_hourly != None %}--keep-hourly {{ item.keep_hourly }} {% endif %}
{% if repo.keep_daily is defined and repo.keep_daily != None %}--keep-daily {{ item.keep_daily }}{% endif %} \ {% if repo.keep_daily is defined and repo.keep_daily != None %}--keep-daily {{ item.keep_daily }} {% endif %}
{% if repo.keep_weekly is defined and repo.keep_weekly != None %}--keep-weekly {{ item.keep_weekly }}{% endif %} \ {% if repo.keep_weekly is defined and repo.keep_weekly != None %}--keep-weekly {{ item.keep_weekly }} {% endif %}
{% if repo.keep_monthly is defined and repo.keep_monthly != None %}--keep-monthly {{ item.keep_monthly }}{% endif %} \ {% if repo.keep_monthly is defined and repo.keep_monthly != None %}--keep-monthly {{ item.keep_monthly }} {% endif %}
{% if repo.keep_yearly is defined and repo.keep_yearly != None %}--keep-yearly {{ item.keep_yearly }}{% endif %} \ {% if repo.keep_yearly is defined and repo.keep_yearly != None %}--keep-yearly {{ item.keep_yearly }} {% endif %}
{% if repo.keep_within is defined and repo.keep_within != None %}--keep-within {{ item.keep_within }}{% endif %} \ {% if repo.keep_within is defined and repo.keep_within != None %}--keep-within {{ item.keep_within }} {% endif %}
{% if repo.keep_tag is defined and (repo.keep_tag|length>0) %}{{ keep_tags(repo.keep_tag) }}{% endif %} {% if repo.keep_tag is defined and (repo.keep_tag|length>0) %}{{ keep_tags(repo.keep_tag) }} {% endif %}
{%- endmacro %} {%- endmacro %}
{% macro options(options) %} {% macro options(options) %}
@ -236,6 +236,23 @@ set -uxo pipefail
{% endif %} {% endif %}
{% endmacro %} {% endmacro %}
{% macro include(include) %}
{% if include.files_from is defined %}
{% for path in include.files_from %}
--files-from {{ path }} \
{% endfor %}
{% endif %}
{% if include.files_from_verbatim is defined %}
{% for path in include.files_from_verbatim %}
--files-from-verbatim {{ path }} \
{% endfor %}
{% endif %}
{% if include.files_from_raw is defined %}
{% for path in include.files_from_raw %}
--files-from-raw {{ path }} \
{% endfor %}
{% endif %}
{% endmacro %}
{% if item.pre_backup_cmd is defined %} {% if item.pre_backup_cmd is defined %}
{{ item.pre_backup_cmd }} {{ pre_backup_cmd_output_log }} {{ item.pre_backup_cmd }} {{ pre_backup_cmd_output_log }}
@ -278,6 +295,7 @@ fi
{{ tags(item.tags) }} \ {{ tags(item.tags) }} \
{% if item.options is defined %}{{ options(item.options) }}{% endif %} \ {% if item.options is defined %}{{ options(item.options) }}{% endif %} \
{% if item.exclude is defined %}{{ exclude(item.exclude) }}{% endif %} \ {% if item.exclude is defined %}{{ exclude(item.exclude) }}{% endif %} \
{% if item.include is defined %}{{ include(item.include) }}{% endif %} \
$@ $@
} \ } \
{% endif %} {{ backup_output_log }} {% endif %} {{ backup_output_log }}
@ -308,7 +326,7 @@ esac
{# {#
Define stdin forget commands Define stdin forget commands
#} #}
{% if item.forget_snapshots is not defined or item.forget_snapshots %} {% if retention_pattern(item) %}
{{ restic_install_path }}/restic forget {{ paths(item) }} {{ retention_pattern(item) }} {% if item.prune is defined and item.prune == true %}--prune{% endif %} {{ forget_output_log }} {{ restic_install_path }}/restic forget {{ paths(item) }} {{ retention_pattern(item) }} {% if item.prune is defined and item.prune == true %}--prune{% endif %} {{ forget_output_log }}
if [[ $? -eq 0 ]] if [[ $? -eq 0 ]]
then then