View Single Post
Community Council | Posts: 4,920 | Thanked: 12,867 times | Joined on May 2012 @ Southerrn Finland
#8
Yes, it might get tricky if you want to handle more than just updates. That's the reason I just upsync stuff most of the time, and I have separate directory for things to download to the device from cloud.

Here is a quick&dirty solution for monitoring a directory (/home/user/mydocs/Upload/) and immediately syncing the content to cloud if a new file is dropped there:

Code:
#!/usr/bin/python
import os
import subprocess
import pyinotify

watchmanager = pyinotify.WatchManager()
watchmask =  pyinotify.IN_CREATE

class watchprocess(pyinotify.ProcessEvent):
   def process_IN_CREATE(self, event):
      subprocess.call("/root/upload2cloud.sh", shell=True)

inotifier = pyinotify.Notifier(watchmanager, watchprocess())
ret = watchmanager.add_watch('/home/user/MyDocs/Upload', watchmask, rec=True)

while True:
   try:
      inotifier.process_events()
      if inotifier.check_events():
         inotifier.read_events()
   except KeyboardInterrupt:
      inotifier.stop()
      break
You also need another script that is launched by the python inotify monitor script. I could have reused cloudsync.sh but I decided to strip it down a bit to "/root/upload2cloud.sh", like this:

Code:
G_LOGTAG="cloudsync"
G_LOCKFILE="/var/lock/update2cloud.pid"
G_USERNAME="your_cloudserver_username_goes_here"
G_DNSNAME="dns.name.of.your.cloudserver"
G_LOCALNAME="server.local.ip.address"
G_OWNIP="ip.given.to.me"
G_UPLOAD_DIR="/home/user/MyDocs/Upload/"

logger -t "$G_LOGTAG" "Triggered upload from filesystem"

if [ -e "$G_LOCKFILE" ]; then
  logger -t "$G_LOGTAG" "Cannot get lock $G_LOCKFILE, exiting"
  exit 1
fi

echo "$$" > "$G_LOCKFILE"

## get the wlan state to see if we're in home network
aa=$(/sbin/ifconfig wlan0 | grep "$G_OWNIP")
if [ "$?" -eq "0" ]; then
  G_CLOUDHOST=$G_LOCALNAME
  logger -t "$G_LOGTAG" "Using WLAN connection to server $G_CLOUDHOST"
else
  G_CLOUDHOST=$G_DNSNAME
  logger -t "$G_LOGTAG" "Using GPRS connection to server $G_CLOUDHOST"
fi

G_SYNC=$(/usr/bin/rsync -av $G_UPLOAD_DIR $G_USERNAME@$G_CLOUDHOST:download)
G_RET=$?

logger -t "$G_LOGTAG" "$G_SYNC"

if [ "$G_RET" -eq "0" ]; then
  logger -t "$G_LOGTAG" "Upload tranfer succeeded"
else
  logger -t "$G_LOGTAG" "Upload transfer failed (error code $G_RET)"
fi

rm -f "$G_LOCKFILE"

return 0
 

The Following 7 Users Say Thank You to juiceme For This Useful Post: