From 928d73d2688d956a0de4b6db521fb79537a7bd27 Mon Sep 17 00:00:00 2001 From: Karl DeBisschop Date: Sat, 27 Jan 2024 17:49:08 -0500 Subject: [PATCH] quote BACKUP_SOURCES, add service_options --- README.md | 18 ++++++++++++++---- templates/restic.service.j2 | 5 +++++ templates/restic_script_Linux.j2 | 21 +++++++++++++-------- 3 files changed, 32 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 96d7b5d..75a5fc8 100644 --- a/README.md +++ b/README.md @@ -153,13 +153,14 @@ Available variables: | ------------------ |:-----------------------------:| ------------ | | `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 (unless `stdin` == `true`) | 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)? | +| `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_cmd` | no (yes if `stdin` == `true`) | The command to produce the stdin. | | `stdin_filename` | no | The filename used in the repository. | | `pre_backup_cmd` | no | A command to run before backup, typically used to dump databases to disk | | `tags` | no | Array of default tags | -| `options` | no | Array of additional options to restic backup | +| `options` | no (see Note) | 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_hourly` | no | If set, only keeps the last n hourly snapshots. | | `keep_daily` | no | If set, only keeps the last n daily snapshots. | @@ -183,6 +184,8 @@ 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=` | | `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. + Example: ```yaml restic_backups: @@ -208,7 +211,14 @@ restic_backups: scheduled: true schedule_oncalendar: '*-*-* 02:00:00' pre_backup_cmd: cd /var/dumped_data && mariadb -N -e 'show databases' | while read dbname; do mariadb-dump --complete-insert --routines --triggers --single-transaction "$dbname" > "$dbname".sql; done - + specified_files: + name: specified_files + repo: remote + options: + - --files-from + - /etc/restic-specified-files.txt + scheduled: true + schedule_oncalendar: '*-*-* 03:00:00' ``` > You can also specify restic_backups as an array, which is a legacy feature and diff --git a/templates/restic.service.j2 b/templates/restic.service.j2 index 1d35d41..3ac079e 100644 --- a/templates/restic.service.j2 +++ b/templates/restic.service.j2 @@ -7,6 +7,11 @@ After=fstrim.timer [Service] Type=oneshot +{% if item.service_options is defined %} +{% for key, value in item.service_options.items() %} +{{key}}={{value}} +{% endfor %} +{% endif %} {% if item.lvm is defined %} PrivateMounts=on {% endif %} diff --git a/templates/restic_script_Linux.j2 b/templates/restic_script_Linux.j2 index 9107d04..9bfbbf2 100644 --- a/templates/restic_script_Linux.j2 +++ b/templates/restic_script_Linux.j2 @@ -79,10 +79,10 @@ export B2_ACCOUNT_ID={{ restic_repos[item.repo].b2_account_id }} export B2_ACCOUNT_KEY={{ restic_repos[item.repo].b2_account_key }} {% endif %} {% if item.src is defined and item.src is string %} -BACKUP_SOURCE={{ item.src }} +BACKUP_SOURCE="{{ item.src }}" {% endif %} {% if item.src is defined and item.src.__class__.__name__ =='list' %} -BACKUP_SOURCE={{ item.src| join(' ') }} +BACKUP_SOURCE="{{ item.src| join(' ') }}" {% endif %} {% if item.lvm is defined %} @@ -203,9 +203,11 @@ set -uxo pipefail {%- endmacro %} {% macro options(options) %} - {% for option in options %} - {{ option }} \ - {% endfor %} + {% if options is defined %} + {% for option in options %} + {{ option }} \ + {% endfor %} + {% endif %} {% endmacro %} {% macro exclude(exclude) %} @@ -290,7 +292,7 @@ case $? in *) echo "$(date -u '+%Y-%m-%d %H:%M:%S') ERROR" {{ backup_result_log }} {% if item.mail_on_error is defined and item.mail_on_error == true %} - mail -s "restic backup failed on {{ ansible_hostname }}" {{ item.mail_address }} <<< "Something went wrong while running restic backup script running at {{ ansible_hostname }} at $(date -u '+%Y-%m-%d %H:%M:%S'). + mail -s "restic backup failed on {{ ansible_hostname }}" {{ item.mail_address }} <<< "Something went wrong in backups while running restic backup script running at {{ ansible_hostname }} at $(date -u '+%Y-%m-%d %H:%M:%S'). {%- if item.src is defined -%} {{ ' ' }}We tried to backup '{{ item.src }}'. {%- endif -%} @@ -306,6 +308,7 @@ esac {# Define stdin forget commands #} +{% if item.forget_snapshots is not defined or item.forget_snapshots %} {{ 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 ]] then @@ -316,12 +319,14 @@ then else echo "$(date -u '+%Y-%m-%d %H:%M:%S') ERROR" {{ forget_result_log }} {% if item.mail_on_error is defined and item.mail_on_error == true %} - mail -s "restic backup failed on {{ ansible_hostname }}" {{ item.mail_address }} <<< "Something went wrong while running restic backup script running at {{ ansible_hostname }} at $(date -u '+%Y-%m-%d %H:%M:%S'). + mail -s "restic backup failed on {{ ansible_hostname }}" {{ item.mail_address }} <<< "Something went wrong in forgetting while running restic backup script running at {{ ansible_hostname }} at $(date -u '+%Y-%m-%d %H:%M:%S'). {%- if item.src is defined -%} - {{ ' ' }}We tried to backup '{{ item.src }}'. + {{ ' ' }}We tried to forget '{{ item.src }}'. {%- endif -%} {{ ' ' }}Please repair the restic-{{ item.name | replace(' ', '') }} job." {% endif %} fi +{% endif %} + rm -f $pid # remove pid file just before exiting exit