#!/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 |
Yeah maybe you can also run this script from the task planner instead of Synology Autorun... Not sure. It's supposed to run when a disk is attached. I've sold my Synology gear so can't really help out. I hope you figure it out :)
Hi!
I finally found a chance to try your script.
It succeeds until the integrity check (lines 53 and 57). There I get a 'Permission denied' error. I guess this happens, because I have password protected the backup.
Do you have a hint for me, how I can adapt these lines passing the password to the function, too?
And what about line 42:
Do I have to replace the '$2' with anything?
Thank you very much in advance for any hint!
Dear greetings,
Gerald
I'd try to run the /var/packages/HyperBackup/target/bin/detect_monitor -k $task_id -t -f -g
command manually from a terminal and experiment how to get it working. You'd need to manually specify the task_id in this case of course. If you get it working you can adapt the script accordingly.
There's no need to set the $2 on line 42 to anything when using the script in combination with synology-autorun. It will pass several arguments to autorun.sh.
Thank you for your fast response!
When I run sudo /var/packages/HyperBackup/target/bin/detect_monitor -k $task_id -t -f -g
command manually in the terminal, I am being asked for the password. But how can I run this command within the script automatically passing the password?
I am not using autorun - isn't it not working anymore with DSM7? (see https://bernd.distler.ws/archives/1835-Synology-automatische-Datensicherung-mit-DSM6.html)
Instead I am running the script using the task scheduler. Is then anything to adapt in your script?
Hyper backup jobs can already be scheduled from the Synology GUI right? Maybe it's easier to do that.
autorun.sh
was made specifically to be used combined with synology-aurorun. If you're not using that you'd somehow have to pass the device name of the backup disk as second argument to autorun.sh
. But I don't think hardcoding this value will work well, as it may be different depending on the number of disks and the order in which they were attached...
Unfortunately Hyper backup still does not support running this one after the other:
- Password-encrypted backup to external USB-Drive
- Integrity check
- Unmount external USB-Drive
For this I'm still looking for a scripting solution.
I'm just afraid, I have too little experience in scripting to achieve that.
But your script makes me feel confident of this being possible.
Do you have a hint for me, how to automatically pass/enter the password, which is necessary for the integrity check?
My use case was similar (encrypted backup) and I didn't need to enter/pass the encryption password. Since Hyper backup allows to schedule encrypted backups, the encryption password has to be stored on the Synology NAS itself (and therefore there should be no need to enter/pass it again).
I'd ask for help in some Synology user form if I were you. I can't be of further help since I no longer own Synology devices. Good luck!
Thank you anyway for taking some of your time for me 🤗
EDIT:
Just if you were interested: The problem was not the encryption password, but the owner of the script. As soon as I changed the owner to root, no 'Permission denied' appeared anymore.
EDIT 2:
In DSM there is the Task Scheduler now, which makes it even easier to send notification emails.
Therefore I adapted your script a bit without a need of /usr/syno/bin/synonotify anymore.
It works like a charm - thank you for all your input!
Great job! Glad to hear you got it working. 😄
Thank you for your great script - I have been looking for that for a long time!
I want to achieve following steps using 'Hyper Backup' to backup my DS718+ to an external HDD drive (connected via USB):
Do I understand right, that I can achieve that with your script?
Can I also run that script using the task planner (instead of Synology Autorun)?