Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training,
learning paths, books, tutorials, and more.

Automatically Synchronize Your Camera and Computer

With a custom hotplug script, you can automatically
synchronize your computer and USB storage device camera just by plugging
in the camera.

On a particular vacation where I brought both my laptop and my
digital camera, I noticed I was offloading the day’s pictures onto my
laptop each night. The manual process was to plug in the camera, mount
it as a USB storage device, create a new directory for the images, copy the images to the new directory, and,
finally, unmount the camera. With a few tweaks to
hotplug and a little detective work, I was able to
make the entire process hands free: I now plug in the camera, and it
automatically synchronizes everything for me. In this hack I describe
the tweaks to hotplug, along with tips for creating
your own custom synchronization script.

Configure Hotplug

Hotplug is a program under Linux that
manages hot-pluggable devices. For the most part it is a program you
don’t have to think about—just plug in your USB mouse, for instance,
and hotplug will make sure the applicable drivers
are loaded. Hotplug is very powerful, however, in that it
allows you to customize what to do when a certain device is plugged
in. The first step to configuring hotplug is to
plug in a camera and scan the system logs to make sure the camera is
recognized. In the case of my camera, hotplug
recognized that it was a USB storage device and made sure my
usb-storage module was already loaded:

USB drives (and in this case cameras that function as USB
drives) work as regular SCSI hard drives in this situation and require
the sd_mod module to be loaded. You could just
mount the device that is created (/dev/sda1
typically, unless multiple drives are plugged in simultaneously), copy
files, and be finished with the camera. But if you want to automatically synchronize this camera to your computer, you must perform special commands just for
this drive, and not for any other USB storage device. To set up
automatic synchronization, you first need some information from
/proc. The
/proc/bus/usb/devices file will show information
about the various USB devices on the system. For instance, here is a
snip from the file when my digital camera is plugged in:

Yeah, it’s a lot of gibberish, but there is information in there
that is useful for setting up hotplug. The parts
that are of interest to you are Vendor=07b4,
ProdID=0105, Product=C740UZ, and Driver=usb-storage. You can use these
sections to configure hotplug to do certain
things when this device is plugged in. Generally
hotplug will detect that a device is a USB
storage device automatically. In case your drive isn’t automatically
detected, you will have to add a configuration line for it in
/etc/hotplug/usb/usb.usermap. The first four
columns of usb.usermap are all that should
concern you, and the information you need for those columns can be
found in those special sections of the
/proc/bus/usb/devices file. The first column
contains the USB module to associate with the device, or what follows
the Driver option (Driver=usb-storage). The second column uses
0x00f for all USB storage devices.
The third and fourth options contain the hex code for the Vendor and Product
ID respectively. The rest of the fields don’t matter—the
first three fields are enough to match this device—so put in 0x00 for those. For my camera, the file
would look like this:

This line in usb.usermap tells
hotplug to run the
/etc/hotplug/usb/usb-storage script whenever it
sees that this device is plugged in. That script can then load modules
or run other programs (including setting up programs to run when the
drive is removed). The next step is to create this file and add an
entry that matches your camera based on the PRODUCT environment variable the
hotplug script generates. Then create the
directory in which the camera will be mounted (in this example
/mnt/camera). The
/etc/hotplug/usb/usb-storage script for my camera
would look like this (replace usernamewith your user’s name):

The second line needs some explanation. USB drives are assigned
SCSI drive designations on the fly starting from
sda and going up. While I could just say
/dev/sda1, if more than one USB device were
plugged in at the time, the wrong device might get mounted. With the
standard Linux device system, there isn’t an easy way to determine
what SCSI drive letter was assigned to the USB drive. A kludge around
this fact is to simply grab the last mention of a USB SCSI drive
assignment out of the system log and cut out the device information.
Based on your kernel version, your system might log this information
slightly differently, so you may have to compare the
grep pattern to your syslog
file and possibly adjust the -f 10
value to match the field that contains the SCSI device entry.

The PRODUCT environment
variable specifies the Vendor and Product IDs for the current USB
device. To see what kind of environment variables are present when
this script is run, you can insert a set >
/tmp/settings line above the case statement. Read through that file to
see how PRODUCT is set for your
product. In my case it was set to 7b4/105/1. Then set up the case statement to mount this camera when
that device is actually plugged in to
/mnt/camera. This method is a bit brittle, as you
will see below, but it does work. Another environment variable,
REMOVER, specifies the name of a
script that is run after the device is removed. This script is empty
by default, so I echoed in the umount command I wanted to use when I
removed the drive.

Next make the script executable with this command:

$ chmod a+x /etc/hotplug/usb/usb-storage

Then restart the hotplug service:

$ /etc/init.d/hotplug restart

Now plug the camera and type df to confirm that
/mnt/camera is mounted. After you unplug the
drive, the drive will be umounted.

Use autofs Instead of Mounting Manually

There are some problems with the previous method. For one, the
drive isn’t unmounted until it already has been removed. This could
result in file system corruption over time if the device failed to
sync before being removed. The solution is to use
autofs to mount the device on demand and then
unmount when idle.

First, install the autofs system with your
distribution’s package manager. Then modify
/etc/auto.master and add a line for your
removable drives:

That last line tells autofs to mount any
removable devices specified in
/etc/auto.removable under
/var/autofs/removable, and to unmount them after
two seconds of idling. Now create the
/etc/auto.removable file:

This file sets up mount points under
/var/autofs/removable/, the mounting options to
use, and indicates which device to mount. All of this happens outside
/etc/fstab or any other mounting, so remove the
/mnt/camera mount point. Instead, have the
USB-storage hotplug script you created make a
symlink to the autofs mount point when the drive
is inserted, and then remove the symlink when the drive is removed.
That way, you can deal only with /mnt/camera and not worry
about /var/autofs directories. The new and
improved /etc/hotplug/usb/usb-storage
follows:

Now, plug in the camera and confirm that
/mnt/camera was created. In my case, I created
the symlink to dig into the camera’s directory structure and take me
directly to the images (/dcim/100olymp). Change
this directory path to match the path your camera uses. Because you
are using autofs, the camera will not
be mounted unless you access /mnt/camera. After
two seconds, autofs will unmount the drive. You
can monitor /var/log/syslog to watch this
happening:

As you can see, the drive unmounted seconds after the command
finished since it had been idling. It would now be safe to remove the
camera.

Make a Synchronization Script

Until now, the /etc/hotplug/usb/usb-storage
script had the following line commented out:

su username -c /home/username/bin/camera_sync

This line executes a custom camera syncing script as the normal
user. This way you can change the script and add features without having to be root. Use su to change
to your user from the root user so all of the images will be accessible by your normal user. The
synchronization script relies on rsync to perform the synchronization logic,
and the complete script looks like this:

This script creates a new directory, named with today’s date,
under a photos directory I created in my home directory. Then
rsync synchronizes the images with that directory
according to their file sizes (since other Unix-style file data isn’t
present on the FAT32 drives most cameras use). The --suffix argument handles any potential
duplicate files that may occur, in case your camera doesn’t create
unique-enough filenames across multiple flash cards. You can also
launch a slideshow tool such as gThumb with this script and
immediately look at thumbnails of all your photos. Once this script is
created, uncomment the corresponding line from
/etc/hotplug/usb/usb-storage and plug in your
camera to test the synchronization. Any pictures on the camera will
appear in a new directory under ~/photos.

Warning

If you don’t clear your photos off the camera between syncs,
plugging in the camera on a different day will cause this script to
create a new directory containing the same photos.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training,
learning paths, books, interactive tutorials, and more.