Make one env optional and warn about optional missing envs (#110)
* Make one env optional and warn about optional missing envs * Restrict visibility of var * Fix warning * Dont print empty warning list
This commit is contained in:
29
bin/restic_backup.sh
Normal file → Executable file
29
bin/restic_backup.sh
Normal file → Executable file
@@ -33,17 +33,38 @@ assert_envvars() {
|
|||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
|
warn_on_missing_envvars() {
|
||||||
|
local unset_envs=()
|
||||||
|
local varnames=("$@")
|
||||||
|
for varname in "${varnames[@]}"; do
|
||||||
|
if [ -z "${!varname}" ]; then
|
||||||
|
unset_envs=("${unset_envs[@]}" "$varname")
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
if [ ${#unset_envs[@]} -gt 0 ]; then
|
||||||
|
printf "The following env variables are recommended, but have not been set. This script may not work as expected: %s\n" "${unset_envs[*]}" >&2
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
assert_envvars \
|
assert_envvars \
|
||||||
B2_ACCOUNT_ID B2_ACCOUNT_KEY B2_CONNECTIONS \
|
|
||||||
RESTIC_BACKUP_PATHS RESTIC_BACKUP_TAG \
|
RESTIC_BACKUP_PATHS RESTIC_BACKUP_TAG \
|
||||||
RESTIC_BACKUP_EXCLUDE_FILE RESTIC_BACKUP_EXTRA_ARGS RESTIC_PASSWORD_FILE RESTIC_REPOSITORY RESTIC_VERBOSITY_LEVEL \
|
RESTIC_BACKUP_EXCLUDE_FILE RESTIC_BACKUP_EXTRA_ARGS RESTIC_REPOSITORY RESTIC_VERBOSITY_LEVEL \
|
||||||
RESTIC_RETENTION_DAYS RESTIC_RETENTION_MONTHS RESTIC_RETENTION_WEEKS RESTIC_RETENTION_YEARS
|
RESTIC_RETENTION_DAYS RESTIC_RETENTION_MONTHS RESTIC_RETENTION_WEEKS RESTIC_RETENTION_YEARS
|
||||||
|
|
||||||
|
warn_on_missing_envvars \
|
||||||
|
B2_ACCOUNT_ID B2_ACCOUNT_KEY B2_CONNECTIONS \
|
||||||
|
RESTIC_PASSWORD_FILE
|
||||||
|
|
||||||
|
|
||||||
# Convert to arrays, as arrays should be used to build command lines. See https://github.com/koalaman/shellcheck/wiki/SC2086
|
# Convert to arrays, as arrays should be used to build command lines. See https://github.com/koalaman/shellcheck/wiki/SC2086
|
||||||
IFS=':' read -ra backup_paths <<< "$RESTIC_BACKUP_PATHS"
|
IFS=':' read -ra backup_paths <<< "$RESTIC_BACKUP_PATHS"
|
||||||
IFS=' ' read -ra extra_args <<< "$RESTIC_BACKUP_EXTRA_ARGS"
|
IFS=' ' read -ra extra_args <<< "$RESTIC_BACKUP_EXTRA_ARGS"
|
||||||
|
|
||||||
|
B2_ARG=
|
||||||
|
[ -z "${B2_CONNECTIONS+x}" ] || B2_ARG=(--option b2.connections="$B2_CONNECTIONS")
|
||||||
|
|
||||||
# If you need to run some commands before performing the backup; create this file, put them there and make the file executable.
|
# If you need to run some commands before performing the backup; create this file, put them there and make the file executable.
|
||||||
PRE_SCRIPT="${INSTALL_PREFIX}/etc/restic/pre_backup.sh"
|
PRE_SCRIPT="${INSTALL_PREFIX}/etc/restic/pre_backup.sh"
|
||||||
test -x "$PRE_SCRIPT" && "$PRE_SCRIPT"
|
test -x "$PRE_SCRIPT" && "$PRE_SCRIPT"
|
||||||
@@ -81,7 +102,7 @@ restic backup \
|
|||||||
--verbose="$RESTIC_VERBOSITY_LEVEL" \
|
--verbose="$RESTIC_VERBOSITY_LEVEL" \
|
||||||
$FS_ARG \
|
$FS_ARG \
|
||||||
--tag "$RESTIC_BACKUP_TAG" \
|
--tag "$RESTIC_BACKUP_TAG" \
|
||||||
--option b2.connections="$B2_CONNECTIONS" \
|
"${B2_ARG[@]}" \
|
||||||
"${exclusion_args[@]}" \
|
"${exclusion_args[@]}" \
|
||||||
"${extra_args[@]}" \
|
"${extra_args[@]}" \
|
||||||
"${backup_paths[@]}" &
|
"${backup_paths[@]}" &
|
||||||
@@ -93,7 +114,7 @@ wait $!
|
|||||||
restic forget \
|
restic forget \
|
||||||
--verbose="$RESTIC_VERBOSITY_LEVEL" \
|
--verbose="$RESTIC_VERBOSITY_LEVEL" \
|
||||||
--tag "$RESTIC_BACKUP_TAG" \
|
--tag "$RESTIC_BACKUP_TAG" \
|
||||||
--option b2.connections="$B2_CONNECTIONS" \
|
"${B2_ARG[@]}" \
|
||||||
--prune \
|
--prune \
|
||||||
--group-by "paths,tags" \
|
--group-by "paths,tags" \
|
||||||
--keep-hourly "$RESTIC_RETENTION_HOURS" \
|
--keep-hourly "$RESTIC_RETENTION_HOURS" \
|
||||||
|
|||||||
27
bin/restic_check.sh
Normal file → Executable file
27
bin/restic_check.sh
Normal file → Executable file
@@ -25,10 +25,29 @@ assert_envvars() {
|
|||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
assert_envvars \
|
|
||||||
B2_ACCOUNT_ID B2_ACCOUNT_KEY B2_CONNECTIONS \
|
warn_on_missing_envvars() {
|
||||||
|
local unset_envs=()
|
||||||
|
local varnames=("$@")
|
||||||
|
for varname in "${varnames[@]}"; do
|
||||||
|
if [ -z "${!varname}" ]; then
|
||||||
|
unset_envs=("${unset_envs[@]}" "$varname")
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
if [ ${#unset_envs[@]} -gt 0 ]; then
|
||||||
|
printf "The following env variables are recommended, but have not been set. This script may not work as expected: %s\n" "${unset_envs[*]}" >&2
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
assert_envvars\
|
||||||
RESTIC_PASSWORD_FILE RESTIC_REPOSITORY RESTIC_VERBOSITY_LEVEL
|
RESTIC_PASSWORD_FILE RESTIC_REPOSITORY RESTIC_VERBOSITY_LEVEL
|
||||||
|
|
||||||
|
warn_on_missing_envvars \
|
||||||
|
B2_ACCOUNT_ID B2_ACCOUNT_KEY B2_CONNECTIONS
|
||||||
|
|
||||||
|
B2_ARG=
|
||||||
|
[ -z "${B2_CONNECTIONS+x}" ] || B2_ARG=(--option b2.connections="$B2_CONNECTIONS")
|
||||||
|
|
||||||
# Remove locks from other stale processes to keep the automated backup running.
|
# Remove locks from other stale processes to keep the automated backup running.
|
||||||
# NOTE nope, don't unlock like restic_backup.sh. restic_backup.sh should take precedence over this script.
|
# NOTE nope, don't unlock like restic_backup.sh. restic_backup.sh should take precedence over this script.
|
||||||
@@ -36,7 +55,7 @@ assert_envvars \
|
|||||||
#wait $!
|
#wait $!
|
||||||
|
|
||||||
# Check repository for errors.
|
# Check repository for errors.
|
||||||
restic check \
|
echo restic check \
|
||||||
--option b2.connections="$B2_CONNECTIONS" \
|
"${B2_ARG[@]}" \
|
||||||
--verbose="$RESTIC_VERBOSITY_LEVEL" &
|
--verbose="$RESTIC_VERBOSITY_LEVEL" &
|
||||||
wait $!
|
wait $!
|
||||||
|
|||||||
Reference in New Issue
Block a user