Assert that all envvars are set in scripts

Remind user to source profile before executing.

Fixes #62
This commit is contained in:
Erik Westrup
2022-02-01 16:45:19 +01:00
parent f4b90c2499
commit 687111fddf
3 changed files with 39 additions and 2 deletions

View File

@@ -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).

View File

@@ -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.

View File

@@ -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.