I have a set of hard drives which are rotated for offsite backup. There are several steps that need to be performed when one of the disks is reinserted which I've rolled into a simple bash script. I'd like to execute the script automatically when one of the disks is hot-swapped in.
Ideally I'd have a list of disk-id's (/dev/disk/by-id/...
) for the disks which should trigger the script; when one of them is inserted the script would be ran and the ID of the specific disk would be passed in as a parameter. As an alternative it would be good enough to have a copy of the script per-disk and have each script ran for its respective disk.
I'm running Ubuntu Server 14.04 LTS, and have been skimming similar questions related to USB drives and am looking at udev
and hotplug
as the two systems that I'll likely need to use, but was hoping someone may be able to point me to a specific succinct solution for my needs.
My usual approach to udev rule writing is to find something similar to what I need in
/lib/udev
and complement it with a google search or a quick look at the man pages.For your case, most of the mojo can be found in
/lib/udev/rules.d/60-persistent-storage.rules
. I came up with this rule:You would need one rule for each drive, and your script receives the drive serial as a parameter.
I'm only using the drive serial here, that should be enough but you can rebuild the whole ID as seen in
/dev/disk/by-id
with this string:To check the drive's serial number you can use: