Merge branch 'bugfix/shellcheck' into 'master'

Fix shellcheck reported issues

See merge request ungleich-public/ccollect!13
This commit is contained in:
poljakowski 2019-11-14 19:17:50 +01:00
commit c9eef21e43
2 changed files with 78 additions and 40 deletions

View file

@ -199,6 +199,9 @@ dist: distclean documentation
/tmp/ccollect: /tmp/ccollect:
mkdir -p /tmp/ccollect mkdir -p /tmp/ccollect
shellcheck: ./ccollect
shellcheck -s sh -f gcc -x ./ccollect
test: $(CCOLLECT_SOURCE) /tmp/ccollect test: $(CCOLLECT_SOURCE) /tmp/ccollect
cd ./conf/sources/; for s in *; do CCOLLECT_CONF=../ ../../ccollect daily "$$s"; done cd ./conf/sources/; for s in *; do CCOLLECT_CONF=../ ../../ccollect daily "$$s"; done
touch /tmp/ccollect/$$(ls /tmp/ccollect | head -n1).ccollect-marker touch /tmp/ccollect/$$(ls /tmp/ccollect | head -n1).ccollect-marker

115
ccollect
View file

@ -27,9 +27,9 @@ set -u
# #
# Standard variables (stolen from cconf) # Standard variables (stolen from cconf)
# #
__pwd="$(pwd -P)" __mydir="${0%/*}"
__mydir="${0%/*}"; __abs_mydir="$(cd "$__mydir" && pwd -P)" __abs_mydir="$(cd "$__mydir" && pwd -P)"
__myname=${0##*/}; __abs_myname="$__abs_mydir/$__myname" __myname=${0##*/}
# #
# where to find our configuration and temporary file # where to find our configuration and temporary file
@ -41,7 +41,8 @@ CPREEXEC="${CDEFAULTS}/pre_exec"
CPOSTEXEC="${CDEFAULTS}/post_exec" CPOSTEXEC="${CDEFAULTS}/post_exec"
CMARKER=".ccollect-marker" CMARKER=".ccollect-marker"
export TMP="$(mktemp "/tmp/${__myname}.XXXXXX")" TMP="$(mktemp "/tmp/${__myname}.XXXXXX")"
export TMP
CONTROL_PIPE="/tmp/${__myname}-control-pipe" CONTROL_PIPE="/tmp/${__myname}-control-pipe"
VERSION="2.6" VERSION="2.6"
@ -76,6 +77,7 @@ LOCKFD=4
lock_flock() lock_flock()
{ {
# $1 = source to backup # $1 = source to backup
# shellcheck disable=SC2059
lockfile="${LOCKDIR}/$(printf "${LOCKFILE_PATTERN}" "$1")" lockfile="${LOCKDIR}/$(printf "${LOCKFILE_PATTERN}" "$1")"
eval "exec ${LOCKFD}> ${lockfile}" eval "exec ${LOCKFD}> ${lockfile}"
@ -85,6 +87,7 @@ lock_flock()
unlock_flock() unlock_flock()
{ {
# $1 = source to backup # $1 = source to backup
# shellcheck disable=SC2059
lockfile="${LOCKDIR}/$(printf "${LOCKFILE_PATTERN}" "$1")" lockfile="${LOCKDIR}/$(printf "${LOCKFILE_PATTERN}" "$1")"
eval "exec ${LOCKFD}>&-" eval "exec ${LOCKFD}>&-"
rm -f "${lockfile}" rm -f "${lockfile}"
@ -96,6 +99,7 @@ unlock_flock()
lock_mkdir() lock_mkdir()
{ {
# $1 = source to backup # $1 = source to backup
# shellcheck disable=SC2059
lockfile="${LOCKDIR}/$(printf "${LOCKFILE_PATTERN}" "$1")" lockfile="${LOCKDIR}/$(printf "${LOCKFILE_PATTERN}" "$1")"
mkdir "${lockfile}" && return 0 || return 1 mkdir "${lockfile}" && return 0 || return 1
@ -104,6 +108,7 @@ lock_mkdir()
unlock_mkdir() unlock_mkdir()
{ {
# $1 = source to backup # $1 = source to backup
# shellcheck disable=SC2059
lockfile="${LOCKDIR}/$(printf "${LOCKFILE_PATTERN}" "$1")" lockfile="${LOCKDIR}/$(printf "${LOCKFILE_PATTERN}" "$1")"
rmdir "${lockfile}" rmdir "${lockfile}"
@ -112,7 +117,7 @@ unlock_mkdir()
# #
# determine locking tool: flock or mkdir # determine locking tool: flock or mkdir
# #
if $(which flock > /dev/null 2>&1) if command -v flock > /dev/null 2>&1
then then
lockf="lock_flock" lockf="lock_flock"
unlockf="unlock_flock" unlockf="unlock_flock"
@ -137,6 +142,7 @@ LOGONLYERRORS=""
# catch signals # catch signals
# #
TRAPFUNC="rm -f \"${TMP}\"" TRAPFUNC="rm -f \"${TMP}\""
# shellcheck disable=SC2064
trap "${TRAPFUNC}" 1 2 15 trap "${TRAPFUNC}" 1 2 15
# #
@ -147,7 +153,7 @@ trap "${TRAPFUNC}" 1 2 15
# see: http://www.tldp.org/LDP/abs/html/intandnonint.html # see: http://www.tldp.org/LDP/abs/html/intandnonint.html
_is_interactive() _is_interactive()
{ {
[ -t 0 -o -p /dev/stdin ] [ -t 0 ] || [ -p /dev/stdin ]
} }
# #
@ -163,26 +169,26 @@ delete_from_file()
# dirs for deletion will be moved to this trash dir inside destination dir # dirs for deletion will be moved to this trash dir inside destination dir
# - for fast mv operation # - for fast mv operation
trash="$(mktemp -d ".trash.XXXXXX")" trash="$(mktemp -d ".trash.XXXXXX")"
while read to_remove; do while read -r to_remove; do
mv "${to_remove}" "${trash}" || mv "${to_remove}" "${trash}" ||
_exit_err "Moving ${to_remove} to ${trash} failed." _exit_err "Moving ${to_remove} to ${trash} failed."
set -- "$@" "${to_remove}" set -- "$@" "${to_remove}"
if [ "${suffix}" ]; then if [ "${suffix}" ]; then
to_remove_no_suffix="$(echo ${to_remove} | sed "s/$suffix\$//")" to_remove_no_suffix="$(echo "${to_remove}" | sed "s/$suffix\$//")"
mv "${to_remove_no_suffix}" "${trash}" || mv "${to_remove_no_suffix}" "${trash}" ||
_exit_err "Moving ${to_remove_no_suffix} to ${trash} failed." _exit_err "Moving ${to_remove_no_suffix} to ${trash} failed."
set -- "$@" "${to_remove_no_suffix}" set -- "$@" "${to_remove_no_suffix}"
fi fi
done < "${file}" done < "${file}"
_techo "Removing $@ in ${trash}..." _techo "Removing $* in ${trash}..."
empty_dir=".empty-dir" empty_dir=".empty-dir"
mkdir "${empty_dir}" || _exit_err "Empty directory ${empty_dir} cannot be created." mkdir "${empty_dir}" || _exit_err "Empty directory ${empty_dir} cannot be created."
[ "${VVERBOSE}" ] && echo "Starting: rsync -a --delete ${empty_dir} ${trash}" [ "${VVERBOSE}" ] && echo "Starting: rsync -a --delete ${empty_dir} ${trash}"
# rsync needs ending slash for directory content # rsync needs ending slash for directory content
rsync -a --delete "${empty_dir}/" "${trash}/" || _exit_err "Removing $@ failed." rsync -a --delete "${empty_dir}/" "${trash}/" || _exit_err "Removing $* failed."
rmdir "${trash}" || _exit_err "Removing ${trash} directory failed" rmdir "${trash}" || _exit_err "Removing ${trash} directory failed"
rmdir "${empty_dir}" || _exit_err "Removing ${empty_dir} directory failed" rmdir "${empty_dir}" || _exit_err "Removing ${empty_dir} directory failed"
_techo "Removing $@ in ${trash} finished." _techo "Removing $* in ${trash} finished."
} }
display_version() display_version()
@ -232,7 +238,7 @@ unlock()
# stdout version # stdout version
_techo_stdout() _techo_stdout()
{ {
echo "$(${DDATE}): $@" echo "$(${DDATE}): $*"
} }
# syslog version # syslog version
@ -260,14 +266,16 @@ _techo()
{ {
if [ "${LOGLEVEL}" = "a" ] if [ "${LOGLEVEL}" = "a" ]
then then
set -- ${name:+"[${name}]"} $@ # name is exported before calling this function
# shellcheck disable=SC2154
set -- ${name:+"[${name}]"} "$@"
"${_techof}" "$@" "${_techof}" "$@"
fi fi
} }
_techo_err() _techo_err()
{ {
_techo "Error: $@" _techo "Error: $*"
} }
_exit_err() _exit_err()
@ -368,7 +376,7 @@ else
LOGLEVEL="a" LOGLEVEL="a"
fi fi
if [ "${LOGFILE}" -o "${SYSLOG}" ] if [ "${LOGFILE}" ] || [ "${SYSLOG}" ]
then then
if [ "${LOGONLYERRORS}" ] if [ "${LOGONLYERRORS}" ]
then then
@ -378,8 +386,7 @@ fi
# check that MAX_JOBS is natural number > 0 # check that MAX_JOBS is natural number > 0
# empty string means run all in parallel # empty string means run all in parallel
echo "${MAX_JOBS}" | grep -q -E '^[1-9][0-9]*$|^$' if ! echo "${MAX_JOBS}" | grep -q -E '^[1-9][0-9]*$|^$'
if [ "$?" -ne 0 ]
then then
_exit_err "Invalid max jobs value \"${MAX_JOBS}\"" _exit_err "Invalid max jobs value \"${MAX_JOBS}\""
fi fi
@ -412,19 +419,22 @@ if [ "${USE_ALL}" = 1 ]; then
( cd "${CSOURCES}" && ls -1 > "${TMP}" ) || \ ( cd "${CSOURCES}" && ls -1 > "${TMP}" ) || \
_exit_err "Listing of sources failed. Aborting." _exit_err "Listing of sources failed. Aborting."
while read tmp; do while read -r tmp; do
eval export source_${no_sources}=\"${tmp}\" eval export "source_${no_sources}=\"${tmp}\""
no_sources=$((${no_sources}+1)) no_sources=$((no_sources + 1))
done < "${TMP}" done < "${TMP}"
else else
# #
# Get sources from command line # Get sources from command line
# #
while [ "$#" -ge 1 ]; do while [ "$#" -ge 1 ]; do
eval arg=\"\$1\"; shift eval "arg=\"\$1\""
shift
eval export source_${no_sources}=\"${arg}\" # arg is assigned in the eval above
no_sources="$((${no_sources}+1))" # shellcheck disable=SC2154
eval export "source_${no_sources}=\"${arg}\""
no_sources="$((no_sources + 1))"
done done
fi fi
@ -461,6 +471,7 @@ if [ "${PARALLEL}" ]; then
# fd 5 is tied to control pipe # fd 5 is tied to control pipe
eval "exec 5<>${CONTROL_PIPE}" eval "exec 5<>${CONTROL_PIPE}"
TRAPFUNC="${TRAPFUNC}; rm -f \"${CONTROL_PIPE}\"" TRAPFUNC="${TRAPFUNC}; rm -f \"${CONTROL_PIPE}\""
# shellcheck disable=SC2064
trap "${TRAPFUNC}" 0 1 2 15 trap "${TRAPFUNC}" 0 1 2 15
# determine how much parallel jobs to prestart # determine how much parallel jobs to prestart
@ -483,7 +494,7 @@ while [ "${source_no}" -lt "${no_sources}" ]; do
# Get current source # Get current source
# #
eval export name=\"\$source_${source_no}\" eval export name=\"\$source_${source_no}\"
source_no=$((${source_no}+1)) source_no=$((source_no + 1))
# #
# Start ourself, if we want parallel execution # Start ourself, if we want parallel execution
@ -498,12 +509,12 @@ while [ "${source_no}" -lt "${no_sources}" ]; do
then then
# run prestart child if pending # run prestart child if pending
{ "$0" "${INTERVAL}" "${name}"; printf '\n' >&5; } & { "$0" "${INTERVAL}" "${name}"; printf '\n' >&5; } &
prestart=$((${prestart} - 1)) prestart=$((prestart - 1))
continue continue
else else
# each time a child finishes we get a line from the pipe # each time a child finishes we get a line from the pipe
# and then launch another child # and then launch another child
while read line while read -r line
do do
{ "$0" "${INTERVAL}" "${name}"; printf '\n' >&5; } & { "$0" "${INTERVAL}" "${name}"; printf '\n' >&5; } &
# get out of loop so we can contnue with main loop # get out of loop so we can contnue with main loop
@ -557,6 +568,7 @@ while [ "${source_no}" -lt "${no_sources}" ]; do
# redefine trap to also unlock (rm lockfile) # redefine trap to also unlock (rm lockfile)
TRAPFUNC="${TRAPFUNC}; unlock \"${name}\"" TRAPFUNC="${TRAPFUNC}; unlock \"${name}\""
# shellcheck disable=SC2064
trap "${TRAPFUNC}" 1 2 15 trap "${TRAPFUNC}" 1 2 15
# #
@ -576,10 +588,10 @@ while [ "${source_no}" -lt "${no_sources}" ]; do
for opt in verbose very_verbose summary exclude rsync_options \ for opt in verbose very_verbose summary exclude rsync_options \
delete_incomplete rsync_failure_codes \ delete_incomplete rsync_failure_codes \
mtime quiet_if_down ; do mtime quiet_if_down ; do
if [ -f "${backup}/${opt}" -o -f "${backup}/no_${opt}" ]; then if [ -f "${backup}/${opt}" ] || [ -f "${backup}/no_${opt}" ]; then
eval c_$opt=\"${backup}/$opt\" eval "c_$opt=\"${backup}/$opt\""
else else
eval c_$opt=\"${CDEFAULTS}/$opt\" eval "c_$opt=\"${CDEFAULTS}/$opt\""
fi fi
done done
@ -599,6 +611,8 @@ while [ "${source_no}" -lt "${no_sources}" ]; do
# #
# Sort by ctime (default) or mtime (configuration option) # Sort by ctime (default) or mtime (configuration option)
# #
# variable is assigned using eval
# shellcheck disable=SC2154
if [ -f "${c_mtime}" ] ; then if [ -f "${c_mtime}" ] ; then
TSORT="t" TSORT="t"
else else
@ -642,6 +656,8 @@ while [ "${source_no}" -lt "${no_sources}" ]; do
# #
# Exclude list # Exclude list
# #
# variable is assigned using eval
# shellcheck disable=SC2154
if [ -f "${c_exclude}" ]; then if [ -f "${c_exclude}" ]; then
set -- "$@" "--exclude-from=${c_exclude}" set -- "$@" "--exclude-from=${c_exclude}"
fi fi
@ -649,6 +665,8 @@ while [ "${source_no}" -lt "${no_sources}" ]; do
# #
# Output a summary # Output a summary
# #
# variable is assigned using eval
# shellcheck disable=SC2154
if [ -f "${c_summary}" ]; then if [ -f "${c_summary}" ]; then
set -- "$@" "--stats" set -- "$@" "--stats"
fi fi
@ -657,6 +675,8 @@ while [ "${source_no}" -lt "${no_sources}" ]; do
# Verbosity for rsync, rm, and mkdir # Verbosity for rsync, rm, and mkdir
# #
VVERBOSE="" VVERBOSE=""
# variable is assigned using eval
# shellcheck disable=SC2154
if [ -f "${c_very_verbose}" ]; then if [ -f "${c_very_verbose}" ]; then
set -- "$@" "-vv" set -- "$@" "-vv"
VVERBOSE="-v" VVERBOSE="-v"
@ -667,8 +687,10 @@ while [ "${source_no}" -lt "${no_sources}" ]; do
# #
# Extra options for rsync provided by the user # Extra options for rsync provided by the user
# #
# variable is assigned using eval
# shellcheck disable=SC2154
if [ -f "${c_rsync_options}" ]; then if [ -f "${c_rsync_options}" ]; then
while read line; do while read -r line; do
# Trim line. # Trim line.
ln=$(echo "${line}" | awk '{$1=$1;print;}') ln=$(echo "${line}" | awk '{$1=$1;print;}')
# Only if ln is non zero length string. # Only if ln is non zero length string.
@ -687,6 +709,8 @@ while [ "${source_no}" -lt "${no_sources}" ]; do
# Check: source is up and accepting connections (before deleting old backups!) # Check: source is up and accepting connections (before deleting old backups!)
# #
if ! rsync "$@" "${source}" >/dev/null 2>"${TMP}" ; then if ! rsync "$@" "${source}" >/dev/null 2>"${TMP}" ; then
# variable is assigned using eval
# shellcheck disable=SC2154
if [ ! -f "${c_quiet_if_down}" ]; then if [ ! -f "${c_quiet_if_down}" ]; then
cat "${TMP}" cat "${TMP}"
fi fi
@ -706,10 +730,13 @@ while [ "${source_no}" -lt "${no_sources}" ]; do
# #
# Check incomplete backups (needs echo to remove newlines) # Check incomplete backups (needs echo to remove newlines)
# #
# shellcheck disable=SC2010
ls -1 | grep "${CMARKER}\$" > "${TMP}"; ret=$? ls -1 | grep "${CMARKER}\$" > "${TMP}"; ret=$?
if [ "$ret" -eq 0 ]; then if [ "$ret" -eq 0 ]; then
_techo "Incomplete backups: $(echo $(cat "${TMP}"))" _techo "Incomplete backups: $(cat "${TMP}")"
# variable is assigned using eval
# shellcheck disable=SC2154
if [ -f "${c_delete_incomplete}" ]; then if [ -f "${c_delete_incomplete}" ]; then
delete_from_file "${TMP}" "${CMARKER}" & delete_from_file "${TMP}" "${CMARKER}" &
fi fi
@ -718,12 +745,15 @@ while [ "${source_no}" -lt "${no_sources}" ]; do
# #
# Include current time in name, not the time when we began to remove above # Include current time in name, not the time when we began to remove above
# #
export destination_name="${INTERVAL}.$(${CDATE}).$$-${source_no}" destination_name="${INTERVAL}.$(${CDATE}).$$-${source_no}"
export destination_dir="${ddir}/${destination_name}" export destination_name
destination_dir="${ddir}/${destination_name}"
export destination_dir
# #
# Check: maximum number of backups is reached? # Check: maximum number of backups is reached?
# #
# shellcheck disable=SC2010
count="$(ls -1 | grep -c "^${INTERVAL}\\.")" count="$(ls -1 | grep -c "^${INTERVAL}\\.")"
_techo "Existing backups: ${count} Total keeping backups: ${c_interval}" _techo "Existing backups: ${count} Total keeping backups: ${c_interval}"
@ -731,6 +761,7 @@ while [ "${source_no}" -lt "${no_sources}" ]; do
if [ "${count}" -ge "${c_interval}" ]; then if [ "${count}" -ge "${c_interval}" ]; then
# Use oldest directory as new backup destination directory. # Use oldest directory as new backup destination directory.
# It need not to be deleted, rsync will sync its content. # It need not to be deleted, rsync will sync its content.
# shellcheck disable=SC2010
oldest_bak=$(ls -${TSORT}1r | grep "^${INTERVAL}\\." | head -n 1 || \ oldest_bak=$(ls -${TSORT}1r | grep "^${INTERVAL}\\." | head -n 1 || \
_exit_err "Listing oldest backup failed") _exit_err "Listing oldest backup failed")
_techo "Using ${oldest_bak} for destination dir ${destination_dir}" _techo "Using ${oldest_bak} for destination dir ${destination_dir}"
@ -739,14 +770,15 @@ while [ "${source_no}" -lt "${no_sources}" ]; do
touch "${destination_dir}" touch "${destination_dir}"
# We have something to remove only if count > interval. # We have something to remove only if count > interval.
remove="$((${count} - ${c_interval}))" remove="$((count - c_interval))"
else else
_techo_err "Renaming oldest backup ${oldest_bak} to ${destination_dir} failed, removing it." _techo_err "Renaming oldest backup ${oldest_bak} to ${destination_dir} failed, removing it."
remove="$((${count} - ${c_interval} + 1))" remove="$((count - c_interval + 1))"
fi fi
if [ "${remove}" -gt 0 ]; then if [ "${remove}" -gt 0 ]; then
_techo "Removing ${remove} backup(s)..." _techo "Removing ${remove} backup(s)..."
# shellcheck disable=SC2010
ls -${TSORT}1r | grep "^${INTERVAL}\\." | head -n "${remove}" > "${TMP}" || \ ls -${TSORT}1r | grep "^${INTERVAL}\\." | head -n "${remove}" > "${TMP}" || \
_exit_err "Listing old backups failed" _exit_err "Listing old backups failed"
@ -760,6 +792,7 @@ while [ "${source_no}" -lt "${no_sources}" ]; do
# oldest existing destination directory. # oldest existing destination directory.
# #
dest_dir_name=$(basename "${destination_dir}") dest_dir_name=$(basename "${destination_dir}")
# shellcheck disable=SC2010
last_dir="$(ls -${TSORT}p1 | grep '/$' | grep -v "${dest_dir_name}" | head -n 1)" || \ last_dir="$(ls -${TSORT}p1 | grep '/$' | grep -v "${dest_dir_name}" | head -n 1)" || \
_exit_err "Failed to list contents of ${ddir}." _exit_err "Failed to list contents of ${ddir}."
@ -793,8 +826,10 @@ while [ "${source_no}" -lt "${no_sources}" ]; do
# Check if rsync exit code indicates failure. # Check if rsync exit code indicates failure.
# #
fail="" fail=""
# variable is assigned using eval
# shellcheck disable=SC2154
if [ -f "$c_rsync_failure_codes" ]; then if [ -f "$c_rsync_failure_codes" ]; then
while read code ; do while read -r code ; do
if [ "$ret" = "$code" ]; then if [ "$ret" = "$code" ]; then
fail=1 fail=1
fi fi
@ -831,10 +866,10 @@ while [ "${source_no}" -lt "${no_sources}" ]; do
# Time calculation # Time calculation
# #
end_s="$(${SDATE})" end_s="$(${SDATE})"
full_seconds="$((${end_s} - ${begin_s}))" full_seconds="$((end_s - begin_s))"
hours="$((${full_seconds} / 3600))" hours="$((full_seconds / 3600))"
minutes="$(((${full_seconds} % 3600) / 60))" minutes="$(((full_seconds % 3600) / 60))"
seconds="$((${full_seconds} % 60))" seconds="$((full_seconds % 60))"
_techo "Backup lasted: ${hours}:${minutes}:${seconds} (h:m:s)" _techo "Backup lasted: ${hours}:${minutes}:${seconds} (h:m:s)"