Forum
Auto Copy Files When USB Mounted - Printable Version

+- Forum (http://forum.xbian.org)
+-- Forum: Software (/forum-6.html)
+--- Forum: Kodi (/forum-18.html)
+--- Thread: Auto Copy Files When USB Mounted (/thread-2493.html)



Auto Copy Files When USB Mounted - mjbrown - 24th Sep, 2014 03:31 PM

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


RE: Auto Copy Files When USB Mounted - f1vefour - 26th Sep, 2014 02:00 PM

Are you talking system updates or simple media files?


RE: Auto Copy Files When USB Mounted - mjbrown - 26th Sep, 2014 03:00 PM

(26th Sep, 2014 02:00 PM)f1vefour Wrote:  Are you talking system updates or simple media files?

Just media files. Just want to make it as easy as giving family a USB key to connect and it will copy the new content and update XBMC library.

One of the guys on the XBMC forum posted a possible solution I just need to test it.
http://forum.xbmc.org/showthread.php?tid=205060&pid=1800362#pid1800362

Do you happen to know how i can install Rsync on the Xbian distro? Part of the solution above require Rsync.

Thanks for the response

Mike


RE: Auto Copy Files When USB Mounted - f1vefour - 26th Sep, 2014 03:05 PM

Sure can, it's what I was going to suggest.

sudo apt-get install rsync


RE: Auto Copy Files When USB Mounted - mjbrown - 26th Sep, 2014 03:17 PM

(26th Sep, 2014 03:05 PM)f1vefour Wrote:  Sure can, it's what I was going to suggest.

sudo apt-get install rsync

Excellent.

Let everyone know if i can get it working.

Thanks,


RE: Auto Copy Files When USB Mounted - f1vefour - 26th Sep, 2014 04:08 PM

Good luck.


RE: Auto Copy Files When USB Mounted - mjbrown - 28th Sep, 2014 07:14 PM

I can't seem to get the xbmc-send to work

xbmc-send --action="UpdateLibrary(video)"

Has anyone here used the xbmc-send command with xbian?

Thanks,

Mike


RE: Auto Copy Files When USB Mounted - f1vefour - 28th Sep, 2014 09:13 PM

You can use curl also, but yes I have used it before.


RE: Auto Copy Files When USB Mounted - mjbrown - 1st Oct, 2014 09:56 PM

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


RE: Auto Copy Files When USB Mounted - mjbrown - 2nd Oct, 2014 02:32 PM

Just to mention that running the script manually completes the copy. I just fails to complete when executed from the udev rule.

Since the size of the file is different each time i was thinking udev may have a time out.

Thanks,

Mike


RE: Auto Copy Files When USB Mounted - mk01 - 8th Oct, 2014 02:14 PM

@mjbrown

timeout on udev rules makes perfect sense. otherwise udevtrigger could block booting of system.

to check if this is problem, make your usbcopy.sh just wrapper to launch another - real copy script as spawned process. so in usbcopy.sh called from udev rull do just
Code:
/run/my/real/copy.sh &

this way copy process will start as another process and usbcopy.sh rule will return to udev immediately.

another approach and maybe more suitable would be to re-use xbians usbmount implementation (auto usb storage mounter).

it itself is called by a udev rule, spawned, and mounts your drive. it is already designed to provide "hooks" for user to easy inject the mount and umount events. all you need is to copy your scrtipt into /etc/usbmount/mount.d . all scripts here are started after disk is mounted. you don't need any conf files changes / updates.

it even provides some internal info to the scripts, like UM_MOUNTPOINT is actual dir where the disk is mounted, and others ...
UM_DEVICE - filename of the device node
UM_MOUNTPOINT - mointpoint
UM_FILESYSTEM - filesystem type
UM_MOUNTOPTIONS - mount options that have been passed to the mount command
UM_VENDOR - vendor of the device (empty if unknown)
UM_MODEL - model name of the device (empty if unknown)

some basic doc is at /usr/share/doc/usbmount/README.gz . but again, just put the script into relevant hook folder and you are done.


RE: Auto Copy Files When USB Mounted - f1vefour - 10th Oct, 2014 07:31 PM

I mentioned you could update the library with curl but I never gave an example.

Code:
curl -H "Content-Type: application/json" -u user:pword -d "{\"jsonrpc\":\"2.0\",\"method\":\"VideoLibrary.Scan\",\"id\":\"scan\"}" http://127.0.0.1:8686/jsonrpc