LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   idea: usb disk becomes available, do something (https://www.linuxquestions.org/questions/linux-newbie-8/idea-usb-disk-becomes-available-do-something-4175479226/)

ezekieldas 10-01-2013 12:26 PM

idea: usb disk becomes available, do something
 
So I've been using this method to backup for some time: USB connection to an external SATA docking station, 1TB disk goes there. I then get on my system run dmesg, confirm the output below, mount /dev/sdd1 /opt.bk, then run a script I have that does all the backup magic.

I'd like to extend/improve this scheme by automating most, or possibly all of it. I'm wondering if anyone can tell me 1) is there a file in /proc that will tell me if this disk is available or not (I've looked and I'm not finding it) 2) does anyone have an example of a daemon or cron run job that watches for something in proc then acts on it?


Code:

usb 1-1.4: new high speed USB device using ehci_hcd and address 3
usb 1-1.4: configuration #1 chosen from 1 choice
Initializing USB Mass Storage driver...
scsi6 : SCSI emulation for USB Mass Storage devices
usb-storage: device found at 3
usbcore: registered new driver usb-storage
usb-storage: waiting for device to settle before scanning
USB Mass Storage support registered.
  Vendor: ST        Model: ST1000DM003-9YN1  Rev: 2.02
  Type:  Direct-Access                      ANSI SCSI revision: 04
SCSI device sdd: 1953525168 512-byte hdwr sectors (1000205 MB)
sdd: Write Protect is off
sdd: Mode Sense: 23 00 00 00
sdd: assuming drive cache: write through
SCSI device sdd: 1953525168 512-byte hdwr sectors (1000205 MB)
sdd: Write Protect is off
sdd: Mode Sense: 23 00 00 00
sdd: assuming drive cache: write through
 sdd: sdd1
sd 6:0:0:0: Attached scsi disk sdd
sd 6:0:0:0: Attached scsi generic sg4 type 0
usb-storage: device scan complete


smallpond 10-01-2013 02:03 PM

What I have in my backup script is to mount my USB drive, test if the backup directory now exists, run the backup, then umount.
The backup script runs from a cron job and adds start and end times to a log file in my home directory so I know how long its taking and whether it completed.

TobiSGD 10-01-2013 03:23 PM

If you want to start a your backup script once your disk becomes available have a look at udev scripts: http://reactivated.net/writing_udev_rules.html

jpollard 10-01-2013 06:21 PM

and if you use udev, you can tie the rule to a specific volumn name so that it backs up only when you plug in a device labeled for backup...

ezekieldas 10-02-2013 10:01 AM

I used this. It's a bit sloppy but seems to work fine.

Code:


cat /proc/bus/usb/devices  | grep "Driver=usb-storage" >/dev/null
if [ "$?" -ne "0" ]; then
  exit 1
fi

dmesg | tail -n 10  | grep sdd1 > /dev/null
if [ "$?" -ne "0" ]; then
  exit 1
fi



All times are GMT -5. The time now is 12:22 AM.