Skip to content

Instantly share code, notes, and snippets.

@costastf
Created September 20, 2017 14:10
Show Gist options
  • Save costastf/d9f33c098bd90a5942a0f0179df644ef to your computer and use it in GitHub Desktop.
Save costastf/d9f33c098bd90a5942a0f0179df644ef 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
#!/usr/bin/env python
import os
import pyinotify
import sys
from pyinotify import WatchManager, ThreadedNotifier, ProcessEvent
# from rdiff_backup.Main import Main as backup
from subprocess import Popen
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
self.free = True
def process_IN_ATTRIB(self, event):
self.syncronize(os.path.join(event.path, event.name))
def process_IN_DELETE(self, event):
self.syncronize(os.path.join(event.path, event.name))
def process_IN_CREATE(self, event):
self.syncronize(os.path.join(event.path, event.name))
def process_IN_MODIFY(self, event):
self.syncronize(os.path.join(event.path, event.name))
def process_IN_CLOSE_WRITE(self, event):
self.syncronize(os.path.join(event.path, event.name))
def syncronize(self, local_path):
local_path = os.path.abspath(local_path)
if not os.path.isdir(local_path):
local_path = os.path.basename(local_path)
remote_path = self.file_server + '::' + local_path
cmd = ['rdiff-backup',
local_path,
remote_path]
sync = Popen(cmd)
sync.wait()
if __name__ == "__main__":
user = os.environ.get('USER')
destination = sys.argv[1]
wm = WatchManager()
notifier = ThreadedNotifier(wm, Watch(destination))
notifier.start()
wdd = wm.add_watch(os.environ.get('HOME'), mask, rec=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment