add backup and notifier script

This commit is contained in:
thomasabishop 2025-02-22 14:04:01 +00:00
commit d8e62a632d
5 changed files with 182 additions and 0 deletions

28
home_backup.sh Executable file
View file

@ -0,0 +1,28 @@
#!/bin/bash
# Run timed rsync backups using rsnapshot, executed via cron
# Parameters
# --- $1 = Time schedule ('hourly' | 'daily' | 'weekly' | 'monthly')
USER=thomas
export XDG_RUNTIME_DIR=/run/user/1000
source /home/thomas/.env
SCHEDULE=$1
THIS_FILE=${0}
SLACK_NOTIFIER="/home/thomas/repos/utilities/slack_notifier.sh"
if mountpoint -q /media/samsung-T3; then
ERROR=$(sudo /usr/bin/rsnapshot -c /etc/rsnapshot.conf hourly 2>&1)
STATUS=$?
if [ $STATUS -ne 0 ]; then
$SLACK_NOTIFIER "backups" "error" "${SCHEDULE} /home backup failed" "$ERROR" "$THIS_FILE"
else
$SLACK_NOTIFIER "backups" "success" "${SCHEDULE} /home backup completed"
fi
else
$SLACK_NOTIFIER "backups" "error" \
"${SCHEDULE} /home backup failed" "disk not mounted" \
"$THIS_FILE"
fi

View file

@ -0,0 +1,35 @@
#!/bin/bash
THIS_FILE=${0}
SLACK_NOTIFIER="/home/thomas/repos/utilities/slack_notifier.sh"
FILE_NAME=$(date +%Y-%m-%d_%H:%M:%S-%Z)
BAK=$(ls -dt "/media/crucial/music/"*"music_lib_bak"* | head -n 1)
TEMP="/media/crucial/music/temp"
# Create new backup via Rsync for existing between music ssd and existing bak
# file
sudo rsync -rtv --progress --delete --no-perms --exclude='.Trash*' /media/samsung-T7/ "$BAK"
STATUS=$?
if [ $STATUS -ne 0 ]; then
$SLACK_NOTIFIER "backups" "error" \
"Error creating music library backup:" "problem with rsync." \
"$THIS_FILE"
exit
fi
# Rename the backup file with timestamp
mv "$BAK" "$TEMP"
STATUS_1=$?
mv "$TEMP" "/media/crucial/music/music_lib_bak-$FILE_NAME"
STATUS_2=$?
if [ $STATUS_1 -ne 0 ] || [ $STATUS_2 -ne 0 ]; then
$SLACK_NOTIFIER "backups" "error" \
"Error creating music library backup:" "problem copying backup." \
"$THIS_FILE"
exit
else
$SLACK_NOTIFIER "backups" "success" \
"Successfully created music library backup: /media/crucial/music/music_lib_bak-$FILE_NAME"
fi

23
music_sync/transfer_to_DAP.sh Executable file
View file

@ -0,0 +1,23 @@
#!/bin/bash
SOURCE_DIR="/media/samsung-T7"
DEST_DIR="/media/dap"
# Ensure directories exist
if [ ! -d "$SOURCE_DIR" ]; then
echo "ERROR: Source directory does not exist"
exit 1
fi
if [ ! -d "$DEST_DIR" ]; then
echo "ERROR: Destination directory does not exist"
exit 1
fi
# Sanitise track file names
cd ${SOURCE_DIR} || exit
sudo detox -r ${SOURCE_DIR}
# Transfer to DAP
sudo rsync -rtv --progress --delete --no-perms --exclude='.Trash*' "$SOURCE_DIR/" "$DEST_DIR/"

View file

@ -0,0 +1,14 @@
#!/bin/bash
BLUE='\033[0;34m'
NO_COLOR='\033[0m'
echo -e "${BLUE}INFO Syncing music library to DAP SD card...${NO_COLOR}"
/home/thomas/repos/utilities/music_sync/transfer_to_DAP.sh
sleep 3
echo -e "${BLUE}INFO Backing up music library to additional external drive...${NO_COLOR}"
/home/thomas/repos/utilities/music_sync/backup_music_library.sh
echo -e "${BLUE}INFO Backup completed...${NO_COLOR}"

82
slack_notifier.sh Executable file
View file

@ -0,0 +1,82 @@
#!/bin/bash
# Send error and success notifications to Slack channels and play sound
# Env vars:
# --- Webhook URLs for given channel, eg $SLACK_WEBHOOK_TEST, $SLACK_WEBHOOK_EOLAS
# --- sourced from `.env` file in Zsh path
# Parameters:
# --- $1 = Slack channel,
# --- $2 = type 'error' | 'success'
# --- $3 = Message
# --- $4 = (Opt) Error details
# --- $5 = (Opt) Error source
# Usage:
# --- ./slack_notifier.sh test 'SUCCESS: ...'
# --- ./slack_notifier.sh test 'ERROR: ... ' 'Error details' 'source'
declare -A CHANNEL_TO_WEBHOOK
CHANNEL_TO_WEBHOOK["test"]=$SLACK_WEBHOOK_TEST
CHANNEL_TO_WEBHOOK["backups"]=$SLACK_WEBHOOK_BACKUPS
CHANNEL_TO_WEBHOOK["eolas"]=$SLACK_WEBHOOK_EOLAS
CHANNEL_TO_WEBHOOK["website"]=$SLACK_WEBHOOK_SYSTEMS_OBSCURE
CHANNEL_TO_WEBHOOK["time-tracking"]=$SLACK_WEBHOOK_TIME_TRACKING
WEBHOOK=${CHANNEL_TO_WEBHOOK[$1]}
ERROR_BLOCKS=$(
jq -n \
--arg channel "$1" \
--arg message "$3" \
--arg details "$4" \
--arg source "$5" \
'{
channel: $channel,
blocks: ([
{
type: "section",
text: {
type: "plain_text",
text: "🔴 \($message)"
}
},
{
type: "section",
text: {
type: "mrkdwn",
text: "```\n\($details)\n```"
}
},
{
"type": "context",
"elements": [
{
"type": "plain_text",
text: $source
}
]
}
])
}'
)
# Initialise sound playback
mpv --volume=0 --start=0 --length=0.1 /home/thomas/dotfiles/gruvbox-95/sounds/st-computer-on.mp3
sleep 1
# Process notification
if [ "$2" != "error" ]; then
curl -X POST \
-H 'Content-type: application/json' \
--data '{"text":"🟢 '"$3"'"}' \
"$WEBHOOK"
mpv --volume=100 /home/thomas/dotfiles/sounds/star-trek-computer-success.mp3
else
curl -X POST \
-H 'Content-type: application/json' \
--json "$ERROR_BLOCKS" \
"$WEBHOOK"
mpv --volume=100 /home/thomas/dotfiles/sounds/star-trek-computer-error.mp3
fi