Skip to content

Instantly share code, notes, and snippets.

@TimSC
Created November 20, 2017 15:59
Show Gist options
  • Save TimSC/b7af0e82af18548cb6f2135b4346370b to your computer and use it in GitHub Desktop.
Save TimSC/b7af0e82af18548cb6f2135b4346370b to your computer and use it in GitHub Desktop.
Invoke rsync using python/cron
#!/usr/bin/env python
import subprocess
import fcntl
import os
if __name__=="__main__":
lockFina = "docslock"
fullLockFina = os.path.join(os.path.dirname(os.path.realpath(__file__)), lockFina)
if not os.path.exists(fullLockFina):
fi = open(fullLockFina, "w")
else:
fi = open(fullLockFina, "r")
#Try to get exclusive lock, quit if this fails
#This stops multiple instances of rsync running at the same time
fcntl.flock(fi, fcntl.LOCK_EX | fcntl.LOCK_NB)
# https://stackoverflow.com/a/18482169/4288232
subprocess.call(["rsync", "-avz", "-delete", "-e", "ssh", "/home/user/Documents/", "user@nas:/path/to/backup"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment