Skip to content

Instantly share code, notes, and snippets.

@fbender
Last active December 19, 2023 17:23
Show Gist options
  • Save fbender/b53e580793148ee069ba76fb1eb05684 to your computer and use it in GitHub Desktop.
Save fbender/b53e580793148ee069ba76fb1eb05684 to your computer and use it in GitHub Desktop.
Task source code to remount an ejected external USB drive in Synology DSM (e.g. to enable fully-automated Hyper Backup with auto-eject enabled)
# HOW TO SET UP
# - Go to Task Scheduler and create a new "user-defined script" task for "root" user.
# - Schedule task before e.g. next Hyper Backup run (my sequence is: 00:58 mount task, 01:00
# run drive integrity test, 03:00 run backup, which will eject drive again at the end).
# - Copy this file's content to the "user-defined script" field and save.
# - Make sure execution output logging is configured (e.g. saving to a shared folder).
# - Either run script once and follow next section's info, or run "lsusb" yourself via SSH.
# - Fill in device details in variables below (until "END OF CONFIGURATION").
set -euxo pipefail
lsusb
# Above will show lines like this:
# |__7-1 1a43:aa23:0100 00 3.00 5000MBit/s 150mA 1IF (RAIDSonic USB 3.0 Device 4A4F00000000000001D5)
# ^^^ USBPATH ^^^^ PRODUCTID SERIALNO ^^^^^^^^^^^^^^^^^^^^
# ^^^^ VENDORID ^^^^^^^^^ (part of) DESCRIPTION
# ... from which we get (this info is used to auto-detect USBPATH for use in script):
VENDORID=1a43
PRODUCTID=55aa
DESCRIPTION=Intenso
SERIALNO=31900000000000003D52
# Share name (usbshareX by default, but strongly recommended to configure a custom name for
# the drive via Control Panel -> Shared Folder).
SHARENAME=usb-backup-hdd
# END OF CONFIGURATION, DO NOT EDIT BELOW HERE.
# auto-detect device path (this may change e.g. depending on where the drive is connected)
USBPATH=$( lsusb | grep -Ei "$VENDORID\:$PRODUCTID\:.+($DESCRIPTION.* $SERIALNO)" | sed -E 's/^\s*\|__([[:digit:]]+-[[:digit:]]+)\s+.*/\1/' )
# abort if device already mounted as $SHARENAME
test -z $(sudo synoshare --enum ALL | tail -n+2 | grep $SHARENAME)
# otherwise disable USB port of device, sleep 1 sec, then re-enable USB port
echo 0 | tee /sys/bus/usb/devices/$USBPATH/authorized
sleep 1
echo 1 | tee /sys/bus/usb/devices/$USBPATH/authorized
sleep 15
SHARENAME2=$(synoshare --enum ALL | tail -n+2 | grep $SHARENAME)
echo "Mounted USB$USBPATH as '$SHARENAME2'."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment