Auto Copy Files When USB Mounted
#1
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

Thanks,

Mike
Reply
#2
I would use a udev rule.
[/code]
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,

/storage/.config/udev.rules.d/99-autocopy.rules
Code:
ACTION=="add", KERNEL=="sd[a-z]1", ENV{ID_FS_LABEL}=="mydrive", RUN+="/storage/.config/myscript.sh"

You can find the ID_FS_LABEL or ID_FS_UUID via udevadm
Code:
udevadm info -q all -n /dev/sdd1
Something like this will show,
Code:
ID_FS_LABEL=mydrive
ID_FS_UUID=ddb88d44-4453-41af-97a3-c4b5621c88bd

For the script I would just use shell, something like

/storage/.config/myscript.sh
Code:
#!/bin/sh

/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
Reply
#3
Nice Smile

I will give it a go and let you know.

Thanks lrusak
Reply
#4
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.

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

Thanks,

Mike
Reply
#5
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/usbcopy.sh"

Script:
#!/bin/sh

/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

thanks,

Mike
Reply
#6
Change

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

to

/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?
Reply
#7
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.

Thank,

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

Thank you
Reply
#9
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?
Reply
#10
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.
Reply



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