Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

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)?

share|improve this question
1  
possible duplicate of Autorun a script after I plugged or unplugged an USB device –  Radu Rădeanu Aug 14 '13 at 15:03
    
@radu I dont like the answer given >:-D With the upstart it is probably possible too: you can create events on creation and deletion of files (and an umount removes a directory :) ) If I got time Ill add it to that one ;) –  Rinzwind Aug 14 '13 at 15:07
    
@Rinzwind I don't like as well, but what can I do... The question is the same. –  Radu Rădeanu Aug 14 '13 at 15:13
1  
It's similar, but I'm not sure that it's the same. I'm looking to run a script when a particular usb is ejected. The other question is dealing with plugging and unplugging in general. –  rudivonstaden Aug 14 '13 at 16:20

1 Answer 1

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 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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.