From 13e8b15ba3747e752fac6284fbcc094bc9ebdb79 Mon Sep 17 00:00:00 2001 From: n Date: Thu, 10 Aug 2023 21:47:21 +0200 Subject: [PATCH] shellcheck fixes (#2) --- backupnaz.sh | 35 +++++++++++++++++++++------ bootstraponline.sh | 2 ++ cleanmaildir.sh | 12 +++++----- dodo.sh | 2 ++ dump_mysql.sh | 18 +++++++------- dump_postgresql.sh | 18 +++++++------- pivotroot.sh | 10 ++++---- randomwallpaper.sh | 51 +++++++++++++++++---------------------- superscreen | 10 ++++---- supertmux | 25 +++++++++----------- zfsync.sh | 59 ++++++++++++++++++++++------------------------ 11 files changed, 128 insertions(+), 114 deletions(-) diff --git a/backupnaz.sh b/backupnaz.sh index 4635104..c98eda3 100755 --- a/backupnaz.sh +++ b/backupnaz.sh @@ -1,19 +1,40 @@ -#!/bin/bash +#!/bin/sh # Monthly backup script for my NAS -DEVICE=`readlink -f /dev/disk/by-id/usb-ST3000DM_001-1CH166_000000000000-0:0-part1` +#set -e -if grep $DEVICE /proc/mounts >/dev/null +DEVICE=/dev/sdb1 + +pass backups/disk | sudo /sbin/cryptsetup luksOpen $DEVICE backups "$@" + +if grep /dev/mapper/backups /proc/mounts >/dev/null then echo "external drive ($DEVICE) seems to be mounted. good." else - if sudo mount $DEVICE /mnt/usb/ + if sudo mount /dev/mapper/backups /mnt/backups/ then - echo "external drive ($DEVICE) successfuly mounted." + echo " * external drive ($DEVICE) successfuly mounted." else - echo "FATAL: unable to mount" + echo " * FATAL: unable to mount" exit 1 fi fi -time ionice -c 2 sudo rsync --info=progress2 -av --delete --exclude-from /root/.backup_excludes naz:/mnt/md1/ /mnt/usb/ +for slot in public private +do + echo + echo " * backing up $slot" + echo + time ionice -c 2 rsync --rsh='ssh -q -o RemoteCommand=none' --info=progress2 -av --delete --exclude-from ~/.backup_excludes naz:/nfs/$slot/ /mnt/backups/$slot/ +done + +echo +echo " * unmounting" +echo + +sleep 2 +sync + +sudo umount /dev/mapper/backups +sudo dmsetup remove /dev/mapper/backups + diff --git a/bootstraponline.sh b/bootstraponline.sh index 70c5d85..e6e3c33 100644 --- a/bootstraponline.sh +++ b/bootstraponline.sh @@ -1,3 +1,5 @@ +#!/bin/sh + gpart destroy -F ada0 gpart create -s gpt ada0 gpart add -s 222 -a 4k -t freebsd-boot -l boot0 ada0 diff --git a/cleanmaildir.sh b/cleanmaildir.sh index b49fb77..00e8f52 100755 --- a/cleanmaildir.sh +++ b/cleanmaildir.sh @@ -12,13 +12,13 @@ # and maxdays is the maximum number of days before a message is deleted # # put it in the user's crontab: -# 50 * * * * /path/to/cleanmaildir.sh +# 50 * * * * /path/to/cleanmaildir -etcfile=/etc/cleanmaildir.conf +etcfile=/usr/local/etc/cleanmaildir.conf -for i in `cat $etcfile` +grep -v '^#' < $etcfile | while IFS= read -r i do - dir=`echo $i | cut -f 1 -d ,` - time=`echo $i | cut -f 2 -d ,` - find ~/Maildir/$dir/cur/* -mtime +`expr $time - 1` -exec rm '{}' \; + dir=$(echo "$i" | cut -f 1 -d ,) + time=$(echo "$i" | cut -f 2 -d ,) + find "$HOME/Maildir/$dir/cur/" -type f -mtime +$(( time - 1)) -exec rm '{}' \; done diff --git a/dodo.sh b/dodo.sh index e1c2389..95c7c9d 100755 --- a/dodo.sh +++ b/dodo.sh @@ -1 +1,3 @@ +#!/bin/sh + xset dpms force off diff --git a/dump_mysql.sh b/dump_mysql.sh index e741d60..e714be1 100755 --- a/dump_mysql.sh +++ b/dump_mysql.sh @@ -1,20 +1,20 @@ #!/bin/sh -BACKUPDIR=/home/mysql_backup +BACKUPDIR=/home/backups/mysql LOGFILE=${BACKUPDIR}/my_dump_mysql.log -SENDNSCA=/usr/local/sbin/send_nsca\ -H\ \ -d\ ';;'\ -c\ /usr/local/etc/nagios/send_nsca.cfg -HOSTNAME=`/bin/hostname -f | /usr/bin/cut -d '.' -f 1,2` +SENDZABBIX="zabbix_sender --config /usr/local/etc/zabbix6/zabbix_agentd.conf --key mysql.backups" +HOSTNAME=$(hostname -f | cut -d '.' -f 1,2) backup_success=1 mkdir -p ${BACKUPDIR} rm -f ${LOGFILE}; -for i in `echo 'show databases;' | mysql | grep -v Database`; +for i in $(mysql --skip-column-names --silent --raw --execute 'show databases;' | grep --invert-match Database); do - echo -n " ---> backing up ${i}..." >> ${LOGFILE} + printf " ---> backing up %s..." "$i" >> ${LOGFILE} mysqldump --add-drop-table --add-locks --create-options --disable-keys --extended-insert --single-transaction\ - --databases --lock-tables --quick --set-charset $i > ${BACKUPDIR}/$i.sql 2>> ${LOGFILE}; - if [ $? -eq 0 ]; + --databases --lock-tables --quick --set-charset "$i" | lbzip2 > "$BACKUPDIR/$i.sql.bz2" 2>> ${LOGFILE}; + if [ $? ]; then echo " OK" >> ${LOGFILE}; else @@ -24,7 +24,7 @@ done if [ $backup_success -eq 1 ] then - echo "${HOSTNAME};;mysql_backups;;0;OK - backup succeeded at `date`" | ${SENDNSCA} >/dev/null + ${SENDZABBIX} --value 0 >/dev/null else - echo "${HOSTNAME};;mysql_backups;;2;CRITICAL - backup failed at `date`, please check ${LOGFILE} for complete report." | ${SENDNSCA} >/dev/null + ${SENDZABBIX} --value 1 >/dev/null fi diff --git a/dump_postgresql.sh b/dump_postgresql.sh index 48c0aa2..f457746 100755 --- a/dump_postgresql.sh +++ b/dump_postgresql.sh @@ -2,19 +2,19 @@ BACKUPDIR=/home/backups/postgresql LOGFILE=${BACKUPDIR}/my_dump_postgresql.log -SENDNSCA=/usr/local/sbin/send_nsca\ -H\ \ -d\ ';;'\ -c\ /usr/local/etc/nagios/send_nsca.cfg -HOSTNAME=`/bin/hostname -f | /usr/bin/cut -d '.' -f 1,2` +SENDZABBIX="zabbix_sender --config /usr/local/etc/zabbix6/zabbix_agentd.conf --key pgsql.backups" +HOSTNAME=$(/bin/hostname -f | /usr/bin/cut -d '.' -f 1,2) backup_success=1 mkdir -p ${BACKUPDIR} rm -f ${LOGFILE} -cd ${BACKUPDIR} +cd ${BACKUPDIR} || backup_success=0 echo "start" >> ${LOGFILE} -for i in `psql --list --tuples-only --port 6432 | grep -v template0 | grep -v template1 | cut -f 2 -d " " | sed '/^$/d' | tr "\n" " "`; +for i in $(psql --list --tuples-only --port 6432 | grep -v template0 | grep -v template1 | cut -f 2 -d " " | sed '/^$/d' | tr "\n" " "); do - echo -n " ---> backing up ${i}..." >> ${LOGFILE} - pg_dump --port 5432 ${i} > ${i}.sql && rm -f ${i}.sql.bz2 && bzip2 ${i}.sql >> ${LOGFILE}; - if [ $? -eq 0 ]; + printf " ---> backing up %s..." "$i" >> ${LOGFILE} + pg_dump --port 6432 --format c "${i}" > "${i}".dump 2>> ${LOGFILE} 2>&1; + if [ $? ]; then echo " OK" >> ${LOGFILE}; else @@ -24,8 +24,8 @@ done if [ $backup_success -eq 1 ] then - echo "${HOSTNAME};;postgresql_backups;;0;OK - backup succeeded at `date`" | ${SENDNSCA} >/dev/null + ${SENDZABBIX} --value 0 >/dev/null else - echo "${HOSTNAME};;postgresql_backups;;2;CRITICAL - backup failed at `date`, please check ${LOGFILE} for complete report." | ${SENDNSCA} >/dev/null + ${SENDZABBIX} --value 1 >/dev/null fi echo "end" >> ${LOGFILE} diff --git a/pivotroot.sh b/pivotroot.sh index 89d1946..e56b5a8 100755 --- a/pivotroot.sh +++ b/pivotroot.sh @@ -45,16 +45,18 @@ cp -vrp /etc/* ${CHROOTDIR}/etc/ chmod g+w ${CHROOTDIR}/run echo "" > ${CHROOTDIR}/root/.bash_history -echo "TERM=xterm-16color" >>${CHROOTDIR}/root/.profile #necessary for tmux/screen to work -echo "alias halt=\"echo 'o' > /proc/sysrq-trigger\"" >>${CHROOTDIR}/root/.profile -echo "alias reboot=\"echo 'b' > /proc/sysrq-trigger\"" >>${CHROOTDIR}/root/.profile +{ + echo "TERM=xterm-16color" #necessary for tmux/screen to work + echo "alias halt=\"echo 'o' > /proc/sysrq-trigger\"" + echo "alias reboot=\"echo 'b' > /proc/sysrq-trigger\"" +} >> ${CHROOTDIR}/root/.profile pivot_root ${CHROOTDIR} ${CHROOTDIR}/oldroot /sbin/dropbear -p ${CHROOTPORT} echo echo "system pivot-rooted." -echo "you can now connect with ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -p ${CHROOTPORT} root@`hostname`, launch screen/tmux and shred" +echo "you can now connect with ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -p ${CHROOTPORT} root@$(hostname), launch screen/tmux and shred" echo "and finaly use reboot or halt commands (warning: they are not the real thing...)" echo echo "happy wiping!" diff --git a/randomwallpaper.sh b/randomwallpaper.sh index a3ae0a8..ed836e5 100755 --- a/randomwallpaper.sh +++ b/randomwallpaper.sh @@ -1,10 +1,9 @@ -#!/bin/bash +#!/bin/sh -#export DISPLAY=:0.0 echo "DISPLAY:$DISPLAY" -Dir="~/img/wallpap/1920x1200/" -wallpaplist="~/.wallpaplist" +Dir="/home/n/img/wallpap/2560x1440/" +wallpaplist="/home/n/.cache/wallpaplist" contact="n@tourmentine.com" if [ ! -d "$Dir" ]; @@ -19,47 +18,41 @@ then fi while true; do -AvailableFiles=`ls -1 ${Dir} | wc -l` -AlreadyViewedFiles=`cat ${wallpaplist} | wc -l` +AvailableFiles=$(find ${Dir} | wc -l) +AlreadyViewedFiles=$(cat ${wallpaplist} | wc -l) -TotalFiles=`expr $AvailableFiles - $AlreadyViewedFiles` +TotalFiles=$((AvailableFiles-AlreadyViewedFiles)) -if [ $TotalFiles -lt 1 ]; +if [ "$TotalFiles" -lt 1 ]; then echo "*** Seems we have displayed all available wallpapers; let's do the time warp...again. ***" - echo ""> ${wallpaplist} + printf ""> ${wallpaplist} echo "" | /usr/bin/mail -s "Seems we have displayed all available wallpapers; let's do the time warp...again." ${contact} TotalFiles=$AvailableFiles fi -RandomNumber=$(( $RANDOM % $TotalFiles )) -test ! $RandomNumber = 0 || RandomNumber=1 +RandomNumber=$(shuf --input-range=1-"$TotalFiles" --head-count=1) +test ! "$RandomNumber" = 0 || RandomNumber=1 -RandomFile="$( ls -1 ${Dir} | grep -w -v -F -f ${wallpaplist} | head -n $RandomNumber | tail -n 1)" -if [[ ${RandomFile} == "" ]]; +RandomFile="$(find ${Dir} -type f -printf '%f\n' | sort | grep -w -v -F -f ${wallpaplist} | head -n "$RandomNumber" | tail -n 1)" +if [ "${RandomFile}" = "" ]; then echo "*** Oops, something went wrong. erasing cache for security ***" - echo ""> ${wallpaplist} + printf ""> ${wallpaplist} echo "" | /usr/bin/mail -s "Oops, something went wrong. erasing cache for security" ${contact} exit 1 else echo "${RandomFile}" >> ${wallpaplist} fi -if [ -z "${DBUS_SESSION_BUS_ADDRESS}" ] ; then - . ${HOME}/.dbus/session-bus/`ls -rt ${HOME}/.dbus/session-bus/ | tail -1` - export DBUS_SESSION_BUS_ADDRESS DBUS_SESSION_BUS_PID -fi +export DBUS_SESSION_BUS_ADDRESS DBUS_SESSION_BUS_PID -#echo ${DBUS_SESSION_BUS_ADDRESS} - -echo "" | /usr/bin/mail -s "New wallpaper: ${RandomFile}" ${contact} -echo "Selected File: $RandomFile" -xfconf-query -c xfce4-desktop -p /backdrop/screen0/monitor0/image-path -s "${Dir%/}/${RandomFile}" -xfconf-query -c xfce4-desktop -v -p /backdrop/screen0/monitor0/last-image -s "${Dir%/}/${RandomFile}" -xfconf-query -c xfce4-desktop -v -p /backdrop/screen0/monitorVGA1/workspace0/last-image -s "${Dir%/}/${RandomFile}" -xfconf-query -c xfce4-desktop -v -p /backdrop/screen0/monitor0/workspace0/last-image -s "${Dir%/}/${RandomFile}" -pkill gkrellm && gkrellm & -# 3 hours -sleep 10800 +AlreadyViewedFiles=$(cat ${wallpaplist} | wc -l) +echo "" | /usr/bin/mail -s "New wallpaper: ${RandomFile} (${AlreadyViewedFiles}/${AvailableFiles})" ${contact} +echo "Selected File: $RandomFile, which is the ${AlreadyViewedFiles}th out of ${AvailableFiles}" +for workspace in 1 2 3 4 +do + xfconf-query -c xfce4-desktop -p /backdrop/screen0/monitorDisplayPort-${workspace}/workspace0/last-image -s "${Dir%/}/${RandomFile}" 2>/dev/null +done +sleep 3h done diff --git a/superscreen b/superscreen index aff09ac..37e0a7c 100755 --- a/superscreen +++ b/superscreen @@ -16,20 +16,20 @@ then fi # main screen (physical host) -screen -d -m -S $session -t `hostname -s` +screen -d -m -S "$session" -t "$(hostname -s)" # servers' screens : open a ssh connexion from the host to them, then rename it for server in $servers do window=$(( window + 1 )) - screen -S $session -X screen ssh $server - screen -S $session -p $window -X title $server + screen -S "$session" -X screen ssh "$server" + screen -S "$session" -p "$window" -X title "$server" done # do not attach screen if "-d" option is set -if [ ! -z "$1" ] +if [ -n "$1" ] then - if [ $1 == "-d" ] + if [ "$1" = "-d" ] then exit 0 fi diff --git a/supertmux b/supertmux index 92be3fb..bca3c21 100755 --- a/supertmux +++ b/supertmux @@ -1,28 +1,25 @@ #!/bin/sh + # supertmux: open a tmux session with ssh connections to some servers -session=`hostname -s` +session=$(hostname -s) servers="db dns ftp mail web" tmux kill-server tmux start-server -tmux new-session -d -s $session; -tmux rename-window $session - -tmux set-option status-style fg=white,bg=black -tmux set-option -g status-right "[%D %H:%M]" -tmux set-option -g status-justify centre +tmux new-session -d -s "$session"; +tmux rename-window hv counter=1 for server in $servers do - tmux new-window -t $session:$counter - tmux select-window -t $session:$counter - tmux rename-window $server - tmux send -t $session:$counter "ssh $server" ENTER - let counter=$counter+1 >/dev/null + tmux new-window -t "$session":"$counter" + tmux select-window -t "$session":"$counter" + tmux rename-window "$server" + tmux send -t "$session":"$counter" "ssh $server" ENTER + counter=$(( counter + 1 )) done -tmux select-window -t $session:0 -tmux attach-session -t $session +tmux select-window -t "$session":0 +tmux attach-session -t "$session" diff --git a/zfsync.sh b/zfsync.sh index 8bbcd9d..c330a89 100755 --- a/zfsync.sh +++ b/zfsync.sh @@ -8,52 +8,49 @@ # # or manually: # -# - destroy all snapshots on both local & ${bro} +# - destroy all snapshots on both local & $bro # - for i in ftp web mail; do zfs snapshot servers/$i/home@newsync ;done -# - for i in ftp web mail; do zfs send servers/$i/home@newsync | ssh ${bro} zfs receive -vFd servers ;done +# - for i in ftp web mail; do zfs send servers/$i/home@newsync | ssh "$bro" zfs receive -vFd servers ;done # - for i in ftp web mail; do zfs rename servers/$i/home@newsync servers/$i/home@prevsync ;done -# - ssh ${bro} 'for i in ftp web mail; do zfs rename servers/$i/home@newsync servers/$i/home@prevsync ;done' +# - ssh "$bro" 'for i in ftp web mail; do zfs rename servers/$i/home@newsync servers/$i/home@prevsync ;done' -bro="small" +bro="{{ bro }}" pools="ftp mail web" -SENDNSCA=/usr/local/sbin/send_nsca\ -H\ 192.168.X.X\ -d\ ';;'\ -c\ /usr/local/etc/nagios/send_nsca.cfg -HOSTNAME=`/bin/hostname -f | /usr/bin/cut -d '.' -f 1,1` -SERVICENAME=zfsync -LOGFILE=/tmp/zfsync.log +SENDZABBIX="zabbix_sender --config /usr/local/etc/zabbix6/zabbix_agentd.conf --key check.zfsync" +HOSTNAME=$(/bin/hostname -f | /usr/bin/cut -d '.' -f 1,1) LOCKFILE=/tmp/zfsync.lock ZROOT=zdata/servers -sync_success=1 -echo "`date`: ZFSync starting." -if [ -e $LOCKFILE ] +echo "$(date): ZFSync starting." +if [ -e "$LOCKFILE" ] then - echo "ERROR: $0 is already running with pid `cat $LOCKFILE`, check $LOCKFILE" + echo "ERROR: $0 is already running with pid $(cat $LOCKFILE), check $LOCKFILE" exit 1 fi echo ${$}>$LOCKFILE alert() { - echo "${HOSTNAME};;${SERVICENAME};;2;CRITICAL - ZFS Sync failed at `date`, please check ${LOGFILE} for complete report." | ${SENDNSCA} >/dev/null + ${SENDZABBIX} --value 1 >/dev/null exit $? } -if [ $1 ] +if [ "$1" ] then -if [ $1 = "init" ] +if [ "$1" = "init" ] then echo "wiping previous syncs" - for snapshot in `zfs list -t snapshot -o name | grep -e prevsync -e newsync`; do zfs destroy $snapshot ; done - ssh -q ${bro} 'for snapshot in `zfs list -t snapshot -o name | grep -e prevsync -e newsync`; do zfs destroy $snapshot ; done' + for snapshot in $(zfs list -t snapshot -o name | grep -e prevsync -e newsync); do zfs destroy "$snapshot" ; done + ssh -q "$bro" 'for snapshot in $(zfs list -t snapshot -o name | grep -e prevsync -e newsync); do zfs destroy $snapshot ; done' echo "making full snapshots" - for jail in $pools; do zfs snapshot $ZROOT/$jail/home@newsync;done + for jail in $pools; do zfs snapshot "$ZROOT/$jail/home@newsync";done echo "sending full snapshots" - for jail in $pools; do zfs send $ZROOT/$jail/home@newsync | ssh -q ${bro} 'zfs receive -vFdu zdata' ;done + for jail in $pools; do zfs send "$ZROOT/$jail/home@newsync" | ssh -q "$bro" 'zfs receive -vFdu zdata' ;done echo "renaming them to use them as a base for diff snapshots" - for jail in $pools; do zfs rename $ZROOT/$jail/home@newsync $ZROOT/$jail/home@prevsync ;done - ssh -q ${bro} 'for jail in ftp mail web; do zfs rename zdata/servers/$jail/home@newsync zdata/servers/$jail/home@prevsync ;done' + for jail in $pools; do zfs rename "$ZROOT/$jail/home@newsync" "$ZROOT/$jail/home@prevsync" ;done + ssh -q "$bro" 'for jail in ftp mail web; do zfs rename zdata/servers/$jail/home@newsync zdata/servers/$jail/home@prevsync ;done' exit $? fi fi @@ -62,20 +59,20 @@ for pool in $pools do dataset="$ZROOT/${pool}/home" echo "making new snapshot for ${pool}" - zfs snapshot ${dataset}@newsync || alert + zfs snapshot "${dataset}@newsync" || alert echo "sending snapshot" - ssh -q ${bro} "zfs set readonly=on ${dataset}" || alert - sleep 2 - zfs send -i @prevsync ${dataset}@newsync | ssh -q ${bro} "zfs receive -vFdu zdata" || alert - ssh -q ${bro} "zfs set readonly=off ${dataset}" || alert + ssh -q "$bro" "zfs set readonly=on ${dataset}" || alert + sleep 2 + zfs send -i @prevsync "${dataset}@newsync" | ssh -q "$bro" "zfs receive -vFdu zdata" || alert + ssh -q "$bro" "zfs set readonly=off ${dataset}" || alert echo "cleaning" - zfs destroy ${dataset}@prevsync || alert - zfs rename ${dataset}@newsync ${dataset}@prevsync || alert - ssh -q ${bro} "zfs destroy ${dataset}@prevsync && zfs rename ${dataset}@newsync ${dataset}@prevsync" || alert + zfs destroy "${dataset}@prevsync" || alert + zfs rename "${dataset}@newsync" "${dataset}@prevsync" || alert + ssh -q "$bro" "zfs destroy ${dataset}@prevsync && zfs rename ${dataset}@newsync ${dataset}@prevsync" || alert done echo -echo "${HOSTNAME};;${SERVICENAME};;0;OK - ZFS Sync succeeded at `date`" | ${SENDNSCA} >/dev/null +${SENDZABBIX} --value 0 >/dev/null rm -f $LOCKFILE -echo "`date`: ZFSync ending." +echo "$(date): ZFSync ending."