-
-
Save gersteba/6b07be49aa94c8df1bb88e7db615987d to your computer and use it in GitHub Desktop.
Autorun Synology Hyper Backup and Integrity Check with Email Notifications
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# This script is to be used in combination with Synology Autorun: | |
# - https://github.com/reidemei/synology-autorun | |
# - https://github.com/Jip-Hop/synology-autorun | |
# | |
# You need to change the task_id to match your Hyper Backup task. | |
# Get it with command: more /usr/syno/etc/synobackup.conf | |
# | |
# I like to keep "Beep at start and end" disabled in Autorun, because I don't | |
# want the NAS to beep after completing (could be in the middle of the night) | |
# But beep at start is a nice way to confirm the script has started, | |
# so that's why this script starts with a beep. | |
# | |
# After the backup completes, the integrity check will start. | |
# Unfortunately in DSM you can't choose to receive email notifications of the integrity check results. | |
# So there's a little workaround, at the end of this script, to send an (email) notification. | |
# The results of the integrity check are taken from the synobackup.log file. | |
# | |
# In DSM -> Control Panel -> Notification I enabled email notifications, | |
# I changed its Subject to %TITLE% and the content to: | |
# Dear user, | |
# | |
# Integrity check for %TASK_NAME% is done. | |
# | |
# %OUTPUT% | |
# | |
# This way I receive email notifications with the results of the Integrity Check. | |
# | |
# Credits: | |
# - https://github.com/Jip-Hop | |
# - https://bernd.distler.ws/archives/1835-Synology-automatische-Datensicherung-mit-DSM6.html | |
# - https://www.beatificabytes.be/send-custom-notifications-from-scripts-running-on-a-synology-new/ | |
task_id=6 # Hyper Backup task id, get it with command: more /usr/syno/etc/synobackup.conf | |
task_name="USB3 3TB Seagate" # Only used for the notification | |
/bin/echo 2 > /dev/ttyS1 # Beep on start | |
startTime=$(date +"%Y/%m/%d %H:%M:%S") # Current date and time | |
device=$2 # e.g. sde1, passed to this script as second argument | |
# Backup | |
/usr/syno/bin/synobackup --backup $task_id --type image | |
while sleep 60 && /var/packages/HyperBackup/target/bin/dsmbackup --running-on-dev $device | |
do | |
: | |
done | |
# Check integrity | |
/var/packages/HyperBackup/target/bin/detect_monitor -k $task_id -t -f -g | |
# Wait a bit before detect_monitor is up and running | |
sleep 60 | |
# Wait until check is finished, poll every 60 seconds | |
/var/packages/HyperBackup/target/bin/detect_monitor -k $task_id -p 60 | |
# Send results of integrity check via email (from last lines of log file) | |
IFS='' | |
output="" | |
title= | |
NL=$'\n' | |
while read line | |
do | |
# Compute the seconds since epoch for the start date and time | |
t1=$(date --date="$startTime" +%s) | |
# Date and time in log line (second column) | |
dt2=$(echo "$line" | cut -d$'\t' -f2) | |
# Compute the seconds since epoch for log line date and time | |
t2=$(date --date="$dt2" +%s) | |
# Compute the difference in dates in seconds | |
let "tDiff=$t2-$t1" | |
# echo "Approx diff b/w $startTime & $dt2 = $tDiff" | |
# Stop reading log lines from before the startTime | |
if [[ "$tDiff" -lt 0 ]]; then | |
break | |
fi | |
text=`echo "$line" | cut -d$'\t' -f4` | |
# Get rid of [Local] prefix | |
text=`echo "$text" | sed 's/\[Local\]//'` | |
if [ -z ${title} ]; then | |
title=$text | |
fi | |
output="$output${NL}$text" | |
done <<<$(tac /var/log/synolog/synobackup.log) | |
# Hijack the ShareSyncError event to send custom message. | |
# This event is free to reuse because I don't use the Shared Folder Sync (rsync) feature. | |
# More info on sending custom (email) notifications: https://www.beatificabytes.be/send-custom-notifications-from-scripts-running-on-a-synology-new/ | |
/usr/syno/bin/synonotify "ShareSyncError" "{\"%OUTPUT%\": \"${output}\", \"%TITLE%\": \"${title}\", \"%TASK_NAME%\": \"${task_name}\"}" | |
# Sleep a bit more before unmounting the disk | |
sleep 60 | |
# Unmount the disk | |
exit 100 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I found the above scripts very helpful in writing a version for my own purposes, which I ended up completely over-engineering. I originally wanted to support running multiple integrity checks in sequence against multiple HyperBackup jobs with targets on more than one external drive. In the end I also arbitrarily decided I only wanted to have to pass a single argument matching a HyperBackup job name and have multiple instances of the script figure out the rest. Pretty sure I made it way too complicated, but just in case anyone finds it useful I'll post it here anyway.
If you use this, be sure to schedule schedule a job to mount your drive(s), then one job for each HyperBackup task you want to check that calls this script with a matching task name argument, and then your actual backup jobs with the setting to remove the destination external device when the backup has completed selected, in that order. Synology queues HyperBackup jobs if they are scheduled while another is already running, so if you have multiple backup tasks targeting one disk, like me, just schedule them in the proper order one right after the other and only set the final one accessing the target disk to remove it.
There is definitely a fair bit of cleanup I could do and improvements I could make, plus there's a less than perfect locking mechanism and some ideas I had for multiple disk support in the functions ended up being unnecessary but left in anyway for now. Oh well.
Click for code