4

I am trying to sync between a folder and usb drive, so that

a) when I plug in a particular usb drive, a script runs to copy any newer files from the usb to the folder; and

b) when I unmount the drive (click eject in nautilus), a script runs to copy any newer files from the folder to the usb.

I am confident that I can use udev and rsync to accomplish part a), but how can I achieve part b)?

1 Answers1

0

The duplicate answer has most of what's necessary. Comments welcome. Plug it in to get the ids:

lsusb

replace the ids and tell it what scripts.

ACTION=="add", ATTRS{idVendor}=="09da", ATTRS{idProduct}=="0260", OWNER="{userid}", RUN+="/usr/local/bin/usb-copy-add.sh"
ACTION=="remove", ATTRS{idVendor}=="09da", ATTRS{idProduct}=="0260", OWNER="{userid}", RUN+="/usr/local/bin/usb-copy-remove.sh"

The usb-copy scripts could be done in multiple ways. They will look something like the following. This is the remove. Reverse things for the add (does not work recursively):

#!/bin/sh
localf={/your/local/folder/}
for x in `ls -1 "$localf"`
do
    file=`basename $x`
    cd {mounted dir}
    if [ "$file" -nt "$x" ]
    then
        cp "$file" "$localf"
    fi
done

or from superuser answer there is a link that describes cp --update (with -r recursive). This is the remove, reverse for the add:

cp -r -u {mounted dir} {/your/local/folder/}

also from the same superuser answer, this is the remove, reverse for the add:

rsync --progress -r -u {mounted dir} {/your/local/folder/}

Hare are some other copy ideas.

grantbow
  • 976