I've been doing a lot on a portable USB flash drive recently. Because this is important work and because horrible things can easily happen to small, easily missed pieces of hardware, I wrote a script to back it up:
Invoking this script from the command line makes it do exactly what I want it to. (Knowing that, of course, I could just title it autorun.sh and put it on my flash drive, but I want to hammer this out.)Code:#!/bin/bash #A script to sync my computer with my USB drives, backing up files when the #drive is plugged in. Takes the name of the drive as the command line #argument. #Do not consider spaces to be field separators. IFS=$'\n' #If the drive is not designed to sync (has no "sync" directory), the program quits. if [ ! -d /media/$1/"$USERNAME"_sync ] then exit 0 fi #Create a corresponding sync folder on the main drive if necessary. mkdir -p /home/sync/$1 #Recursively delete all files on the main drive not on the external drive. cd; cd sync; cd $1 testFile=/media/$1/"$USERNAME"_sync function clean_up_wd { for f in `ls` do testFile=$testFile/$f if [ ! -e $testFile ] then rm -r ./$f elif [ -d $f ] then cd $f clean_up_wd fi testFile=`dirname $testFile` done } clean_up_wd #Copy over all files on the external drive. rsync -r /media/$1/"$USERNAME"_sync/ /home/$USERNAME/sync/$1
My current device is named "CHARON." Running
yieldsCode:$ udevinfo -a -p $(udevinfo -q path -n /dev/disk/by-label/CHARON)
I wrote the following udev rule and put it in 80-programs.rules:Code:looking at device '/devices/pci0000:00/0000:00:13.2/usb3/3-3/3-3:1.0/host4/target4:0:0/4:0:0:0/block/sdb/sdb1': KERNEL=="sdb1" SUBSYSTEM=="block" DRIVER=="" ATTR{start}=="8064" ATTR{size}=="3913856" ATTR{stat}==" 182 1017 3832 504 4 5 9 28 0 448 532" looking at parent device '/devices/pci0000:00/0000:00:13.2/usb3/3-3/3-3:1.0/host4/target4:0:0/4:0:0:0/block/sdb': KERNELS=="sdb" SUBSYSTEMS=="block" DRIVERS=="" ATTRS{range}=="16" ATTRS{removable}=="1" ATTRS{ro}=="0" ATTRS{size}=="3921920" ATTRS{capability}=="13" ATTRS{stat}==" 194 1079 4424 584 4 5 9 28 0 512 612" looking at parent device '/devices/pci0000:00/0000:00:13.2/usb3/3-3/3-3:1.0/host4/target4:0:0/4:0:0:0/block': KERNELS=="block" SUBSYSTEMS=="" DRIVERS=="" looking at parent device '/devices/pci0000:00/0000:00:13.2/usb3/3-3/3-3:1.0/host4/target4:0:0/4:0:0:0': KERNELS=="4:0:0:0" SUBSYSTEMS=="scsi" DRIVERS=="sd" ATTRS{device_blocked}=="0" ATTRS{type}=="0" ATTRS{scsi_level}=="0" ATTRS{vendor}==" " ATTRS{model}=="USB Flash Memory" ATTRS{rev}=="PMAP" ATTRS{state}=="running" ATTRS{timeout}=="30" ATTRS{iocounterbits}=="32" ATTRS{iorequest_cnt}=="0x2175" ATTRS{iodone_cnt}=="0x2175" ATTRS{ioerr_cnt}=="0x2" ATTRS{modalias}=="scsi:t-0x00" ATTRS{evt_media_change}=="0" ATTRS{queue_depth}=="1" ATTRS{queue_type}=="none" ATTRS{max_sectors}=="240" looking at parent device '/devices/pci0000:00/0000:00:13.2/usb3/3-3/3-3:1.0/host4/target4:0:0': KERNELS=="target4:0:0" SUBSYSTEMS=="scsi" DRIVERS=="" looking at parent device '/devices/pci0000:00/0000:00:13.2/usb3/3-3/3-3:1.0/host4': KERNELS=="host4" SUBSYSTEMS=="scsi" DRIVERS=="" looking at parent device '/devices/pci0000:00/0000:00:13.2/usb3/3-3/3-3:1.0': KERNELS=="3-3:1.0" SUBSYSTEMS=="usb" DRIVERS=="usb-storage" ATTRS{bInterfaceNumber}=="00" ATTRS{bAlternateSetting}==" 0" ATTRS{bNumEndpoints}=="02" ATTRS{bInterfaceClass}=="08" ATTRS{bInterfaceSubClass}=="06" ATTRS{bInterfaceProtocol}=="50" ATTRS{modalias}=="usb:v0930p6545d0110dc00dsc00dp00ic08isc06ip50" looking at parent device '/devices/pci0000:00/0000:00:13.2/usb3/3-3': KERNELS=="3-3" SUBSYSTEMS=="usb" DRIVERS=="usb" ATTRS{configuration}=="" ATTRS{bNumInterfaces}==" 1" ATTRS{bConfigurationValue}=="1" ATTRS{bmAttributes}=="80" ATTRS{bMaxPower}=="200mA" ATTRS{urbnum}=="17657" ATTRS{idVendor}=="0930" ATTRS{idProduct}=="6545" ATTRS{bcdDevice}=="0110" ATTRS{bDeviceClass}=="00" ATTRS{bDeviceSubClass}=="00" ATTRS{bDeviceProtocol}=="00" ATTRS{bNumConfigurations}=="1" ATTRS{bMaxPacketSize0}=="64" ATTRS{speed}=="480" ATTRS{busnum}=="3" ATTRS{devnum}=="2" ATTRS{version}==" 2.00" ATTRS{maxchild}=="0" ATTRS{quirks}=="0x0" ATTRS{authorized}=="1" ATTRS{manufacturer}==" " ATTRS{product}=="USB Flash Memory" ATTRS{serial}=="5B8614000401" looking at parent device '/devices/pci0000:00/0000:00:13.2/usb3': KERNELS=="usb3" SUBSYSTEMS=="usb" DRIVERS=="usb" ATTRS{configuration}=="" ATTRS{bNumInterfaces}==" 1" ATTRS{bConfigurationValue}=="1" ATTRS{bmAttributes}=="e0" ATTRS{bMaxPower}==" 0mA" ATTRS{urbnum}=="63" ATTRS{idVendor}=="1d6b" ATTRS{idProduct}=="0002" ATTRS{bcdDevice}=="0206" ATTRS{bDeviceClass}=="09" ATTRS{bDeviceSubClass}=="00" ATTRS{bDeviceProtocol}=="00" ATTRS{bNumConfigurations}=="1" ATTRS{bMaxPacketSize0}=="64" ATTRS{speed}=="480" ATTRS{busnum}=="3" ATTRS{devnum}=="1" ATTRS{version}==" 2.00" ATTRS{maxchild}=="8" ATTRS{quirks}=="0x0" ATTRS{authorized}=="1" ATTRS{manufacturer}=="Linux 2.6.27-7-generic ehci_hcd" ATTRS{product}=="EHCI Host Controller" ATTRS{serial}=="0000:00:13.2" ATTRS{authorized_default}=="1" looking at parent device '/devices/pci0000:00/0000:00:13.2': KERNELS=="0000:00:13.2" SUBSYSTEMS=="pci" DRIVERS=="ehci_hcd" ATTRS{vendor}=="0x1002" ATTRS{device}=="0x4373" ATTRS{subsystem_vendor}=="0x1179" ATTRS{subsystem_device}=="0xff00" ATTRS{class}=="0x0c0320" ATTRS{irq}=="19" ATTRS{local_cpus}=="ffffffff,ffffffff" ATTRS{local_cpulist}=="0-63" ATTRS{modalias}=="pci:v00001002d00004373sv00001179sd0000FF00bc0Csc03i20" ATTRS{broken_parity_status}=="0" ATTRS{msi_bus}=="" looking at parent device '/devices/pci0000:00': KERNELS=="pci0000:00" SUBSYSTEMS=="" DRIVERS==""
It's general because I anticipate using more and different devices at later times. This site suggests that $env{ID_FS_LABEL} is the device's label (in this case, "CHARON"), which is what I intend the command line argument to be.Code:ACTION=="add", KERNEL=="sdb*", SUBSYSTEM=="block", SUBSYSTEMS=="usb", DRIVERS=="usb-storage", RUN+="/usr/local/bin/sync.sh $env{ID_FS_LABEL}"
However, with all this, it still doesn't run when I plug my device in. Does anyone know the problem?



Adv Reply







Bookmarks