From 7e7502696ac2bb825418ae97ca619a8266bca586 Mon Sep 17 00:00:00 2001 From: iFargle Date: Sat, 16 Dec 2023 17:33:07 +0900 Subject: [PATCH] Update script for better error handling --- nixos/hosts/backups-rpi4/backup-script.nix | 25 +++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/nixos/hosts/backups-rpi4/backup-script.nix b/nixos/hosts/backups-rpi4/backup-script.nix index 37c76329..eb2a6172 100644 --- a/nixos/hosts/backups-rpi4/backup-script.nix +++ b/nixos/hosts/backups-rpi4/backup-script.nix @@ -9,9 +9,28 @@ GOTIFY_TOKEN=`cat /var/run/secrets/gotify_token | head -n 1` payload="{\"message\": \"Backup has started\nDate: `date`\", \"priority\": 2,\"title\": \"backups-rpi4: Backup Started\"}" curl -X "POST" "https://gotify.sysctl.io/message" -H "accept: application/json" -H "Content-Type: application/json" -H "X-Gotify-Key: $GOTIFY_TOKEN" -d "$payload" -drive1=`lsblk -o NAME,SERIAL | grep -v crypt | grep 9RGHYP8C | awk {'print $1'}` -drive2=`lsblk -o NAME,SERIAL | grep -v crypt | grep 9RGXW93C | awk {'print $1'}` -drive3=`lsblk -o NAME,SERIAL | grep -v crypt | grep 5QH05G3F | awk {'print $1'}` +drive1=`lsblk -o NAME,SERIAL | grep -v luks | grep 9RGHYP8C | awk {'print $1'}` +drive2=`lsblk -o NAME,SERIAL | grep -v luks | grep 9RGXW93C | awk {'print $1'}` +drive3=`lsblk -o NAME,SERIAL | grep -v luks | grep 5QH05G3F | awk {'print $1'}` + +mount1=`mount -l | grep $drive1` +mounted1=`echo $?` +mount2=`mount -l | grep $drive2` +mounted2=`echo $?` +mount3=`mount -l | grep $drive3` +mounted3=`echo $?` + +echo "status" +echo "$drive1 / $mount1 / $mounted1" +echo "$drive2 / $mount2 / $mounted2" +echo "$drive3 / $mount3 / $mounted3" + +# If any drive isn't found, exit: +if [[ $mounted1 != 0 ]] && [[ munted2 != 0 ]] && [[ $mounted3 != 0 ]] ; then + payload="{\"message\": \"Backup stopped. One or more local drives are not mounted.\nDate: `date`\", \"priority\": 2,\"title\": \"backups-rpi4: Backup Stopped\"}" + curl -X "POST" "https://gotify.sysctl.io/message" -H "accept: application/json" -H "Content-Type: application/json" -H "X-Gotify-Key: $GOTIFY_TOKEN" -d "$payload" + +fi echo "Drive 1: $drive1" echo "Drive 2: $drive2"