Go to ...

Techpository

A Better Technical Repository

RSS Feed

Linux: Write a UDEV rule to automatically back up your USB thumb drive when its plugged in


Write a UDEV rule to automatically back up your USB thumb drive when its plugged in

Have you ever wanted to run an external program when your USB thumb drive is plugged in? For example, it could be handy to automatically back up its contents to the local hard drive. This will save your butt in case you do something foolish and delete everything on it. (I’ve done this). So here’s a simple example to get you started.

I used Writing udev rules as reference. Please refer to that for a more in-depth explanation of rule creation.

First thing to do is plug in the thumb drive to gather some information. Plug it in and find where it’s mounted.

$ mount
/dev/sde1 on /media/disk type vfat (rw,nosuid,nodev,uhelper=hal,shortname=lower,uid=500)

So here we see its mounted at /media/disk and its device name is /dev/sde1. Assuming we want to back up only one particular thumb drive, instead of each one we might have, let’s get some unique information to ID this one.

# udevinfo -a -p /sys/block/sde
~
~
~
looking at parent device '/devices/pci0000:00/0000:00:10.4/usb1/1-3':
KERNELS=="1-3"
SUBSYSTEMS=="usb"
DRIVERS=="usb"
ATTRS{configuration}==""
ATTRS{bNumInterfaces}==" 1"
ATTRS{bConfigurationValue}=="1"
ATTRS{bmAttributes}=="80"
ATTRS{bMaxPower}=="500mA"
ATTRS{urbnum}=="602"
ATTRS{idVendor}=="090c"
ATTRS{idProduct}=="1000"
ATTRS{bcdDevice}=="1100"
ATTRS{bDeviceClass}=="00"
ATTRS{bDeviceSubClass}=="00"
ATTRS{bDeviceProtocol}=="00"
ATTRS{bNumConfigurations}=="1"
ATTRS{bMaxPacketSize0}=="64"
ATTRS{speed}=="480"
ATTRS{busnum}=="1"
ATTRS{devnum}=="5"
ATTRS{version}==" 2.00"
ATTRS{maxchild}=="0"
ATTRS{quirks}=="0x0"
ATTRS{authorized}=="1"
ATTRS{manufacturer}=="SMI Corporation"
ATTRS{product}=="USB DISK"
ATTRS{serial}=="AA71013000016033"

Ok, so I grabbed some information from the block of output that has the unique information we need. This is a parent device to the USB drive’s SCSI block device. We’re interested in the SUBSYSTEMS and serial information. We want to take action on a USB subsystem event where the serial attribute matches the unique serial number of our device. And we’re going to create a UDEV rule to launch a script that will do the backup.

Create a file called 99-MyFlash.rules in /etc/udev/rules.d and write the rule.

/etc/udev/rules.d/99-MyFlash.rules

SUBSYSTEMS=="usb", ATTRS{serial}=="AA71013000016033", RUN+="/tmp/syncit.sh"

And we’ll create the script called syncit.sh that will back up the directory where we saw the drive was mounted, to a directory on the local drive called usb_drive

/tmp/syncit.sh

#!/bin/bash
#
rsync -avz /media/disk/ /data/recover/usb_drive

Make this script executable

$ chmod 755 /tmp/syncit.sh

Now we’ll reload the UDEV rules

# udevcontrol reload_rules

To test, we need to unmount and then plug in the USB drive again.

#umount /dev/sde

When you plug the drive in, UDEV will process this new rule and execute the script. Check that destination directory, it should contain a copy of the files from the USB drive! That backup script is just an example, but some pre-flight checks can be done to make sure things are in place before the backup. Maybe checking for the existence of the target directory and creating it if it’s not there.

By: Wayne