shellcheck fixes (#2)

This commit is contained in:
n 2023-08-10 21:47:21 +02:00
parent cb42356bcd
commit 13e8b15ba3
Signed by: n
GPG key ID: 510227DD6C502CE3
11 changed files with 128 additions and 114 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -1 +1,3 @@
#!/bin/sh
xset dpms force off

View file

@ -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\ <nagios_host>\ -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

View file

@ -2,19 +2,19 @@
BACKUPDIR=/home/backups/postgresql
LOGFILE=${BACKUPDIR}/my_dump_postgresql.log
SENDNSCA=/usr/local/sbin/send_nsca\ -H\ <nagios_host>\ -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}

View file

@ -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!"

View file

@ -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

View file

@ -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

View file

@ -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"

View file

@ -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
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
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."