diff --git a/CHANGELOG.md b/CHANGELOG.md index 5adb01c..a5025e5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Allow extra arguments to restic-backup with `$RESTIC_BACKUP_EXTRA_ARGS`. - Add `$RESTIC_VERBOSITY_LEVEL` for debugging. +- Assertion on all needed envionment variables in the backup and check scripts. ### Changed - Align terminology used in README with the one used by B2 for credentials (keyId + applicationKey pair). diff --git a/usr/local/sbin/restic_backup.sh b/usr/local/sbin/restic_backup.sh index b4f19b3..59d16da 100644 --- a/usr/local/sbin/restic_backup.sh +++ b/usr/local/sbin/restic_backup.sh @@ -11,6 +11,27 @@ # Exit on error, unset var, pipe failure set -euo pipefail + +# Assert that all needed environment variables are set. +# TODO in future if this grows, move this to a restic_lib.sh +assert_envvars() { + local varnames=($@) + for varname in "${varnames[@]}"; do + # Check if variable is set, then if it is not empty (need to do both as of `set -u`). + if [ -z ${!varname+x} ] || [ -z ${!varname} ] ; then + printf "%s must be set with a value for this script to work.\n\nDid you forget to source a /etc/restic/*.env profile in the current shell before executing this script?\n" $varname >&2 + exit 1 + fi + done +} +assert_envvars \ + B2_ACCOUNT_ID B2_ACCOUNT_KEY B2_CONNECTIONS \ + BACKUP_PATHS BACKUP_TAG \ + RESTIC_BACKUP_EXCLUDE_FILE RESTIC_BACKUP_EXTRA_ARGS RESTIC_PASSWORD_FILE RESTIC_REPOSITORY RESTIC_VERBOSITY_LEVEL \ + RETENTION_DAYS RETENTION_MONTHS RETENTION_WEEKS RETENTION_YEARS + + + # Clean up lock if we are killed. # If killed by systemd, like $(systemctl stop restic), then it kills the whole cgroup and all it's subprocesses. # However if we kill this script ourselves, we need this trap that kills all subprocesses manually. diff --git a/usr/local/sbin/restic_check.sh b/usr/local/sbin/restic_check.sh index 019677c..238e76e 100644 --- a/usr/local/sbin/restic_check.sh +++ b/usr/local/sbin/restic_check.sh @@ -2,8 +2,23 @@ # Check the backups made with restic to Backblaze B2 for errors. # See restic_backup.sh on how this script is run (as it's analogous for this script). -# Exit on failure, pipe failure -set -e -o pipefail +# Exit on error, unset var, pipe failure +set -euo pipefail + +# Assert that all needed environment variables are set. +assert_envvars() { + local varnames=($@) + for varname in "${varnames[@]}"; do + # Check if variable is set, then if it is not empty (need to do both as of `set -u`). + if [ -z ${!varname+x} ] || [ -z ${!varname} ] ; then + printf "%s must be set with a value for this script to work.\n\nDid you forget to source a /etc/restic/*.env profile in the current shell before executing this script?\n" $varname >&2 + exit 1 + fi + done +} +assert_envvars \ + B2_ACCOUNT_ID B2_ACCOUNT_KEY B2_CONNECTIONS \ + RESTIC_PASSWORD_FILE RESTIC_REPOSITORY RESTIC_VERBOSITY_LEVEL # Clean up lock if we are killed. # If killed by systemd, like $(systemctl stop restic), then it kills the whole cgroup and all it's subprocesses.