Auto Sync Kindle in Ubuntu

For Christmas I got a Kindle 3 and it was instantly filled with Free eBooks (http://www.gutenberg.org/wiki/Main_Page) and research papers (PDFs). I found myself wanting to be able to automatically synchronize it with a folder on my desktop when it was plugged in and found no one else had posted online how to do this, so I began researching into udev and rsync to get something working.

Setting up udev rules to run a script when the Kindle is plugged in.

Firstly, we need to find some parameters of the Kindle which are unique to it so that udev can identify that it is the Kindle being plugged in and not another USB mass storage device. We’ll need to find out where the device is mounted in /dev/ which can be a little tricky because this folder is pretty full.

An easier way to do this is to first, without the Kindle plugged in do the following command (list the directory and pipe the output to a file called dev1 in the home dir):

ls /dev/ > ~/dev1

Now plug the Kindle in and redo the command, changing the destination output:

ls /dev/ > ~/dev2

The difference between the two files will show what has changed in the /dev/ directory:

 sdiff ~/dev1 ~/dev2

The output for mine (show in the image below) shows that 3 things changed when I plugged in the Kindle, sdb, sdb1 and sg2 directorys were added (shown by the > sign to the left). The folder I am looking for is the sdb1, this is where the mass storage device is found.

Write down where it’s found in /dev/ and then you can clean up those two files you previously made:

rm ~/dev1 | rm ~/dev2

The next thing we want to do is use udevadm to collect some useful information about the device which we can do by the following command (I have chosen to pipe the output to a file called kindle in the home dir, because the output was pretty big. If you don’t want to remove the > ~/kindle from the end of the command):

udevadm info -a -p $(udevadm info -q path -n /dev/sdb1) > ~/kindle

WARNING: This can look pretty daunting but, (in the words of Douglas Adams) Don’t Panic.

  looking at device '/devices/pci0000:00/0000:00:1d.0/usb2/2-2/2-2:1.0/host21/target21:0:0/21:0:0:0/block/sdb/sdb1':
    KERNEL=="sdb1"
    SUBSYSTEM=="block"
    DRIVER==""
    ATTR{partition}=="1"
    ATTR{start}=="16"
    ATTR{size}=="6410672"
    ATTR{alignment_offset}=="0"
    ATTR{discard_alignment}=="4294959104"
    ATTR{stat}=="     151     3389     4239     3776        0        0        0        0        0     2424     3776"
    ATTR{inflight}=="       0        0"

  looking at parent device '/devices/pci0000:00/0000:00:1d.0/usb2/2-2/2-2:1.0/host21/target21:0:0/21:0:0:0/block/sdb':
    KERNELS=="sdb"
    SUBSYSTEMS=="block"
    DRIVERS==""
    ATTRS{range}=="16"
    ATTRS{ext_range}=="256"
    ATTRS{removable}=="1"
    ATTRS{ro}=="0"
    ATTRS{size}=="6410688"
    ATTRS{alignment_offset}=="0"
    ATTRS{discard_alignment}=="0"
    ATTRS{capability}=="51"
    ATTRS{stat}=="     156     3389     4279     3808        0        0        0        0        0     2456     3808"
    ATTRS{inflight}=="       0        0"

  looking at parent device '/devices/pci0000:00/0000:00:1d.0/usb2/2-2/2-2:1.0/host21/target21:0:0/21:0:0:0':
    KERNELS=="21:0:0:0"
    SUBSYSTEMS=="scsi"
    DRIVERS=="sd"
    ATTRS{device_blocked}=="0"
    ATTRS{type}=="0"
    ATTRS{scsi_level}=="3"
    ATTRS{vendor}=="Kindle  "
    ATTRS{model}=="Internal Storage"
    ATTRS{rev}=="0100"
    ATTRS{state}=="running"
    ATTRS{timeout}=="30"
    ATTRS{iocounterbits}=="32"
    ATTRS{iorequest_cnt}=="0xe1"
    ATTRS{iodone_cnt}=="0xe1"
    ATTRS{ioerr_cnt}=="0x1"
    ATTRS{modalias}=="scsi:t-0x00"
    ATTRS{evt_media_change}=="0"
    ATTRS{dh_state}=="detached"
    ATTRS{queue_depth}=="1"
    ATTRS{queue_type}=="none"
    ATTRS{max_sectors}=="240"

While this many look daunting, what you want to do if find some attributes from the output which mean only your Kindle will be found when looking for them all, the problem I have found is that, with udev rules you cannot go too far down in the parent tree which stops me being able to use the Kindles serial to identify it. This could mean that my computer will sync any Kindle that’s plugged into it but I don’t have 2 to check. The following are the attributes I chose:

KERNEL=="sd?", ATTRS{vendor}=="Kindle  ", ATTRS{modalias}=="scsi:t-0x00"

Now you’ll want to write the udev rule in /etc/udev/rules.d/

cd /etc/udev/rules.d/

According to the readme the numbers represent the priority of the rule (higher overriding lower) followed by a descriptive name and it must end in .rules eg (xx-description.rules).

sudo gedit 81-kindle-sync.rules

The following is the whole line I used for the udev rule, the only addition to the above is the RUN+=”/home/alex/.scripts/kindlesync.sh” which will run the script in that directory, this script contains the rsync command.

KERNEL=="sd?", ATTRS{vendor}=="Kindle  ", ATTRS{modalias}=="scsi:t-0x00", RUN+="/home/alex/.scripts/./kindlesync.sh"

Save and close the file and then restart the udev service:

sudo service udev restart

Writing a script to sync a folder on the Desktop with a folder on the Kindle using rsync:
Change the first directory from “/home/alex/ebooks/kindle_sync” to the directory on your computer where you want to keep the files that will be synced onto your Kindle and the second from “/media/Kindle/documents/sync/” to the location on your mounted Kindle where you want the files to be stored, save this file as kindlesync.sh. This is the script you call from the udev rules so for mine it’s saved as “/home/alex/.scripts/kindlesync.sh”.

#!/bin/bash

#Sync
rsync -av /home/alex/eBooks/kindle_sync/ /media/Kindle/documents/sync/

Go to the location of the script and make it executable with the following command:

chmod +x kindlesync.sh

Now try plugging in your Kindle and make sure it syncs up!

Problems

In the udev rule, ACTION==”add” can be used to specify when the Kindle is first plugged in, however when trying to use this Ubuntu’s auto-mounting service mounts the drive after the script is ran so the sync becomes useless. Without this it runs the script 3 times, twice before it has mounted and then once it has mounted – this is currently the best I have found but it’s extremely messy.

4 comments to Auto Sync Kindle in Ubuntu

  • MasonMouse

    Great advice. You can more easily see where your device is being mounted from with a simple “dmesg” though. Run it before and after plugging the device in just to make sure you aren’t seeing a previously mounted device.

  • tobi

    Thank you for the good description! I needed something similar for syncing my android phone. But encountered a problem that the device wasn’t yet mounted when the script was executed. Putting in a “sleep 20s” didn’t help, because it pauses udev so nothing is mounted either.
    But I found a good description to bypass this using a simple ‘trigger script’ which calls the actual script (now with ‘sleep ..’ added in front) and exits quickly, such that udev can go on.
    It’s explained here:
    http://forums.fedoraforum.org/showthread.php?t=242389
    (in the last post).

  • JHPArizona

    Another method to list the kindle device is lsscsi. This will list all sorts of useful information. I did not have my Kindle connected when I ran lsscsi because I stumbled across this posting whilst researching how to sync my new Kindle. Here is the listing from lsscsi on my computer:
    [0:0:0:0] disk ATA Maxtor 6E030L0 NAR6 /dev/sda
    [0:0:1:0] disk ATA WDC WD400BB-32CL 05.0 /dev/sdb
    [1:0:0:0] cd/dvd LITE-ON DVDRW SHW-1635S YS0Z /dev/sr0
    [1:0:1:0] disk IOMEGA ZIP 100 13.A /dev/sdc
    [3:0:0:0] disk ATA Maxtor 6Y080M0 YAR5 /dev/sdd
    [4:0:0:0] disk ATA WDC WD400BB-00DE 05.0 /dev/sde
    [4:0:1:0] disk ATA WDC WD400BB-32CF 02.0 /dev/sdf
    [17:0:0:0] disk SanDisk Cruzer Mini 0.2 /dev/sdg

    You can see that my USB Cruzer Flash drive is listed as being mounted on /dev/sdg. Another tool is lsusb which lists just USB devices but does not return as much useful information. Here is the listing of lsusb from my computer and you can see the Cruzer is listed here too.
    Bus 004 Device 001: ID 0000:0000
    Bus 005 Device 001: ID 0000:0000
    Bus 001 Device 001: ID 0000:0000
    Bus 003 Device 001: ID 0000:0000
    Bus 002 Device 010: ID 0781:5150 SanDisk Corp. SDCZ2 Cruzer Mini Flash Drive (thin)
    Bus 002 Device 001: ID 0000:0000

  • doug

    udevadm info -a -p $(udevadm info -q path -n /dev/sdb1) > ~/kindle

    this command doesnt work

    it says -p option needs parameter

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>