Skip to content

Instantly share code, notes, and snippets.

@albertodeste
Last active November 25, 2020 17:19
Show Gist options
  • Save albertodeste/f49c3cf66340cc7d069498c075a0e0fd to your computer and use it in GitHub Desktop.
Save albertodeste/f49c3cf66340cc7d069498c075a0e0fd to your computer and use it in GitHub Desktop.
Simple Raspberry backup script with notification
#/bin/bash
# USER SETTINGS
BACKUP_FOLDER="/srv/repositories"
BACKUP_FILE_NAME=backup-`date +%Y%m%d`.tar.gz
DROPBOX_FOLDER=/Backup
TELEGRAM_USER_ID=""
TELEGRAM_BOT_KEY=""
HEALTH_CHECK_KEY=""
# SYSTEM SETTINGS
DROPBOX_SCRIPT=/opt/Dropbox-Uploader/dropbox_uploader.sh
# PROGRAM VARIABLES
RESULT=OK
tar -c "$BACKUP_FOLDER" | gzip -n > $BACKUP_FILE_NAME
FILESIZE=$(ls -l "$BACKUP_FILE_NAME" | awk -v x=5 '{print $x}')
MD5=$(md5sum "$BACKUP_FILE_NAME" | awk -v x=1 '{print $x}')
$DROPBOX_SCRIPT download $DROPBOX_FOLDER/hash hash
REMOTE_MD5=$(cat hash)
if [ $MD5 != $REMOTE_MD5 ]
then
echo $MD5 > hash
$DROPBOX_SCRIPT upload $BACKUP_FILE_NAME $DROPBOX_FOLDER/$BACKUP_FILE_NAME
$DROPBOX_SCRIPT upload hash $DROPBOX_FOLDER/hash
else
RESULT="SKIPPED (no changes)"
fi
rm $BACKUP_FILE_NAME
rm hash
TEXT="Result: $RESULT%0ADate: `date +%Y-%m-%d:%H:%M:%S`%0ATotal size: $FILESIZE"
URL="https://api.telegram.org/bot$TELEGRAM_BOT_KEY/sendMessage"
curl --fail -s --max-time 10 -d "chat_id=$TELEGRAM_USER_ID&disable_web_page_preview=1&text=$TEXT" $URL > /dev/null || exit 1
curl -fsS --retry 3 "https://hchk.io/$HEALTH_CHECK_KEY" > /dev/null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment