Automated file copying

Q I want to have a folder in my home directory that automatically updates that folder from the same folder on my USB stick when I insert it. If the above description is poor then all I can compare it to is having a Microsoft Windows briefcase. I'm using KDE on a Ubuntu installation.

A KDE has a feature that will run an autorun script when a removable device is mounted, Unfortunately, it's aimed at optical media and doesn't (yet) work with USB devices. The good news is that, with a little scripting, you can do this directly from udev. The first step is to set up a udev rule for your device. This can be specific to one USB key or general enough to match anything with a FAT filesystem (USB keys always use this by default). Add this to the end of the rule:

RUN="/usr/local/bin/synchome &"

The trailing & to detach the process running the script is important - udev stops processing events while the rule is being processed and you don't want your file copying to block the whole of udev. Now you just need a script to do the dirty work of copying the files, using rsync, after making sure that certain conditions are met. Save this as /usr/local/bin/synchome (or whatever name you gave the script in the udev rule) and make it executable with chmod +x /usr/local/bin/synchome.

#!/bin/sh
MYUSER="foo"
[[ ${ACTION} == "add" ]] || exit
if ! mount | grep -q ${DEVNAME}
then
MOUNTPOINT="/media/$(basename
${DEVNAME})"
mkdir -p ${MOUNTPOINT}
mount ${DEVNAME} ${MOUNTPOINT} -o
uid=${MYUSER}
MOUNTED=1
fi
if [[ -d "${MOUNTPOINT}/myfiles" ]] && [[ -d "/
home/${MYUSER}/myfiles" ]]
then
su - ${MYUSER} -c "rsync -ax
${MOUNTPOINT}/myfiles/ /home/${MYUSER}/
myfiles/"
fi
[[ "${MOUNTED}" ]] && umount
${MOUNTPOINT}

The first line stores the name of the target user in a variable - an alternative approach would be to get the username from the name of the directory containing the files. Next we check that the rule has been processed because the device has been connected. Udev rules are run on addition and removal of a device, and the ACTION environment variable is set accordingly. KDE can be set up to automatically mount new devices, so the next seven lines check whether this has happened and mount it if not. Then we check for the presence of your special directory, called myfiles here, in both the USB stick and your home directory before running rsync to copy any new files from the stick.

There are various other options you could use here, such as --delete to remove any files in ~/myfiles that are not on the stick. You could also use Unison (www.cis.upenn.edu/~bcpierce/unison) instead of rsync for two-way synchronisation. We use su to run rsync as the user - not only is it safer to run as little as possible as root, but it also avoids any permission problems later when you could find files in your home directory owned by root. Finally, we unmount the USB stick, but only if it was mounted by the script earlier. If KDE automounted it, we should let KDE take care of unmounting it. This is only one example: there are many possible variations. Read the rsync man page for options that may be useful but be wary of anything that could delete files, especially when it is run automatically in the background.

Follow us on Identi.ca or Twitter

Username:   Password:
Create Account | About TuxRadar