Painless Thumbdrive Backups

Exploit udev rules to back up your Flash drive daily or every time you insert it.

Andrew Fabbro has become an Oracle DBA; however, he still has root at home and he welcomes your comments sent to


Andrew Fabbro is a senior technologist living in the Portland, Oregon, area. He's used Linux since Slackware came on floppies and presently works for Con-way, a Fortune 500 transportation company.


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Please see my question above

Benjamin Cathey's picture

Please see my question above - no one ever responded with any help. I thought it was because no one read this - but there have been posts since.

Automatically backup any USB storage device

Thomas Damgaard's picture


I have a server that only serves as backup server.
I've been trying to make a udev rule that would automatically backup any USB storage device connected.
This way, I can just plug in my USB devices to my server, and it is automatically backed up.

However, what I have made so far does not work. I hope you can help me.

Here is my udev rule:

RUN+="/usr/local/bin/ %k"

I hope you can help.

Cannot get this script working

Benjamin Cathey's picture

I read you magazine regularly and was glad when I found this article. However I cannot get it working at all --

I am running Ubuntu 8.04

For starters, when I run the udev check I do not get any values that speficy SYSFS, they are all ATTRS (although there is a line for serial and model) - also there is no BUS line at all.

I tried writing the rule using ATTRS and nothing, I also tried writing the rule using SYSFS (even though that parameter did not appear) and nothing -

They symlink is not even created.

HELP please

So I never heard back on this???

Benjamin Cathey's picture

Well, I asked for help and I never heard back on this - the output of udevinfo -a -p $(udevinfo -q path -n /dev/sdf) does not result in anything similar to what you are suggesting. There is no sysctl line or bus line - i see similar values in here but they are labelled attrs and the udev script just won't work

This is what I ended up making

root@lighthouse:/etc/udev/rules.d# cat 96-backuphome.rules
BUS=="usb", SYSFS{serial}=="0010101640150EE9W", SYMLINK=="tosh_ext", RUN+="/home/benito/scripts/ tosh_ext"

Although usb is listed as


NOT the BUS (although I know that it is) ... and serial looks like ATTRS{serial}== not SYSFS{serial}== as suggested in this article. I figured the reason I hadn't heard back is that no one read this. I read your magazine monthly - maybe I shouldn't bother if I can't get a reply?



How to recover

Derk Tattersall's picture

At the end of your article, you state that you can recover files from the image usong mount like so:

mkdir /mnt/thumb
mount -o loop corsair_drive.backup.0 /mnt/thumb

My own thumb drive (and most such drives, I think) has the data partition on a partition within the drive. You have to use a different mount command:

mount -o loop, offset=xxxxx corsair_drive.backup.0 /mnt/thumb

Determining the value of the offset is a pain. I found a script at that makes it much easier:

offset=$1; shift
limit=$1; shift
while [ $offset -le $limit ]
do if mount -o ro,loop,offset=$offset $* 2> /dev/null
then echo " Successfully mounted starting from offset $offset."
exit 0
[ $(($offset % 1000)) == 0 ] && echo -n . # Progress indicator
echo "No filesystem found up to $offset."
exit 1

I found the article very useful. Thanks.

Derek Tattersall

usb key partitions

Jeff Pipkins's picture

I found it instructive to write the run rule like this:
RUN+="/usr/local/bin/ myserialnum %k"

Then in the script I added echo $0 $@ >>/tmp/log.txt
I found that the script was called several times, with different device names. Then I removed the echo and added an if [ "$2" = "sdb2" ]
so I could mount only the partition I wanted.

I added a mount line in /etc/fstab and used the uid= and gid= to set myself as the owner. I have the script mount the drive, and luckily enough, when I remove the key, the mount goes away.

BTW, I don't use the key for backup, but I've found that the "unison" utility is very useful for syncing the data on the key with the data on either of two systems.

What I'd really like to do is to pop up a window, like gnome-terminal or xterm or something, and then execute an optionally interactive script. Anybody know how to do that? I tried sudo -u jpipkins gnome-terminal, but that didn't work.

Correction/Diff that worked for me

will's picture

Great article. This is something I've had to manually do and now I'm free of that task. Yahooo!

I struggled a little at first because it just didn't work straight away. I'm running Ubuntu Edgy. Then I followed the link to Daniel Drake's "Writing udev Rules" and noticed that his examples all used "==" instead of "=". Each declaration in the udev rule seems to need 2 math symbols. They should be "==" or "+=". Here is my rule and it worked great. Oh the joy!

BUS=="usb", SYSFS{serial}=="00176F962D19E", SYMLINK=="cruzer", RUN+="/usr/local/bin/ cruzer "

Now I just need new laptop with USB2.0 as a gig thumbdrive takes 30 mins to backup.


Luis Sismeiro's picture

Why not use rsync to backup only the modified files? It isn't difficult if the flash isn't encryped.

Luis Sismeiro


Bill Arlofski's picture

Rsync is a great solution for keeping files and/or directories in sync, and is much faster than copying the whole thing each time.

But, rsync is not so great if you sync, then realize that you need a specific version of a file from 2 days ago.

Bill Arlofski
Reverse Polarity

rsync snapshots

Chris's picture

You can get the best of both worlds though (speed of rsync + multiple versions), with the added bonus of consuming less space than multiple full copies.

The solution described there gives the illusion of multiple full copies, while only requiring the space of one copy plus the sum of the deltas.