diff --git a/backup2me.sh b/backup2me.sh index f60f04f..f2b2121 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/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 diff --git a/mail_wrap_script.sh b/mail_wrap_script.sh index 276115c..dad022d 100755 --- a/mail_wrap_script.sh +++ b/mail_wrap_script.sh @@ -1,20 +1,62 @@ #!/bin/bash -SCRIPT=$1 -shift +# 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 + ;; + --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 [ $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$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 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 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" 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" diff --git a/zfs_snapshot_cron.sh b/zfs_snapshot_cron.sh new file mode 100755 index 0000000..ad7fa83 --- /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]#0} -eq 1 ]; then + W=$(( W-1 )) + echo "KEEP; W: $W" + 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]#0} -eq 1 ] && [ ${scomp[4]#0} -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