Skip to content

Commit

Permalink
Misc fixes all over the place; shellcheck fixes mostly.
Browse files Browse the repository at this point in the history
checksum_file.sh
* explained the OpenBSD situation better
* improved print_usage()
* moved OS variants into alphabetical order
* several shellcheck fixes

rsnapshot/rsnapshot-rotate.sh
* several shellcheck fixes
* set -e
* omit WRAPPER_CONF

Signed-off-by: Christian Kujau <[email protected]>
  • Loading branch information
ckujau committed May 31, 2020
1 parent 7ba5642 commit 1d7972c
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 60 deletions.
63 changes: 35 additions & 28 deletions checksum_file.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
# systems, as older shells may not understand command substitution
# with parentheses, as required by POSIX.
# NetBSD: cksum & {g,s}etextattr, should be included in src.
# OpenBSD: No extended attribute support since July 2005 (commit 9dd8235)
# OpenBSD: No extended attribute support since July 2005:
# > https://github.com/openbsd/src/commit/9dd8235"
# > Extended Attributes was a piece to get to ACLs, however ACLs have
# > not been worked on, so EA is pointless. Also the code is not enabled
# > in GENERIC so it is not being tested or maintained.
#
# Each operating system has its own routines for setting/getting EAs and also
# for calculating checksums. The only hardcoded value is the digest algorithm
Expand All @@ -26,22 +30,25 @@
# - Or rewrite this whole thing in Python, for portability's sake? (hashlib, os/xattr)
#
DIGEST="md5" # md5, sha1, sha256, sha512
PROG=$(basename "$0")

# Adjust if needed
PATH=/bin:/usr/bin:/sbin:/usr/local/bin:/opt/local/bin:/opt/csw/bin:/usr/sfw/bin

print_usage()
{
echo "Usage: $(basename "$0") [get] [file]"
echo " $(basename "$0") [set] [file]"
echo " $(basename "$0") [get-set] [file]"
echo " $(basename "$0") [check-set] [file]"
echo " $(basename "$0") [check] [file]"
echo " $(basename "$0") [remove] [file]"
echo " $(basename "$0") [test]"
echo ""
echo " get-set - sets a new checksum if none is found, print checksum otherwise."
echo " check-set - sets a new checksum if none is found, verify checksum otherwise."
cat <<EOF
Usage: ${PROG} [get] [file]
${PROG} [set] [file]
${PROG} [get-set] [file]
${PROG} [check-set] [file]
${PROG} [check] [file]
${PROG} [remove] [file]
${PROG} [test]
get-set - sets a new checksum if none is found, print checksum otherwise.
check-set - sets a new checksum if none is found, verify checksum otherwise.
EOF
}

if [ $# -ne 2 ] || [ ! -f "$2" ] && [ ! "$1" = "test" ]; then
Expand Down Expand Up @@ -104,24 +111,24 @@ case ${OS} in
${DIGEST} -q "$1"
;;

NetBSD)
# cksum should support all common algorithms.
cksum -q -a ${DIGEST} "$1"
;;

Linux)
# GNU/coreutils should be installed on most Linux distributions.
# It's also by far much faster than its perl or openssl alternatives.
${DIGEST}sum "$1" | awk '{print $1}'
;;

NetBSD)
# cksum should support all common algorithms.
cksum -q -a ${DIGEST} "$1"
;;

SunOS)
# SUNWcsu should be available. If it's not, we'd have much bigger problems.
digest -a ${DIGEST} "$1"
;;

*)
do_log "We don't support ${OS}, yet :-(" 1
do_log "We don't support ${OS} :-(" 1
;;
esac
}
Expand All @@ -137,10 +144,6 @@ case ${OS} in
pxattr -n user.checksum.${DIGEST} "$1" 2>/dev/null | awk '/user.checksum/ {print $NF}'
;;

NetBSD)
getextattr -q user checksum.${DIGEST} "$1"
;;

Linux)
# NOTE: If the designated EA is not set, getfattr may not return a non-zero
# exit code. This has been fixed upstream but may not have been picked up
Expand All @@ -155,12 +158,16 @@ case ${OS} in
getfattr --only-values --name user.checksum.${DIGEST} -- "$1" 2>/dev/null | awk '/[a-z0-9]/ {print $1}'
;;

NetBSD)
getextattr -q user checksum.${DIGEST} "$1"
;;

SunOS)
runat "$1" cat user.checksum.${DIGEST} 2>/dev/null
;;

*)
do_log "We don't support ${OS}, yet :-(" 1
do_log "We don't support ${OS} :-(" 1
;;
esac
}
Expand All @@ -179,20 +186,20 @@ case ${OS} in
pxattr -n user.checksum.${DIGEST} -v "${CHECKSUM_C}" "$1"
;;

NetBSD)
setextattr user checksum.${DIGEST} "${CHECKSUM_C}" "$1"
;;

Linux)
setfattr --name user.checksum.${DIGEST} --value "${CHECKSUM_C}" -- "$1"
;;

NetBSD)
setextattr user checksum.${DIGEST} "${CHECKSUM_C}" "$1"
;;

SunOS)
runat "$1" "echo ${CHECKSUM_C} > user.checksum.${DIGEST}"
;;

*)
do_log "We don't support ${OS}, yet :-(" 1
do_log "We don't support ${OS} :-(" 1
;;
esac
}
Expand Down Expand Up @@ -290,7 +297,7 @@ case ${ACTION} in
;;

*)
do_log "We don't support ${OS}, yet :-(" 1
do_log "We don't support ${OS} :-(" 1
;;
esac
;;
Expand Down
35 changes: 18 additions & 17 deletions rsnapshot/rsnapshot-rotate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,33 @@
CONF="/etc/rsnapshot"

if [ ! $# = 1 ]; then
HOSTS=$(ls "$CONF"/rsnapshot-*.conf | sed 's/.*\/rsnapshot-//;s/\.conf//' | xargs echo | sed 's/ /|/g')
echo "Usage: $(basename "$0") [$HOSTS]"
HOSTS=$(find ${CONF} -maxdepth 1 -name "*.conf" | sed 's/.*\/rsnapshot-//;s/\.conf//' | xargs echo | sed 's/ /|/g')
echo "Usage: $(basename "$0") [${HOSTS}]"
exit 1
else
CONF="$CONF"/rsnapshot-"$1".conf
DIR=$(awk '/^snapshot_root/ {print $2}' "$CONF")
CONF="${CONF}/rsnapshot-${1}.conf"
DIR="$(awk '/^snapshot_root/ {print $2}' "${CONF}")"

# Don't let rsnapshot-wrapper remount our backup store
WRAPPER_CONF="/usr/local/etc/rsnapshot-wrapper.conf"
NOMOUNT=$(awk -F= '/^NOMOUNT/ {print $2}' $WRAPPER_CONF)
touch "$NOMOUNT"

[ -f "$CONF" ] || exit 2
[ -d "$DIR" ] || exit 3
# Safety belt
[ -f "${CONF}" ] || exit 2
[ -d "${DIR}" ] || exit 3
fi

for i in daily weekly monthly; do
C=$(ls -d "$DIR"/"$i".* 2>/dev/null | wc -l)
set -e
# Don't let rsnapshot-wrapper remount our backup store while we are running.
NOMOUNT=$(awk -F= '/^NOMOUNT/ {print $2}' /usr/local/etc/rsnapshot-wrapper.conf)
touch "${NOMOUNT}"

for interval in daily weekly monthly; do
COUNT=$(find "${DIR}"/${interval}.[0-9] -maxdepth 0 2>/dev/null | wc -l)
j=0
while [ $j -le "$C" ]; do
echo "("$j"/"$C") rsnapshot -c "$CONF" "$i"..."
rsnapshot -c "$CONF" "$i"
while [ "${j}" -le "${COUNT}" ]; do
echo "(${j}/${COUNT}) rsnapshot -c ${CONF} ${interval}..."
rsnapshot -c "${CONF}" "${interval}"
j=$((j+1))
done
done

# Since this is not a normal operation and may take a long time, we will
# remove the NOMOUNT flag again, otherwise we may forget to remove it.
rm -f "$NOMOUNT"
rm -f "${NOMOUNT}"
22 changes: 12 additions & 10 deletions ternet/flop.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
# https://web.archive.org/web/20170702162404/http://www.breakingsystemsforfunandprofit.com/the-lolcats-they-are-everywhere/
#
DIR=/var/www/ternet
while read URL; do

while read -r URL; do
SURL=$(echo "${URL}" | cut -d" " -f1)
echo "${SURL}" | egrep -qi "\.(jp(e)?g|gif|png|tiff|bmp|ico)$" &&
(
umask 002
PIC=$$-${RANDOM}
wget -q -O ${DIR}/${PIC}.tmp "${SURL}"
convert -quiet ${DIR}/${PIC}.tmp -flop ${DIR}/${PIC}.png
rm -f ${DIR}/${PIC}.tmp
echo http://127.0.0.1/ternet/${PIC}.png
) || echo "$URL"
if echo "${SURL}" | grep -Eqi "\.(jp(e)?g|gif|png|tiff|bmp|ico)$"; then
umask 002
PIC=$$-${RANDOM}
wget -q -O ${DIR}/${PIC}.tmp "${SURL}"
convert -quiet ${DIR}/${PIC}.tmp -flop ${DIR}/${PIC}.png
rm -f ${DIR}/${PIC}.tmp
echo http://127.0.0.1/ternet/${PIC}.png
else
echo "${URL}"
fi
done
7 changes: 2 additions & 5 deletions vbox-sysrq.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
# Keyboard scancodes: Ordinary scancodes
# https://www.win.tue.nl/~aeb/linux/kbd/scancodes-1.html#ss1.4
#
# Re: Magic sysrq on Linux guest, under a Linux host
# https://forums.virtualbox.org/viewtopic.php?f=6&t=10400#p207674
#
# Use Magic Sysrq-key in guest
# https://www.halfdog.net/Misc/TipsAndTricks/VirtualBox.html#MagicSysrq
#
Expand Down Expand Up @@ -64,8 +61,8 @@ if [ -n "$PRESS" ]; then
VBoxManage controlvm "$VM" keyboardputscancode 1d 38 54 "$PRESS" "$RELEASE" d4 b8 9d
else
echo
echo "Unknown sysrq key! ("${SYSRQ}")"
egrep '^.\|' "$0"
echo "Unknown sysrq key! (${SYSRQ})"
grep -E '^.\|' "$0"
echo
exit 1
fi

0 comments on commit 1d7972c

Please sign in to comment.