12

I have a USB device (POV camera) that collects a lot of data a fills up its storage regularly. The goal is to write a script that:

  1. Listens for the device to connect via USB
  2. Mounts it as a storage device
  3. Downloads data from the device
  4. Safely unmounts the device for removal (upon completion of the download)

I can add to the script later the option for blinking LEDs attached to certain pins on the GPIO to indicate status...

I've searched for "how to automatically download data from a usb device" on ubuntu and other linux sites, but with no results. Can anyone provide a starting point? I'm comfortable with writing code from documentation and examples.

user3.1415927
  • 303
  • 1
  • 2
  • 14

2 Answers2

13

Use udev rules.

find your device information.

udevadm -a -p /dev/path/device/

KERNELS=="1-3"
SUBSYSTEMS=="usb"
DRIVERS=="usb"
...
ATTRS{quirks}=="0x0"
ATTRS{authorized}=="1"
ATTRS{manufacturer}=="SANDisk"
ATTRS{product}=="USB DISK"
ATTRS{serial}=="SD71011000019113"

Then create your udev rules file for your device. When creating rules file, use information you got from udevinfo command.

content of /etc/udev/rules.d/99-mydevice.rules

SUBSYSTEMS=="usb", ATTRS{serial}=="SD71011000019113", RUN+="/home/gurcan/sync.sh"

Create your script that will run as USB device connected

#!/bin/bash
#
rsync -avz /media/disk/photos/ /data/photos/

Reload udev rules

udevcontrol reload_rules

Test it. unplug/plug

goldilocks
  • 58,859
  • 17
  • 112
  • 227
gurcanozturk
  • 3,748
  • 1
  • 19
  • 17
  • 1
    I tried to run udevinfo, but got a -bash: udevinfo: command not found response from the ssh shell. I checked man and there is a man entry for udev, but not for udevinfo. I will likely try to pursue this with udev... your answer was helpful though, so thanks! I'll look around to check on udevinfo too, maybe I haven't installed a repository or something. – user3.1415927 Jul 12 '14 at 01:30
  • 1
    @user3.1415927 Because of udevinfo replaced by udevadm in debian, i updated my answer. – gurcanozturk Jul 12 '14 at 08:22
2

If you don't like udev, you can take a look on devmon.

It allows these options:

--exec-on-device DEVICE "COMMAND" Execute COMMAND after mounting DEVICE --exec-on-label "LABEL" "COMMAND" Execute COMMAND after mounting LABEL --exec-on-video "COMMAND" Execute COMMAND after video DVD mount --exec-on-audio "COMMAND" Execute COMMAND after audio CD insertion --exec-on-disc "COMMAND" Execute COMMAND after data CD/DVD mount --exec-on-drive "COMMAND" Execute COMMAND after drive mount --exec-on-unmount "COMMAND" Execute COMMAND after unmount --exec-on-remove "COMMAND" Execute COMMAND after drive removal Where the following in COMMAND will be replaced with: %d mount point directory (eg /media/cd) %f device name (eg /dev/sdd1) %l label of mounted volume Multiple --exec-on-XXX options may be used to execute multiple commands. Other exec-on-XXX commands are ignored if exec-on-device or -label executed.

kotrfa
  • 316
  • 1
  • 4