Linux console – programmatical detection of which device a USB storage is assigned to

linuxudevusb

Every morning 9am we plug an external HD to our office server (always to the same USB port).
Every day the HD is new, but all of them are made by the same manufacturer (Freecom).

Everyday at 12am, a script runs backing up everything to the HD.
The first step is to mount the HD in this fashion:

mount /dev/sdc1 /mnt/backup

The problem is sometimes the device file is not /dev/sdc1 but something else (i.e. /dev/sdd1). I still can't figure out why as we are not pluging in anything else and we always use the same port.

So my problem is how to programmatically detect the right device file in a very safe way.
My only idea so far is to grep the content of the /dev/disk/by-id directory to find and extract /dev/xxx from this line:

lrwxrwxrwx 1 root root 10 Sep 24 11:03 usb-Freecom_ToughDrive_1A90102657FF-part1 -> ../../sdc1

Any better suggestions? Any concerns about my way?

I am pretty surprised how lsusb can't help me that much.

Best Answer

the by-id entry is a link, in most cases you can simply use that instead of /dev/sdc1. if not, you can dereference it with readlink

readlink -f /dev/disk/by-id/usb-Freecom_ToughDrive_1A90102657FF-part1
/dev/sdc1
Related Topic