From d97bbf8fcef12b1bc4ba8e2beb6614b9d4b83721 Mon Sep 17 00:00:00 2001 From: Matthias Leutenegger Date: Wed, 11 Sep 2019 11:29:36 +0200 Subject: [PATCH] some fixing of the tag keeping --- README.md | 4 ++-- templates/restic_script_Linux.j2 | 25 ++++++++++++++++--------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 9d97cf6..1c1c251 100644 --- a/README.md +++ b/README.md @@ -112,7 +112,7 @@ Available variables: | Name | Required (Default) | Description | | ------------------ |:-----------------------------:| ----------------------------------------------------------------------------------------------------------------------- | -| `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. | | `src` | yes | The source directory or file | | `stdin` | no | Is this backup created from a [stdin](https://restic.readthedocs.io/en/stable/040_backup.html#reading-data-from-stdin)? | @@ -126,7 +126,7 @@ Available variables: | `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_within` | no | If set, only keeps snapshots in this time period. | -| `keep_tag` | no | If set, keep snapshots with this tags. | +| `keep_tag` | no | If set, keep snapshots with this tags. Make sure to specify a list. | | `scheduled` | no (`false`) | If `restic_create_cron` is set to `true`, this backup is scheduled. | | `schedule_minute` | no (`*`) | Minute when the job is run. ( 0-59, *, */2, etc ) | | `schedule_hour` | no (`*`) | Hour when the job is run. ( 0-23, *, */2, etc ) | diff --git a/templates/restic_script_Linux.j2 b/templates/restic_script_Linux.j2 index 29db11c..20ebe7a 100644 --- a/templates/restic_script_Linux.j2 +++ b/templates/restic_script_Linux.j2 @@ -9,11 +9,18 @@ BACKUP_NAME={{ item.name }} {% if item.src is defined %} BACKUP_SOURCE={{ item.src }} {% endif %} +set -euxo pipefail {# Define Tags #} {% macro tags(tags) -%} - {% if tags is defined %}{% for tag in tags %} --tag {{ tag }}{% endfor %}{% endif %} + {% if tags is defined and (tags|length>0) %}{% for tag in tags %} --tag {{ tag }}{% endfor %}{% endif %} +{%- endmacro %} +{# + Define Keeped Tags +#} +{% macro keep_tags(tags) -%} + {% if tags is defined and (tags|length>0) %}{% for tag in tags %} --keep-tag {{ tag }}{% endfor %}{% endif %} {%- endmacro %} {# Define Hostname @@ -37,14 +44,14 @@ BACKUP_SOURCE={{ item.src }} Define retention pattern #} {% macro retention_pattern(repo) -%} - {% if repo.keep_last is defined %}--keep-last {{ item.keep_last }}{% endif %} \ - {% if repo.keep_hourly is defined %}--keep-hourly {{ item.keep_hourly }}{% endif %} \ - {% if repo.keep_daily is defined %}--keep-daily {{ item.keep_daily }}{% endif %} \ - {% if repo.keep_weekly is defined %}--keep-weekly {{ item.keep_weekly }}{% endif %} \ - {% if repo.keep_monthly is defined %}--keep-monthly {{ item.keep_monthly }}{% endif %} \ - {% if repo.keep_yearly is defined %}--keep-yearly {{ item.keep_yearly }}{% endif %} \ - {% if repo.keep_within is defined %}--keep-within {{ item.keep_within }}{% endif %} \ - {% if repo.keep_tag is defined %}--keep-tag {{ item.keep_tag }}{% 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_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_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_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 %} {%- endmacro %} {# Define backup commands