Auto Copy Files When USB Mounted
Hi guys,

Just got my first Pi for XBMC media center and wanted to see if its possible to auto copy content on a USB Key to a main HD connected to the Pi when the USB key is auto mounted.

The idea is that when I build one for family i can provide new content on the USB key and when they connect the USB it will copy the content to the main HD. Even better if i can prompt a library update after the copy in XBMC.

I had a quick search around and couldn't find anything pre written. I thought for sure i'm not the only one with tech challenged family that update media centers in the same way.

Not even sure what programming language to use. The Pi is running Xbian. Easy to change this if it makes the process achievable


I would use a udev rule.
Have the udev rule look for a usb device with a specific UUID or disk label. Then when it is inserted run a script, something like,

ACTION=="add", KERNEL=="sd[a-z]1", ENV{ID_FS_LABEL}=="mydrive", RUN+="/storage/.config/"

You can find the ID_FS_LABEL or ID_FS_UUID via udevadm
udevadm info -q all -n /dev/sdd1
Something like this will show,

For the script I would just use shell, something like


/storage/.xbmc/addons/network.backup.rsync/bin/rsync -a --ignore-existing /media/<disk label | UUID> /media/<disk label | UUID>

/usr/bin/xbmc-send --action="UpdateLibrary(video)"

/bin/umount /media/<disk label | UUID>

/usr/bin/xbmc-send --action="Notification(All Done,You may saftley remove the drive)"

The above would run on OpenELEC after install the rsync addon. You may need to adjust the paths for xbian.
***This is untested and is just an example, YMMV***
"PPC is too slow, your CPU has no balls to handle HD content." ~ Davilla
"Maybe it's a toaster. Who knows, but it has nothing to do with us." ~ Ned Scott
Nice Smile

I will give it a go and let you know.

Thanks lrusak
I have almost got this working but noticed that the udev job i created must run ahead of the one that sends XBMC the USB drive mounted message.

The rule is named: 99-autocopy.rules. I also tired to remove the numbers to make sure it was processed last but i still have the same result.

Before the rule was created; when the USB drive is connected, XBMC will display the message USB drive mounted. After the Rule was configured the USB drive mounted message is displayed last. It looks like the rule will run first and then after that has completed XBMC will receive the USB drive mounted message.

I have just been testing with a basic script until I had confirmed the rule was working so all it will do is:
sleep for 15
/usr/bin/xbmc-send --action="Notification(All Done,You may safety remove the drive)"

After the "All Done,You may safety remove the drive" message is displayed the USB drive mounted message is received.

Just wanted to see what udev job detects and mounts the USB drive in OpenELEC and displays the message to XBMC so I can try and change the order.


Anyone know of timeouts on udev rules?

I have the rule working and the copy starts but stops at about 77-80mb. So I was thinking the udev rule runs the script and then times out.

udev rule: ACTION=="add", SUBSYSTEM=="block", KERNEL=="sdb1", RUN+="/storage/script/"


/storage/.xbmc/addons/network.backup.rsync/bin/rsync -a -v --ignore-existing /media/USB/NewMovies/* /media/mainhd/
/usr/bin/xbmc-send --action="UpdateLibrary(video)"
/usr/bin/xbmc-send --action="Notification(All Done, You may safely remove the drive)"

From WinSCP I can see the file start and progress and then stop at about the 77mb-80mb.

Any ideas



/storage/.xbmc/addons/network.backup.rsync/bin/rsync -a -v --ignore-existing /media/USB/NewMovies/* /media/mainhd/


/storage/.xbmc/addons/network.backup.rsync/bin/rsync -a -v --ignore-existing /media/USB/NewMovies/* /media/mainhd/ >> /tmp/rsync.log

Then look at /tmp/rsync.log to see whats up. Also have you run your script from the command line?
Sorry i forgot to mention that i have manually run the script and it completes the copy as expected.

I will add the dump to log file and see of rsync terminates with any specific errors.


Hi, i'm interested on the same feature, can you post your code or how it worked on the end? Please

Thank you
I am currently working on this also.

I have setup a usb stick with the same files structure as my media store in my kodi machine. I have successfully used rsymc to copy the files from usb to media store.

I have also managed to get popups to appear in kodi from a script.

Does anyone know of a way to stop Kodi automounting a USB drive when it is plugged in?
Has anyone got any further with this yet as i am building a machine with and internal drive on it running on my Pi for a friend and i want to do the exact same thing on it.

Logout Mark Read Team Forum Stats Members Help
Auto Copy Files When USB Mounted0
This forum uses Lukasz Tkacz MyBB addons.