Skip to content

Instantly share code, notes, and snippets.

@upsuper

upsuper/bind-backup.sh

Last active Apr 13, 2021
Embed
What would you like to do?
Script to automatically bind and unbind external USB drive on Synology NAS
#!/bin/bash
SERIAL="00000000"
echo "Looking for device with serial $SERIAL..."
for d in /sys/bus/usb/devices/*-*; do
if [[ -f "$d/serial" ]]; then
serial=$(<"$d/serial")
if [[ "$serial" = "$SERIAL" ]]; then
device="$(basename $d)"
break
fi
fi
done
if [[ -z "$device" ]]; then
echo "Fail to find device with serial $SERIAL"
exit 1
fi
echo "Binding device $device..."
echo "$device" > /sys/bus/usb/drivers/usb/bind || exit 1
echo -n "Waiting for filesystem to mount... "
exec 3< <(tail -f -n1 /var/log/kern.log)
while read line; do
if [[ "$line" = *"[EXFAT] mounted successfully" ]]; then
echo "Mounted!"
break
elif [[ "$line" = *"usb $device: "* && "$line" = *" error "* ]]; then
echo "Error!"
exit 1
fi
done <&3
echo -n "Waiting for filesystem to unmount... "
while read line; do
if [[ "$line" = *"[EXFAT] unmounted successfully" ]]; then
echo "Unmounted!"
break;
fi
done <&3
echo "Unbinding device $device..."
echo "$device" > /sys/bus/usb/drivers/usb/unbind
@e92335i

This comment has been minimized.

Copy link

@e92335i e92335i commented Sep 19, 2017

Hi

Do you have a tutorial on how to use this? Tried it but my error says Looking for device with serial WX11A2426410... Fail to find device with serial WX11A2426410
The usb hard drive device has already been inserted and recognized by the machine (DS3615xs) and properly working. This is the device serial i found using Windows power shell get-disk command as I can't find the device serial on linux/synology.

@schor123

This comment has been minimized.

Copy link

@schor123 schor123 commented Apr 10, 2021

Thanks for the script. Is the unbind command similar to the unmount command?
When I issue the unbind command on the Synology ssh prompt and on a mounted external hdd, I will get a notification (via DSM/email) that the external hdd wasn't ejected properly. Is that a normal behavior? Or do I need to unmount first before issuing the unbind command? THX

@upsuper

This comment has been minimized.

Copy link
Owner Author

@upsuper upsuper commented Apr 11, 2021

@e92335i Sorry for the late reply. I didn't notice that there was comment on this script.

The serial number is not the serial number of the product, but something that the system recognize. You would need to figure out this string through /sys/bus/usb/devices/*/serial.

@upsuper

This comment has been minimized.

Copy link
Owner Author

@upsuper upsuper commented Apr 11, 2021

@schor123 Bind would normally lead to auto mount on DS, and unbind should be done after unmount, because unbind to the system is similar to disconnect physically. As you can see from the script, it explicitly waits for mount and unmount, although such check currently assumes ExFAT file system.

@schor123

This comment has been minimized.

Copy link

@schor123 schor123 commented Apr 11, 2021

Many thanks for the info. Two more questions:

  1. Why would I unbind a device, rather than just unmount it?
  2. I would like to add the script to the task scheduler to bind/mount the device just before a hyper backup, then unmount/unbind it again. Could you give some hints on how to adapt the script to UNmount and unbind the device (just in case it hasn't been unmounted/unbinded), and then bind and re-mount it, just using the script? Would a mount/unmount command issued via the task scheduler (executed as root by default) conflict with any user seeing/accessing the mounted device, or will DSM mount it for all users, anyways?
    Thanks!
@upsuper

This comment has been minimized.

Copy link
Owner Author

@upsuper upsuper commented Apr 12, 2021

IIRC, the reason that I want to unbind a device because with the device still bound, there is no apparent way to mount it again through the control GUI, but if you unbind and bind again, it auto-mounts. As I don't want it to always be mounted, for security (so that it can serve as a semi-cold backup even if it's always attached to the NAS) and possibly saving power as well, I use this script to auto bind then unbind it each time.

@schor123

This comment has been minimized.

Copy link

@schor123 schor123 commented Apr 12, 2021

Ok I see: So you run the script a couple of minutes before the actual backup task and it will continue running DURING the actual backup task as well, waiting for the 'unmount' command as executed at the end of a successful hyper backup, and then unbind the device, right?

@upsuper

This comment has been minimized.

Copy link
Owner Author

@upsuper upsuper commented Apr 12, 2021

That's right.

@schor123

This comment has been minimized.

Copy link

@schor123 schor123 commented Apr 12, 2021

ok, I ran your script as sudo in the ssh console: in the GUI I see the external hdd gets mounted, however, the script stops at 'waiting for the
filesystem to mount'. Apparently, kern.log doesn't output the 'mounted successfully' option:
image

Any ideas?

@schor123

This comment has been minimized.

Copy link

@schor123 schor123 commented Apr 12, 2021

Also, un-mounting (=ejecting) the device via the GUI just adds one single line to kern.log:
image
so I suppose the next part of the script also won't recognize unmounting the hdd

Also, don't you need to put
exec 3< <(tail -f -n1 /var/log/kern.log)
in front of
echo -n "Waiting for filesystem to unmount... " while read line; do if [[ "$line" = *"[EXFAT] unmounted successfully" ]]; then echo "Unmounted!" break; fi done <&3
, as well, in order to read the kern.log?

Thanks!

@upsuper

This comment has been minimized.

Copy link
Owner Author

@upsuper upsuper commented Apr 13, 2021

The script here relies on the log from kernel, and it apparently supports only exFAT filesystem. I have no idea about your system and external disk so I can't really provide any advice. There is probably a better way to detect mounting and unmounting... but as it's working for me, I'm not very motivated to investigate further.

Also, don't you need to put
exec 3< <(tail -f -n1 /var/log/kern.log)
in front of

no, actually that would probably be wrong, IIRC. The previous one would be reused by the second loop.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment