Created
September 21, 2017 07:41
-
-
Save costastf/2f59954afcf9c35cb3da22b96c9c0a18 to your computer and use it in GitHub Desktop.
A hack to sync files on inotify events. Quite old proof of concept on inotify events daemonized
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import os, time, pyinotify, sys | |
from pyinotify import WatchManager, ThreadedNotifier, ProcessEvent | |
from daemon import Daemon | |
from rdiff_backup.Main import Main as backup | |
mask = pyinotify.IN_ATTRIB | pyinotify.IN_DELETE | pyinotify.IN_CREATE | pyinotify.IN_MODIFY | pyinotify.IN_CLOSE_WRITE | |
class Watch(ProcessEvent): | |
def __init__(self, file_server): | |
self.file_server = file_server | |
def process_IN_ATTRIB(self, event): | |
self.syncronize(os.path.join(event.path, event.name)) | |
print "INATTRIB" | |
def process_IN_DELETE(self, event): | |
self.syncronize(os.path.join(event.path, event.name)) | |
print "INDELETE" | |
def process_IN_CREATE(self, event): | |
self.syncronize(os.path.join(event.path, event.name)) | |
print "INCREATE" | |
def process_IN_MODIFY(self, event): | |
self.syncronize(os.path.join(event.path, event.name)) | |
print "INMODIFY" | |
def process_IN_CLOSE_WRITE(self, event): | |
self.syncronize(os.path.join(event.path, event.name)) | |
print "INCLOSEWRITE" | |
def syncronize(self, local_path): | |
if not os.path.isdir(local_path): | |
local_path = os.path.basename(local_path) | |
backup(local_path, self.file_server + '::' + local_path) | |
class SyncronizeHome(Daemon): | |
def __init__(self, pidfile, file_server, stdin='/dev/null', stdout='/dev/stdout', stderr='/dev/null'): | |
self.stdin = stdin | |
self.stdout = stdout | |
self.stderr = stderr | |
self.pid_file = pidfile | |
self.user = os.environ.get('USER') | |
self.file_server = file_server | |
self.destination = self.user + '@' + self.file_server | |
def run(self): | |
wm = WatchManager() | |
notifier = ThreadedNotifier(wm, Watch(self.destination)) | |
notifier.start() | |
wdd = wm.add_watch(os.environ.get('HOME'), mask, rec=True) | |
if __name__ == "__main__": | |
print "ok" | |
print os.environ.get('HOME') | |
remote_ip = sys.argv[2] | |
daemon = SyncronizeHome('/tmp/homeSynchronizer.pid', remote_ip) | |
if len(sys.argv) == 3: | |
if 'start' == sys.argv[1]: | |
daemon.start() | |
elif 'stop' == sys.argv[1]: | |
daemon.stop() | |
elif 'restart' == sys.argv[1]: | |
daemon.restart() | |
else: | |
print "Unknown command" | |
sys.exit(2) | |
sys.exit(0) | |
else: | |
print "usage: %s start|stop|restart remote_ip" % sys.argv[0] | |
sys.exit(2) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment