From 9b7db6d9994e7e3a0fbd6114fd33d2b0fe9f441c Mon Sep 17 00:00:00 2001 From: Erik Westrup Date: Tue, 1 Feb 2022 17:15:47 +0100 Subject: [PATCH] Run shellcheck on all shellscripts --- usr/local/sbin/cron_mail | 2 +- usr/local/sbin/restic_backup.sh | 34 ++++++++++++++++----------------- usr/local/sbin/restic_check.sh | 10 +++++----- usr/local/sbin/systemd-email | 14 +++++++------- 4 files changed, 30 insertions(+), 30 deletions(-) diff --git a/usr/local/sbin/cron_mail b/usr/local/sbin/cron_mail index c3dd692..65af7e4 100755 --- a/usr/local/sbin/cron_mail +++ b/usr/local/sbin/cron_mail @@ -15,7 +15,7 @@ if [ $# -eq 0 ]; then echo "No program to run given!" >&2 exit 1 fi -cmd="$@" +cmd="$*" body=$(eval "$cmd" 2>&1) diff --git a/usr/local/sbin/restic_backup.sh b/usr/local/sbin/restic_backup.sh index be21e05..27a531e 100644 --- a/usr/local/sbin/restic_backup.sh +++ b/usr/local/sbin/restic_backup.sh @@ -15,11 +15,11 @@ 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=($@) + 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 + 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 @@ -48,7 +48,7 @@ trap exit_hook INT TERM 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, # 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 exclusion_args+=" --exclude-file $backup_path/.backup_exclude.txt" 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. # --tag lets us reference these backups later when doing restic-forget. restic backup \ - --verbose=$RESTIC_VERBOSITY_LEVEL \ + --verbose="$RESTIC_VERBOSITY_LEVEL" \ --one-file-system \ - --tag $RESTIC_BACKUP_TAG \ - --option b2.connections=$B2_CONNECTIONS \ - $exclusion_args \ - $RESTIC_BACKUP_EXTRA_ARGS \ - $RESTIC_BACKUP_PATHS & + --tag "$RESTIC_BACKUP_TAG" \ + --option b2.connections="$B2_CONNECTIONS" \ + "$exclusion_args" \ + "$RESTIC_BACKUP_EXTRA_ARGS" \ + "$RESTIC_BACKUP_PATHS" & wait $! # Dereference and delete/prune old backups. # 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. restic forget \ - --verbose=$RESTIC_VERBOSITY_LEVEL \ - --tag $RESTIC_BACKUP_TAG \ - --option b2.connections=$B2_CONNECTIONS \ + --verbose="$RESTIC_VERBOSITY_LEVEL" \ + --tag "$RESTIC_BACKUP_TAG" \ + --option b2.connections="$B2_CONNECTIONS" \ --prune \ --group-by "paths,tags" \ - --keep-daily $RESTIC_RETENTION_DAYS \ - --keep-weekly $RESTIC_RETENTION_WEEKS \ - --keep-monthly $RESTIC_RETENTION_MONTHS \ - --keep-yearly $RESTIC_RETENTION_YEARS & + --keep-daily "$RESTIC_RETENTION_DAYS" \ + --keep-weekly "$RESTIC_RETENTION_WEEKS" \ + --keep-monthly "$RESTIC_RETENTION_MONTHS" \ + --keep-yearly "$RESTIC_RETENTION_YEARS" & wait $! # Check repository for errors. diff --git a/usr/local/sbin/restic_check.sh b/usr/local/sbin/restic_check.sh index 238e76e..bf4d8b5 100644 --- a/usr/local/sbin/restic_check.sh +++ b/usr/local/sbin/restic_check.sh @@ -7,11 +7,11 @@ set -euo pipefail # Assert that all needed environment variables are set. assert_envvars() { - local varnames=($@) + 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 + 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 @@ -37,6 +37,6 @@ trap exit_hook INT TERM # Check repository for errors. restic check \ - --option b2.connections=$B2_CONNECTIONS \ - --verbose=$RESTIC_VERBOSITY_LEVEL & + --option b2.connections="$B2_CONNECTIONS" \ + --verbose="$RESTIC_VERBOSITY_LEVEL" & wait $! diff --git a/usr/local/sbin/systemd-email b/usr/local/sbin/systemd-email index a7c2ae9..159b126 100644 --- a/usr/local/sbin/systemd-email +++ b/usr/local/sbin/systemd-email @@ -1,4 +1,4 @@ -#!/usr/bin/env sh +#!/usr/bin/env bash # Send email notification from systemd. # 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 @@ -15,19 +15,19 @@ # (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. MIN_WAIT_TIME_S=900 -SCRIPT_NAME=$(basename $0) +SCRIPT_NAME=$(basename "$0") LAST_RUN_FILE="/tmp/${SCRIPT_NAME}_last_run.txt" last_touch() { - stat -c %Y $1 + stat -c %Y "$1" } waited_long_enough() { retval=1 - if [ -e $LAST_RUN_FILE ]; then + if [ -e "$LAST_RUN_FILE" ]; then now=$(date +%s) - last=$(last_touch $LAST_RUN_FILE) - wait_s=$(expr $now - $last) + last=$(last_touch "$LAST_RUN_FILE") + wait_s=$((now - last)) if [ "$wait_s" -gt "$MIN_WAIT_TIME_S" ]; then retval=0 fi @@ -35,7 +35,7 @@ waited_long_enough() { retval=0 fi - [ $retval -eq 0 ] && touch $LAST_RUN_FILE + [ $retval -eq 0 ] && touch "$LAST_RUN_FILE" return $retval }