From 0b78d365ca5474dacd34a567deecefaa29892ee5 Mon Sep 17 00:00:00 2001 From: Alice Gaudon Date: Wed, 5 Feb 2020 02:46:19 +0100 Subject: [PATCH 1/8] Add zfs_scrub.sh --- zfs_scrub.sh | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100755 zfs_scrub.sh diff --git a/zfs_scrub.sh b/zfs_scrub.sh new file mode 100755 index 0000000..480d436 --- /dev/null +++ b/zfs_scrub.sh @@ -0,0 +1,17 @@ +#!/bin/sh +# ZFS Scrub script + +POOLS=($(cat ~/.zfs_scrub_pools)) +POOLS_STR="" + +for pool in "${POOLS[@]}"; do + echo "Scrub $pool" + zpool scrub $pool + POOLS_STR="$POOLS_STR$pool " +done + +OUTPUT=$(zpool status) + +echo "Sending mail..." +echo -e "Started scrub procedure for $POOLS_STR\n\n$OUTPUT" | $(dirname $0)/mail.sh "ZFS Scrub - $(hostname)" +echo "Done" From ad7be843cc87beec1cd1a3ca486963b7f16b7f19 Mon Sep 17 00:00:00 2001 From: Alice Gaudon Date: Wed, 5 Feb 2020 05:48:37 +0100 Subject: [PATCH 2/8] Add zfs_snapshot_cron.sh --- zfs_snapshot_cron.sh | 79 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100755 zfs_snapshot_cron.sh diff --git a/zfs_snapshot_cron.sh b/zfs_snapshot_cron.sh new file mode 100755 index 0000000..2b454e6 --- /dev/null +++ b/zfs_snapshot_cron.sh @@ -0,0 +1,79 @@ +#!/bin/bash + +PREFIX="zsc-" +CONFIG_FILE="$HOME/.zfs_snapshot_cron" + +FORMAT='+%Y-%m-%d' +DATE=$(date $FORMAT) + +split_date() { + echo "$1" | sed -E 's/([0-9]{4})-([0-9]{2})-([0-9]{2})/\1\n\2\n\3/' +} + +handle_pool() { + POOL=$1 + D=$2 + W=$3 + M=$4 + Y=$5 + echo "--- $POOL --- $D $W $M $Y" + + SNAPSHOTS=($(zfs list -t snapshot -o name | grep "$POOL@$PREFIX" | sort -r)) + echo "Snapshots: ${#SNAPSHOTS[@]}" + + EXISTS=0 + + for s in "${SNAPSHOTS[@]}"; do + sdate=$(echo "$s" | awk 'match($0, /.+@'"$PREFIX"'(.+)/, a) {print a[1]}') + + if [ "$sdate" = $DATE ]; then + EXISTS=1 + fi + + scomp=($(split_date "$sdate") $(date -d "$sdate" "+%u %W")) + + echo -n "${scomp[3]} ${scomp[4]} | " + + echo -n "${sdate}: " + + if [ $D -gt 0 ]; then + D=$(( D-1 )) + echo "KEEP; D: $D" + elif [ $W -gt 0 ] && [ ${scomp[3]} -eq 1 ]; then + W=$(( W-1 )) + echo "KEEP; W: $W" + elif [ $M -gt 0 ] && [ ${scomp[3]} -eq 1 ] && [ $(( scomp[4] % 4 )) -eq 0 ]; then + M=$(( M-1 )) + echo "KEEP; M: $M" + elif [ $Y -gt 0 ] && [ ${scomp[3]} -eq 1 ] && [ ${scomp[4]} -eq 4 ]; then + Y=$(( Y-1 )) + echo "KEEP; Y: $Y" + else + zfs destroy "$s" + echo "DISCARD" + fi + done + + if [ $EXISTS -eq 0 ]; then + echo -e "Creating snapshot $POOL@$PREFIX$DATE" + zfs snapshot $POOL@$PREFIX$DATE + fi +} + +cat "$CONFIG_FILE" | while IFS=: read pool recursive d w m y; do + echo "| ##### Pool: $pool Recursive: $recursive d: $d w: $w m: $m y: $y #####" + + if [ $recursive = "true" ]; then + SUBPOOLS=($(zfs list -o name | grep "$pool/")) + echo -e "Subpools: ${SUBPOOLS[@]}" + echo -e '|' + for p in "${SUBPOOLS[@]}"; do + handle_pool $p $d $w $m $y | awk '{print "| " $0}' + echo -e '|' + done + fi + + handle_pool $pool $d $w $m $y | awk '{print "| " $0}' + + echo -e "----------------------------------------------------------------------" +done From 0584914cc2eb83a7e9dcfa66e69d41113aa153ea Mon Sep 17 00:00:00 2001 From: Alice Gaudon Date: Sat, 15 Feb 2020 10:13:24 +0100 Subject: [PATCH 3/8] motd.sh: add ZFS features --- motd.sh | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/motd.sh b/motd.sh index d12c731..3ffc89c 100755 --- a/motd.sh +++ b/motd.sh @@ -66,10 +66,18 @@ dim="\e[2m" undim="\e[0m" # disk usage: ignore zfs, squashfs & tmpfs -mapfile -t dfs < <(df -H -x squashfs -x tmpfs -x devtmpfs --output=target,pcent,size | tail -n+2) +DISKS=$(df -H -x squashfs -x tmpfs -x devtmpfs -x zfs --output=target,pcent,size | tail -n+2) + +if test $(command -v zpool); then + DISKS+="\n" + DISKS+=$(zpool list -o name,capacity,size | tail -n+2) +fi printf "\nDisk usage:\n" -for line in "${dfs[@]}"; do +while read line; do + if [ -z "$line" ]; then + continuetest + fi # get disk usage usage=$(echo "$line" | awk '{print $2}' | sed 's/%//') used_width=$((($usage*$bar_width)/100)) @@ -93,7 +101,7 @@ for line in "${dfs[@]}"; do # print usage line & bar echo "${line}" | awk '{ printf("%-31s%+3s used out of %+4s\n", $1, $2, $3); }' | sed -e 's/^/ /' echo -e "${bar}" | sed -e 's/^/ /' -done +done <<< $(echo -e "$DISKS") ### Welcome message ### @@ -101,3 +109,16 @@ LAST_LOGIN=$(last -w | grep arisu | head -2 | tail -1) echo -e echo -e "Welcome to $(hostname), $(whoami)! Last login: $(echo "$LAST_LOGIN" | awk '{for(i=4;i<=NF;i++) print $i}' | paste -sd ' ') from $(echo "$LAST_LOGIN" | awk '{print $3}')" + +### ZFS available space warning ### +if test $(command -v zfs); then + POOLS=$(zfs list -o name,available -p | tail -n+2) + while read line; do + name=$(echo "$line" | awk '{print $1}') + avail=$(echo "$line" | awk '{print $2}') + + if [ $avail -le 150000000000 ]; then + echo -e "${red}Warning! ${white}$name ${undim}has less than 150GB" + fi + done <<< $(echo -e "$POOLS") +fi \ No newline at end of file From 335c6ef0c74db7bf4de7ac1c234a1dba5045b36e Mon Sep 17 00:00:00 2001 From: Alice Gaudon Date: Fri, 20 Mar 2020 13:04:22 +0100 Subject: [PATCH 4/8] Improve mail wrap script with dynamic args and ignoring a specific exit code --- backup2me.sh | 2 +- mail_wrap_script.sh | 39 +++++++++++++++++++++++++++++++++++---- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/backup2me.sh b/backup2me.sh index ae23c2a..be6c093 100755 --- a/backup2me.sh +++ b/backup2me.sh @@ -12,7 +12,7 @@ function join { if ! lockfile-create -p -r 2 $0; then echo "Another backup is running, aborting" - exit 1 + exit 49 fi ### Program arguments ### diff --git a/mail_wrap_script.sh b/mail_wrap_script.sh index 276115c..0773216 100755 --- a/mail_wrap_script.sh +++ b/mail_wrap_script.sh @@ -1,20 +1,51 @@ #!/bin/bash -SCRIPT=$1 -shift +# Default values +IGNORE_EXIST_CODE=0 +SCRIPT="-1" +# Get arguments +while [ $# -gt 1 ]; do + ARG_NAME=$1 + shift + ARG_VALUE=$1 + shift + + case $ARG_NAME in + --ignore-exit-code) + IGNORE_EXIT_CODE=$ARG_VALUE + ;; + --script) + SCRIPT=$ARG_VALUE + break + ;; + esac +done + +# Exit if bad script +if [ "$SCRIPT" = "-1" ]; then + echo No script specified + echo "Sending error mail" + echo -e "No script specified\n\n\n $(ps $PPID)" | $(dirname $0)/mail.sh "[CRITITCAL] $(hostname) undefined script" + echo "Sent" + exit 1 +fi + +# Create temp logfile FILE_ID=$(cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 32 | head -n 1) touch /tmp/$FILE_ID.log chmod a-rwx,u+r /tmp/$FILE_ID.log +# Execute script with remaining arguments $SCRIPT "$@" 2>&1 | tee /tmp/$FILE_ID.log ERR=${PIPESTATUS[0]} OUTPUT=$(cat /tmp/$FILE_ID.log) rm /tmp/$FILE_ID.log -if [ $ERR -ne 0 ]; then +# Send mail in case of error +if [ $ERR -ne 0 ] && [ "$ERR" != "$IGNORE_EXIT_CODE" ]; then echo "Sending error mail" - echo -e "An error occured during the execution of $SCRIPT ($ERR).\n\n$OUTPUT" | $(dirname $0)/mail.sh "$(hostname) $SCRIPT failure" + echo -e "An error occured during the execution of $SCRIPT ($ERR).\n\n\n$(ps $PPID)\n\n$OUTPUT" | $(dirname $0)/mail.sh "$(hostname) $SCRIPT failure" echo "Sent" exit 1 fi From db99afeb1961fede13b453e71a299096c817d434 Mon Sep 17 00:00:00 2001 From: Alice Gaudon Date: Tue, 7 Apr 2020 00:11:38 +0200 Subject: [PATCH 5/8] zfs_snapshot_cron.sh: fix bash leading 0 bug --- zfs_snapshot_cron.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/zfs_snapshot_cron.sh b/zfs_snapshot_cron.sh index 2b454e6..ad7fa83 100755 --- a/zfs_snapshot_cron.sh +++ b/zfs_snapshot_cron.sh @@ -39,13 +39,13 @@ handle_pool() { if [ $D -gt 0 ]; then D=$(( D-1 )) echo "KEEP; D: $D" - elif [ $W -gt 0 ] && [ ${scomp[3]} -eq 1 ]; then + elif [ $W -gt 0 ] && [ ${scomp[3]#0} -eq 1 ]; then W=$(( W-1 )) echo "KEEP; W: $W" - elif [ $M -gt 0 ] && [ ${scomp[3]} -eq 1 ] && [ $(( scomp[4] % 4 )) -eq 0 ]; then + elif [ $M -gt 0 ] && [ ${scomp[3]#0} -eq 1 ] && [ $(( ${scomp[4]#0} % 4 )) -eq 0 ]; then M=$(( M-1 )) echo "KEEP; M: $M" - elif [ $Y -gt 0 ] && [ ${scomp[3]} -eq 1 ] && [ ${scomp[4]} -eq 4 ]; then + elif [ $Y -gt 0 ] && [ ${scomp[3]#0} -eq 1 ] && [ ${scomp[4]#0} -eq 4 ]; then Y=$(( Y-1 )) echo "KEEP; Y: $Y" else From c94d3330bf460eff95da53c03a242900004921ed Mon Sep 17 00:00:00 2001 From: Alice Gaudon Date: Tue, 7 Apr 2020 00:11:56 +0200 Subject: [PATCH 6/8] Formatting --- security-updates-check.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/security-updates-check.sh b/security-updates-check.sh index 4f396e8..aedd788 100755 --- a/security-updates-check.sh +++ b/security-updates-check.sh @@ -4,7 +4,7 @@ UPDATES=$(arch-audit --upgradable | sed "{s/\n/
/g}") if test "$1" != "true" then - UPDATES=$(echo -e "$UPDATES" | grep -v "testing repos\!") + UPDATES=$(echo -e "$UPDATES" | grep -v "testing repos\!") fi echo -en "$UPDATES" From 99baed41c08e44c0c5ddf9f911e361efc407ce58 Mon Sep 17 00:00:00 2001 From: Alice Gaudon Date: Tue, 7 Apr 2020 00:12:30 +0200 Subject: [PATCH 7/8] kernel-updates-check.sh: display output to stdout and fix sendmail condition --- kernel-updates-check.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/kernel-updates-check.sh b/kernel-updates-check.sh index d376c94..814dc29 100755 --- a/kernel-updates-check.sh +++ b/kernel-updates-check.sh @@ -1,8 +1,10 @@ #!/bin/bash +echo "Checking for kernel updates..." UPDATES=$(checkupdates --download) +echo -e "$UPDATES" -if [ ! -z "$(echo -en UPDATES | grep \"linux\")" ]; then +if [ ! -z "$(echo -en $UPDATES | grep linux)" ]; then echo "Updates available!" echo -e "Updates are available:\n\n$UPDATES" | $(dirname $0)/mail.sh "Updates are available on $(hostname)" fi From bb8cf9ffa8c3812afd468386ef6b5d89a8e54223 Mon Sep 17 00:00:00 2001 From: Alice Gaudon Date: Wed, 15 Apr 2020 06:47:16 +0200 Subject: [PATCH 8/8] mail_wrap_script.sh: add --always-send flag --- mail_wrap_script.sh | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/mail_wrap_script.sh b/mail_wrap_script.sh index 0773216..dad022d 100755 --- a/mail_wrap_script.sh +++ b/mail_wrap_script.sh @@ -3,14 +3,25 @@ # Default values IGNORE_EXIST_CODE=0 SCRIPT="-1" +ALWAYS_SEND=0 # Get arguments while [ $# -gt 1 ]; do + # single ARG ARG_NAME=$1 shift + + case $ARG_NAME in + --always-send) + ALWAYS_SEND=1 + continue + ;; + esac + + # ARG + VALUE ARG_VALUE=$1 shift - + case $ARG_NAME in --ignore-exit-code) IGNORE_EXIT_CODE=$ARG_VALUE @@ -43,7 +54,7 @@ OUTPUT=$(cat /tmp/$FILE_ID.log) rm /tmp/$FILE_ID.log # Send mail in case of error -if [ $ERR -ne 0 ] && [ "$ERR" != "$IGNORE_EXIT_CODE" ]; then +if [ $ALWAYS_SEND -ne 0 ] || [ $ERR -ne 0 ] && [ "$ERR" != "$IGNORE_EXIT_CODE" ]; then echo "Sending error mail" echo -e "An error occured during the execution of $SCRIPT ($ERR).\n\n\n$(ps $PPID)\n\n$OUTPUT" | $(dirname $0)/mail.sh "$(hostname) $SCRIPT failure" echo "Sent"