-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimplePythonFtpSync.py
434 lines (380 loc) · 18.4 KB
/
simplePythonFtpSync.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
from sys import platform
import json
import ntpath
import unicodedata
import threading
import os
import logging
from ftplib import FTP
from ftplib import FTP_TLS
from threading import Lock
from shutil import copy2
from datetime import datetime, timedelta
# --- constant connection values
ftpServerName = ""
ftpU = ""
ftpP = ""
directoriesToWatch = []
useTLS = True
dictOfWatchedDir = {}
synchAtStartupBool = False
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
lock = Lock()
workingDir = "/"
startBackupTime = "02:00"
backupDurationInHours = 1
delayToSend = -1
listOfFilesToBeSent = []
respectBackupHours = False
def sendDataCheck():
# we want to avoid sending data within an interval that goes among <backupDurationInHours> hour before the backup time and <backupDurationInHours> hour after the backup time
try:
if (respectBackupHours):
if (startBackupTime!="" and (":" in startBackupTime)):
listOfParam = startBackupTime.split(":")
backupHour = listOfParam[0]
backupMinute = listOfParam[1]
minorCheck = datetime.today() - timedelta(hours=backupDurationInHours, minutes=0)
majorCheck = datetime.today() + timedelta(hours=backupDurationInHours, minutes=0)
date = datetime.now()
dateAndTimeOfBackup = date.replace(hour=int(backupHour), minute=int(backupMinute))
if (dateAndTimeOfBackup > minorCheck and dateAndTimeOfBackup < majorCheck):
return False
else:
return True
else:
#if anything goes wrong we return true
return True
else:
return True
except Exception as inst:
#if anything goes wrong we return true
return True
class Watcher(threading.Thread):
# DIRECTORY_TO_WATCH = "/home/teo/Desktop/temp"
def __init__(self, localDirPath):
try:
threading.Thread.__init__(self)
self.observer = Observer()
self.localDirPath = localDirPath
except Exception as inst:
logger.error("__init__ in Watcher Error for " + self.localDirPath + " - " + timeStamp())
logger.error(type(inst)) # the exception instance
logger.error(inst.args) # arguments stored in .args
logger.error(inst) # __str__ allows args to be printed directly
def run(self):
try:
event_handler = Handler()
self.observer.schedule(event_handler, self.localDirPath, recursive=True)
self.observer.start()
logger.debug("The thread to monitor " + self.localDirPath + " was run")
try:
while True:
time.sleep(5)
except:
self.observer.stop()
logger.error("An exception occured in the thread loop of " + self.localDirPath)
# we want a service that is always run
w = Watcher(self.localDirPath)
logger.info("Restart the thread to observe changes in files in" + self.localDirPath)
w.start()
logger.info("Join the thread for " + self.localDirPath)
self.observer.join()
except Exception as inst:
logger.error("run in Watcher Error for " + self.localDirPath + " - " + timeStamp())
logger.error(type(inst)) # the exception instance
logger.error(inst.args) # arguments stored in .args
logger.error(inst) # __str__ allows args to be printed directly
class Handler(FileSystemEventHandler):
@staticmethod
def on_any_event(event):
result = None
try:
if event.is_directory:
# do nothing
result = None
elif event.event_type == 'created':
# Take any action here when a file is first created.
# when the file is create the "modified" event is also generated
# so we do not anything
logger.info("Received created event - %s." % event.src_path)
# if (event.src_path in dictOfWatchedDir):
# moveFTPFiles(ftpServerName, ftpU, ftpP, event.src_path, dictOfWatchedDir[event.src_path], useTLS)
elif event.event_type == 'modified':
fileToUpload = event.src_path
# Taken any action here when a file is modified.
logger.info("Received modified event - %s." % fileToUpload)
path_without_filename = path_without_leaf(fileToUpload)
if (path_without_filename in dictOfWatchedDir):
if (delayToSend<=0 and sendDataCheck()):
# directly send the file without any delay
moveFTPFiles(ftpServerName, ftpU, ftpP, fileToUpload, dictOfWatchedDir[path_without_filename], useTLS)
else:
# insert the name of the file in a list that will be elaborated only once every delayToSend seconds
if (fileToUpload not in listOfFilesToBeSent):
listOfFilesToBeSent.append(fileToUpload)
elif event.event_type == 'moved':
# on linux, when a file is modified, the system create a temporal file (.goutputstream...)
# and them move it on the right one
fileToUpload = event.dest_path
# Taken any action here when a file is modified.
logger.info("Received moved event - %s. - " % fileToUpload)
path_without_filename = path_without_leaf(fileToUpload)
if (path_without_filename in dictOfWatchedDir):
if (delayToSend<=0 and sendDataCheck()):
# directly send the file without any delay
moveFTPFiles(ftpServerName, ftpU, ftpP, fileToUpload, dictOfWatchedDir[path_without_filename], useTLS)
else:
# insert the name of the file in a list that will be elaborated only once every delayToSend seconds
if (fileToUpload not in listOfFilesToBeSent):
listOfFilesToBeSent.append(fileToUpload)
# path_without_filename = path_without_leaf(fileToUpload)
# if (path_without_filename in dictOfWatchedDir):
# moveFTPFiles(ftpServerName, ftpU, ftpP, fileToUpload, dictOfWatchedDir[path_without_filename], useTLS)
elif event.event_type == 'deleted':
# do nothing
result = None
except Exception as inst:
logger.error("on_any_event Error - " + timeStamp())
logger.error(type(inst)) # the exception instance
logger.error(inst.args) # arguments stored in .args
logger.error(inst) # __str__ allows args to be printed directly
def path_without_leaf(path):
result = ""
try:
head, tail = ntpath.split(path)
result = head
except Exception as inst:
logger.error("path_without_leaf Error - " + timeStamp())
logger.error(type(inst)) # the exception instance
logger.error(inst.args) # arguments stored in .args
logger.error(inst) # __str__ allows args to be printed directly
result = ""
return result
#return the name of the file + extension
def path_leaf(path):
result = ""
try:
head, tail = ntpath.split(path)
result = tail or ntpath.basename(head)
except Exception as inst:
logger.error("Path_leaf Error - " + timeStamp())
logger.error(type(inst)) # the exception instance
logger.error(inst.args) # arguments stored in .args
logger.error(inst) # __str__ allows args to be printed directly
result = ""
return result
def moveFTPFiles(serverName, userName, passWord, fileToUpload, remoteDirectoryPath, useTLS=False):
lock.acquire(True)
"""Connect to an FTP server and bring down files to a local directory"""
if (serverName != '' and userName != '' and passWord != ''):
try:
ftp = None
if useTLS:
ftp = FTP_TLS(serverName, timeout=120)
if (ftp == None):
logger.info("LOG moveFTPFiles 3TLS ftp null")
else:
ftp = FTP(serverName)
ftp.login(userName, passWord)
if useTLS:
ftp.prot_p()
ftp.cwd(remoteDirectoryPath)
ftp.set_pasv(True)
filesMoved = 0
try:
logger.info("Connecting...")
# create a full local filepath
localFileName = path_leaf(fileToUpload)
if localFileName != "" and not localFileName.startswith("."):
#create a copy of the file before sending it
tempFile = create_temporary_copy(fileToUpload, workingDir, localFileName)
# open a the local file
fileObj = open(tempFile, 'rb')
# Download the file a chunk at a time using RETR
ftp.storbinary('STOR ' + localFileName, fileObj)
# Close the file
fileObj.close()
filesMoved += 1
# remove the temp file
# to remove it I need to have the right permissions
os.chmod(tempFile, 0777)
os.remove(tempFile)
logger.info("Uploaded file: " + str(fileToUpload) + " on " + timeStamp())
except Exception as inst:
logger.error(type(inst)) # the exception instance
logger.error(inst.args) # arguments stored in .args
logger.error(inst) # __str__ allows args to be printed directly
logger.error("Connection Error - " + timeStamp())
ftp.quit() # Close FTP connection
ftp = None
except Exception as inst:
logger.error("Couldn't find server")
logger.error(type(inst)) # the exception instance
logger.error(inst.args) # arguments stored in .args
logger.error(inst) # __str__ allows args to be printed directly
else:
logger.error(
"Connection was not possible because one of the following var was not set in the configuration file: ftpServerName , ftpU or ftpP")
lock.release()
def create_temporary_copy(path, temp_dir, fileName):
if platform == "win32":
fileToCheck = temp_dir + '\\' + fileName
else:
fileToCheck = temp_dir + '/' + fileName
# if the file already exists, we will delete it
exists = os.path.isfile(fileToCheck)
if exists:
os.chmod(fileToCheck, 0777)
os.remove(fileToCheck)
temp_path = os.path.join(temp_dir, fileName)
copy2(path, temp_path)
return temp_path
def timeStamp():
"""returns a formatted current time/date"""
import time
return str(time.strftime("%a %d %b %Y %I:%M:%S %p"))
def synchAtStartup():
try:
if (isinstance(directoriesToWatch, list) and len(directoriesToWatch) > 0):
for dest in directoriesToWatch:
if ('localDirPath' in dest and 'remoteDirPath' in dest):
logger.info("synchAtStartup: synchronizing " + dest['localDirPath'])
arr = os.listdir(dest['localDirPath'])
for file in arr:
if os.path.isdir(file) != True:
moveFTPFiles(ftpServerName, ftpU, ftpP, dest['localDirPath'] + "/" + file,
dest['remoteDirPath'], useTLS)
except Exception as inst:
logger.error("synchAtStartup Error - " + timeStamp())
logger.error(type(inst)) # the exception instance
logger.error(inst.args) # arguments stored in .args
logger.error(inst) # __str__ allows args to be printed directly
def elaborateAllChangedFiles():
global listOfFilesToBeSent
if (sendDataCheck()):
while listOfFilesToBeSent:
fileToUpload = listOfFilesToBeSent[0]
##fileToUpload = listOfFilesToBeSent.pop(0)
path_without_filename = path_without_leaf(fileToUpload)
if (path_without_filename in dictOfWatchedDir):
moveFTPFiles(ftpServerName, ftpU, ftpP, fileToUpload, dictOfWatchedDir[path_without_filename], useTLS)
#when the system has finished to upload the file, we remove it from the list
listOfFilesToBeSent.remove(listOfFilesToBeSent[0])
#listOfFilesToBeSent.pop(-1)
else:
logger.info("Not sending data because there could be a running update")
#re-instantiate the thread that should send data
threading.Timer(delayToSend, elaborateAllChangedFiles).start()
if __name__ == '__main__':
try:
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
# create a file handler
handler = logging.FileHandler('log.log')
handler.setLevel(logging.DEBUG)
# create a logging format
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
# add the handlers to the logger
logger.addHandler(handler)
logger.info("loading configuration set")
# read configuration parameters from the config.json file
with open('config.json') as json_data_file:
data = json.load(json_data_file)
if 'configuration' in data:
conf = data['configuration']
if 'directoriesToWatch' in conf:
directoriesToWatch = conf['directoriesToWatch']
if 'ftpServerName' in conf:
ftpServerName = conf['ftpServerName']
if 'ftpUser' in conf:
ftpU = conf['ftpUser']
if 'ftpPass' in conf:
ftpP = conf['ftpPass']
if 'useTLS' in conf:
useTLS = conf['useTLS']
if 'synchAtStartup' in conf:
synchAtStartupBool = conf['synchAtStartup']
if 'workingDir' in conf:
workingDir = conf['workingDir']
if 'startBackupTime' in conf:
startBackupTime = conf['startBackupTime']
if 'backupDurationInHours' in conf:
backupDurationInHours = conf['backupDurationInHours']
if 'respectBackupHours' in conf:
respectBackupHours = conf['respectBackupHours']
if (not respectBackupHours):
logger.info("The script will NOT respect backup hours (so it will upload file also during backup hours)")
#check if the folder exists, otherwise, create it
try:
if not os.path.exists(workingDir):
os.makedirs(workingDir)
except Exception as inst:
if platform == "win32":
workingDir = '/'
else:
workingDir = 'C:\\'
try:
if 'delayToSend' in conf:
if (isinstance(conf['delayToSend'], int)):
delayToSend = conf['delayToSend']
else:
delayToSend = int(conf['delayToSend'],10)
except Exception as inst:
delayToSend = -1
wrongPath = False
logger.info("check correctness of links")
if platform == "win32":
# Windows...
if (isinstance(directoriesToWatch, list) and len(directoriesToWatch) > 0):
for dest in directoriesToWatch:
if ('/' in dest['localDirPath']):
wrongPath = True
logger.error("In the configuraton file you set a path (" + dest[
'localDirPath'] + ") as a linux-like path but you are under linux")
#same check for the workingDir variable
if ('/' in workingDir):
wrongPath = True
logger.error("In the configuraton file you set a path (" + workingDir + ") as a linux-like path but you are under linux")
elif platform == "linux" or platform == "linux2" or platform == "darwin":
# linux or OSX
if (isinstance(directoriesToWatch, list) and len(directoriesToWatch) > 0):
for dest in directoriesToWatch:
if ('\\' in dest['localDirPath']):
wrongPath = True
logger.error("In the configuraton file you set a path (" + dest[
'localDirPath'] + ") as a windows-like path but you are under linux")
# same check for the workingDir variable
if ('\\' in workingDir):
wrongPath = True
logger.error(
"In the configuraton file you set a path (" + workingDir + ") as a windows-like path but you are under linux")
if (wrongPath):
logger.error("Exiting due to errors in the path (e.g., we are under Windows but the user set a linux path)")
exit()
else:
if synchAtStartupBool:
logger.info("synchronizing files at startup")
synchAtStartup()
if (isinstance(directoriesToWatch, list) and len(directoriesToWatch) > 0):
for dest in directoriesToWatch:
if 'localDirPath' in dest and 'remoteDirPath' in dest:
localDirPath = unicodedata.normalize('NFKD', dest['localDirPath']).encode('ascii', 'ignore')
remoteDirPath = unicodedata.normalize('NFKD', dest['remoteDirPath']).encode('ascii',
'ignore')
dictOfWatchedDir[localDirPath] = remoteDirPath
w = Watcher(localDirPath)
logger.info("Start the thread to observe changes in files in" + localDirPath)
w.start()
if (delayToSend>0):
threading.Timer(delayToSend, elaborateAllChangedFiles).start()
except Exception as inst:
logger.error("main Error - " + timeStamp())
logger.error(type(inst)) # the exception instance
logger.error(inst.args) # arguments stored in .args
logger.error(inst) # __str__ allows args to be printed directly