Finally it seems to work:
Python
#!/usr/bin/python
#
# sync Remote ftp directory to local - 2014 by meigrafd
# source: remote
# target: local
#
import time, sys, os
import re #regular expressions
import ftputil
#ftputil docu: http://ftputil.sschwarzer.net/trac/wiki/Documentation
#Dictionary docu: http://openbook.galileocomputing.de/python/python_kapitel_08_006.htm
mirrorDirectories = {}
##----- CONFIG - START ----------------------------------------
ftpHost = "192.168.0.11"
ftpUser = 'pi'
ftpPass = 'raspberry'
## Verzeichnisse die gesynct werden sollen.
# Format: mirrorDirectories.update({"<local>":"<remote>"});
mirrorDirectories.update({"/home/pi/music":"/music"});
mirrorDirectories.update({"/home/pi/movie":"/movie"});
##----- CONFIG - END ------------------------------------------
def logError(message):
try:
sys.stderr.write(message + "\n")
except UnicodeEncodeError:
sys.stderr.write(message.encode("utf8") + "\n")
def onError(err):
print err
def connectFtp():
global connected
connected = True
try:
ftp = ftputil.FTPHost(ftpHost, ftpUser, ftpPass)
except OSError, e:
print >> sys.stderr, 'Got error: "%s"' % e
connected = False
if connected:
return ftp
if __name__ == '__main__':
try:
ftp = connectFtp()
if not connected:
print >> sys.stderr, 'Cant connect to remote! Exiting Script'
sys.exit(1)
for key in mirrorDirectories:
localDir = key
remoteDir = mirrorDirectories.get(key)
if not os.path.exists(localDir):
logError("Local Directory {} doesnt Exists! Skipping!".format(localDir))
else:
#cut maybe ending '/' away..
localPath = localDir.rstrip('/')
remotePath = remoteDir.rstrip('/')
#remove all whitespace characters (space, tab, newline, and so on)
pattern = re.compile(r'\s+')
localPath = re.sub(pattern, '', localPath)
remotePath = re.sub(pattern, '', remotePath)
#change to remote dir
try:
ftp.chdir(remotePath)
except OSError, e:
logError("Remote Directory {} doesnt Exists! Skipping!".format(remotePath))
#print >> sys.stderr, 'Got error: "%s"' % e
continue
#go through remote dir and check if it exists local..
recursive = ftp.walk(remotePath, topdown=False, followlinks=False, onerror=onError)
for dirPath,dirNames,fileNames in recursive:
for fileName in fileNames:
remote_dir = dirPath
local_dir = remote_dir.replace(remotePath, localPath, 1)
if ftp.path.isdir(remote_dir):
print "Checking if remote dir '"+remote_dir+"' exists local: "+local_dir,
if not os.path.isdir(local_dir):
print ".. no, creating new directory!"
os.mkdir(local_dir)
else:
print ".. yes, it exists!"
remote_file = remote_dir+"/"+fileName
local_file = local_dir+"/"+fileName
if ftp.path.isfile(remote_file):
print "Transfering remote file '"+remote_file+"' if its newer as local: "+local_file
ftp.download_if_newer(remote_file, local_file)
else:
print("...its a link? "+remote_file+" skipping...")
except Exception, e:
print "Error: %s occurred" % (e)
except ftputil.error.PermanentError, e:
logError("Permanent Error occurred: %s") % (e)
finally:
ftp.close()
ftp.close()
Display More
Ausgabe:
Code
root@RoPi:~# ./ftp_sync.py
Checking if remote dir '/music/album' exists local: /home/pi/music/album .. no, creating new directory!
Transfering remote file '/music/album/lied.mp3' if its newer as local: /home/pi/music/album/lied.mp3
Checking if remote dir '/music' exists local: /home/pi/music .. yes, it exists!
Transfering remote file '/music/bla.mp3' if its newer a locals: /home/pi/music/bla.mp3
Checking if remote dir '/movie/trailer' exists local: /home/pi/movie/trailer .. no, creating new directory!
Transfering remote file '/movie/trailer/spuren.avi' if its newer as local: /home/pi/movie/trailer/spuren.avi
Checking if remote dir '/movie' exists local: /home/pi/movie .. yes, it exists!
Transfering remote file '/movie/film.mkv' if its newer as local: /home/pi/movie/film.mkv
root@RoPi:~#
root@RoPi:~# ls /home/pi/{music,movie}/*
/home/pi/movie/film.mkv /home/pi/music/bla.mp3
/home/pi/movie/trailer:
spuren.avi
/home/pi/music/album:
lied.mp3
root@RoPi:~#
Display More
Sieht gut aus, oder ?
Er announced zwar das er's transferen würde aber macht er nur wenn's remote_file neuer ist als das locale.