Advertisement
Guest User

autsync

a guest
Feb 8th, 2016
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. import os
  2.  
  3. # Place this in a cron job for say every 2 minutes. To script this use
  4. # crontab -l | { cat; echo "0 0 0 0 0 some entry"; } | crontab -
  5.  
  6. # You may also want to run ntpd on all clients to keep the time in sync.
  7.  
  8. # Array of locations of local and remote locations. I recommend mounting remote folders in
  9. # Example (('/home/user/Documents', '/remotehostname/somefolder'),)
  10. sync_locations = (
  11. ("/home/david/local", "/home/david/server"),
  12. )
  13. # Check if host is up
  14. check_host = "localhost"
  15.  
  16. # Only allow this program to run once!
  17. try:
  18. import socket
  19. s = socket.socket()
  20. host = socket.gethostname()
  21. port = 35636 #make sure this port is not used on this system
  22. s.bind((host, port))
  23. except:
  24. exit()
  25. if not os.path.exists(os.getenv("HOME") + '/.unison_backup'):
  26. os.mkdir(os.getenv("HOME") + '/.unison_backup')
  27.  
  28. if os.path.exists(sync_locations[0][0]) and 0 == os.system('ping -c 1 ' + check_host):
  29. if not os.path.isfile(os.getenv("HOME") + "/.unison_backup/first_sync_complete"):
  30. os.system('notify-send "Unison Backup" "Starting Initial Sync. You will be notified when finished."')
  31. for sync_location in sync_locations:
  32. exit_code = os.system('rsync -r ' + sync_location[1] + "/ " + sync_location[0])
  33. if exit_code != 0:
  34. os.system('notify-send "Could not sync!"')
  35. exit()
  36. open(os.getenv("HOME") + '/.unison_backup/first_sync_complete', 'w').close()
  37. os.system('notify-send "Unison Backup" "Initial Sync Complete"')
  38.  
  39. # Run Unison
  40. for sync_location in sync_locations:
  41. os.system('unison %s %s -batch -prefer newer -times=true' % (sync_location[0], sync_location[1]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement