Run shellcheck on all shellscripts
This commit is contained in:
@@ -15,7 +15,7 @@ if [ $# -eq 0 ]; then
|
|||||||
echo "No program to run given!" >&2
|
echo "No program to run given!" >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
cmd="$@"
|
cmd="$*"
|
||||||
|
|
||||||
body=$(eval "$cmd" 2>&1)
|
body=$(eval "$cmd" 2>&1)
|
||||||
|
|
||||||
|
|||||||
@@ -15,11 +15,11 @@ set -euo pipefail
|
|||||||
# Assert that all needed environment variables are set.
|
# Assert that all needed environment variables are set.
|
||||||
# TODO in future if this grows, move this to a restic_lib.sh
|
# TODO in future if this grows, move this to a restic_lib.sh
|
||||||
assert_envvars() {
|
assert_envvars() {
|
||||||
local varnames=($@)
|
local varnames=("$@")
|
||||||
for varname in "${varnames[@]}"; do
|
for varname in "${varnames[@]}"; do
|
||||||
# Check if variable is set, then if it is not empty (need to do both as of `set -u`).
|
# 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
|
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
|
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
|
exit 1
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
@@ -48,7 +48,7 @@ trap exit_hook INT TERM
|
|||||||
exclusion_args="--exclude-file ${RESTIC_BACKUP_EXCLUDE_FILE}"
|
exclusion_args="--exclude-file ${RESTIC_BACKUP_EXCLUDE_FILE}"
|
||||||
## Self-contained backup files per backup path. E.g. having an USB disk at /mnt/media in RESTIC_BACKUP_PATHS,
|
## Self-contained backup files per backup path. E.g. having an USB disk at /mnt/media in RESTIC_BACKUP_PATHS,
|
||||||
# a file /mnt/media/.backup_exclude.txt will automatically be detected and used:
|
# a file /mnt/media/.backup_exclude.txt will automatically be detected and used:
|
||||||
for backup_path in ${RESTIC_BACKUP_PATHS[@]}; do
|
for backup_path in "${RESTIC_BACKUP_PATHS[@]}"; do
|
||||||
if [ -f "$backup_path/.backup_exclude.txt" ]; then
|
if [ -f "$backup_path/.backup_exclude.txt" ]; then
|
||||||
exclusion_args+=" --exclude-file $backup_path/.backup_exclude.txt"
|
exclusion_args+=" --exclude-file $backup_path/.backup_exclude.txt"
|
||||||
fi
|
fi
|
||||||
@@ -68,28 +68,28 @@ wait $!
|
|||||||
# --one-file-system makes sure we only backup exactly those mounted file systems specified in $RESTIC_BACKUP_PATHS, and thus not directories like /dev, /sys etc.
|
# --one-file-system makes sure we only backup exactly those mounted file systems specified in $RESTIC_BACKUP_PATHS, and thus not directories like /dev, /sys etc.
|
||||||
# --tag lets us reference these backups later when doing restic-forget.
|
# --tag lets us reference these backups later when doing restic-forget.
|
||||||
restic backup \
|
restic backup \
|
||||||
--verbose=$RESTIC_VERBOSITY_LEVEL \
|
--verbose="$RESTIC_VERBOSITY_LEVEL" \
|
||||||
--one-file-system \
|
--one-file-system \
|
||||||
--tag $RESTIC_BACKUP_TAG \
|
--tag "$RESTIC_BACKUP_TAG" \
|
||||||
--option b2.connections=$B2_CONNECTIONS \
|
--option b2.connections="$B2_CONNECTIONS" \
|
||||||
$exclusion_args \
|
"$exclusion_args" \
|
||||||
$RESTIC_BACKUP_EXTRA_ARGS \
|
"$RESTIC_BACKUP_EXTRA_ARGS" \
|
||||||
$RESTIC_BACKUP_PATHS &
|
"$RESTIC_BACKUP_PATHS" &
|
||||||
wait $!
|
wait $!
|
||||||
|
|
||||||
# Dereference and delete/prune old backups.
|
# Dereference and delete/prune old backups.
|
||||||
# See restic-forget(1) or http://restic.readthedocs.io/en/latest/060_forget.html
|
# See restic-forget(1) or http://restic.readthedocs.io/en/latest/060_forget.html
|
||||||
# --group-by only the tag and path, and not by hostname. This is because I create a B2 Bucket per host, and if this hostname accidentially change some time, there would now be multiple backup sets.
|
# --group-by only the tag and path, and not by hostname. This is because I create a B2 Bucket per host, and if this hostname accidentially change some time, there would now be multiple backup sets.
|
||||||
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 \
|
--option b2.connections="$B2_CONNECTIONS" \
|
||||||
--prune \
|
--prune \
|
||||||
--group-by "paths,tags" \
|
--group-by "paths,tags" \
|
||||||
--keep-daily $RESTIC_RETENTION_DAYS \
|
--keep-daily "$RESTIC_RETENTION_DAYS" \
|
||||||
--keep-weekly $RESTIC_RETENTION_WEEKS \
|
--keep-weekly "$RESTIC_RETENTION_WEEKS" \
|
||||||
--keep-monthly $RESTIC_RETENTION_MONTHS \
|
--keep-monthly "$RESTIC_RETENTION_MONTHS" \
|
||||||
--keep-yearly $RESTIC_RETENTION_YEARS &
|
--keep-yearly "$RESTIC_RETENTION_YEARS" &
|
||||||
wait $!
|
wait $!
|
||||||
|
|
||||||
# Check repository for errors.
|
# Check repository for errors.
|
||||||
|
|||||||
@@ -7,11 +7,11 @@ set -euo pipefail
|
|||||||
|
|
||||||
# Assert that all needed environment variables are set.
|
# Assert that all needed environment variables are set.
|
||||||
assert_envvars() {
|
assert_envvars() {
|
||||||
local varnames=($@)
|
local varnames=("$@")
|
||||||
for varname in "${varnames[@]}"; do
|
for varname in "${varnames[@]}"; do
|
||||||
# Check if variable is set, then if it is not empty (need to do both as of `set -u`).
|
# 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
|
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
|
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
|
exit 1
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
@@ -37,6 +37,6 @@ trap exit_hook INT TERM
|
|||||||
|
|
||||||
# Check repository for errors.
|
# Check repository for errors.
|
||||||
restic check \
|
restic check \
|
||||||
--option b2.connections=$B2_CONNECTIONS \
|
--option b2.connections="$B2_CONNECTIONS" \
|
||||||
--verbose=$RESTIC_VERBOSITY_LEVEL &
|
--verbose="$RESTIC_VERBOSITY_LEVEL" &
|
||||||
wait $!
|
wait $!
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
#!/usr/bin/env sh
|
#!/usr/bin/env bash
|
||||||
# Send email notification from systemd.
|
# Send email notification from systemd.
|
||||||
# Source: https://serverfault.com/questions/876233/how-to-send-an-email-if-a-systemd-service-is-restarted
|
# Source: https://serverfault.com/questions/876233/how-to-send-an-email-if-a-systemd-service-is-restarted
|
||||||
# Source: https://wiki.archlinux.org/index.php/Systemd/Timers#MAILTO
|
# Source: https://wiki.archlinux.org/index.php/Systemd/Timers#MAILTO
|
||||||
@@ -15,19 +15,19 @@
|
|||||||
# (24 * 60 * 60) / 100 = 864 second
|
# (24 * 60 * 60) / 100 = 864 second
|
||||||
# One option that I used with my old Axis cameras it to use my gmx.com accunt for sending emails instead, as there are (no?) higher limits there.
|
# One option that I used with my old Axis cameras it to use my gmx.com accunt for sending emails instead, as there are (no?) higher limits there.
|
||||||
MIN_WAIT_TIME_S=900
|
MIN_WAIT_TIME_S=900
|
||||||
SCRIPT_NAME=$(basename $0)
|
SCRIPT_NAME=$(basename "$0")
|
||||||
LAST_RUN_FILE="/tmp/${SCRIPT_NAME}_last_run.txt"
|
LAST_RUN_FILE="/tmp/${SCRIPT_NAME}_last_run.txt"
|
||||||
|
|
||||||
last_touch() {
|
last_touch() {
|
||||||
stat -c %Y $1
|
stat -c %Y "$1"
|
||||||
}
|
}
|
||||||
|
|
||||||
waited_long_enough() {
|
waited_long_enough() {
|
||||||
retval=1
|
retval=1
|
||||||
if [ -e $LAST_RUN_FILE ]; then
|
if [ -e "$LAST_RUN_FILE" ]; then
|
||||||
now=$(date +%s)
|
now=$(date +%s)
|
||||||
last=$(last_touch $LAST_RUN_FILE)
|
last=$(last_touch "$LAST_RUN_FILE")
|
||||||
wait_s=$(expr $now - $last)
|
wait_s=$((now - last))
|
||||||
if [ "$wait_s" -gt "$MIN_WAIT_TIME_S" ]; then
|
if [ "$wait_s" -gt "$MIN_WAIT_TIME_S" ]; then
|
||||||
retval=0
|
retval=0
|
||||||
fi
|
fi
|
||||||
@@ -35,7 +35,7 @@ waited_long_enough() {
|
|||||||
retval=0
|
retval=0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
[ $retval -eq 0 ] && touch $LAST_RUN_FILE
|
[ $retval -eq 0 ] && touch "$LAST_RUN_FILE"
|
||||||
return $retval
|
return $retval
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user