Created
October 28, 2012 00:08
-
-
Save robbielj/3966969 to your computer and use it in GitHub Desktop.
sync files in an "autosync" directory on seedbox to local linux NAS (my book live etc)
This file contains hidden or 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
# sync files in an "autosync" directory on seedbox to local linux NAS (my book live etc) | |
# add this script to crontab | |
# this script will check whether local machine is online. | |
import subprocess, os, sys | |
import cPickle as pickle | |
def connchk(host,port): | |
import socket | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
s.settimeout(8) | |
try: | |
s.connect((host, int(port))) | |
s.shutdown(2) | |
return 200 | |
except Exception, e: | |
try: | |
errno, errtxt = e | |
except ValueError: | |
return 404 | |
s.close | |
autosyncdir=$autosyncdir #for example: '/home/xx/tdowndir/autosync/' | |
cf = $autosynclistcachefile #for example: '/home/xx/autosynclstc.txt' | |
synclst = $autosynclist #for example: '/home/xx/autosynclst.txt' | |
localip = $localip #for example: '123.57.59.222' | |
localsshpath = $localsshpath #for example: '[email protected]:/shares/Dump/' | |
if os.stat(cf)[6]==0: | |
cached=[] | |
else: | |
with open(cf,"rb") as handle: | |
cached = pickle.loads(handle.read()) | |
newcache = [] | |
for item in os.listdir(autosyncdir): | |
if item not in cached: | |
newcache.append(item) | |
if newcache!=[]: | |
if connchk(localip, "22")==200: | |
with open(cf,"w+b") as handle: | |
cached = cached + newcache | |
pickle.dump(cached,handle) | |
slst = open(synclst, 'w') | |
for item in newcache: | |
print >> slst, item | |
slst.close() | |
proc = subprocess.Popen(['screen', '-d', '-m', 'rsync', '-avrz', '--progress', '--bwlimit=560', '--files-from='+ cf , autosyncdir, localsshpath]) | |
else: | |
sys.exit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment