January 11

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':
ATTRS{bNumInterfaces}==" 1"
ATTRS{version}==" 2.00"
ATTRS{manufacturer}=="SMI Corporation"
ATTRS{product}=="USB DISK"

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.


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


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

Copyright 2021. All rights reserved.

Posted January 11, 2012 by Timothy Conrad in category "Linux

About the Author

If I were to describe myself with one word it would be, creative. I am interested in almost everything which keeps me rather busy. Here you will find some of my technical musings. Securely email me using - PGP: 4CB8 91EB 0C0A A530 3BE9 6D76 B076 96F1 6135 0A1B