Skip to content

Instantly share code, notes, and snippets.

@robbielj
Created October 28, 2012 00:08
Show Gist options
  • Save robbielj/3966969 to your computer and use it in GitHub Desktop.
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)
# 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: 'root@123.57.59.222:/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