-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSFTPTransfer.py
135 lines (90 loc) · 3.21 KB
/
SFTPTransfer.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
import pysftp
import os
import shutil
import datetime
from log import log
local_dir = 'local'
class estConnection():
def __init__(self,hostname="",username="",password="",port=22):
self.host = hostname
self.username = username
self.password = password
self.port = port
def openConn(self):
try:
conn = pysftp.Connection(host=self.host, username=self.username, password=self.password,port=self.port)
log.info('Connection: ' + self.host + ':' + str(self.port) + ' established successfully')
return conn
except:
log.info('Error on connection: ' + self.host + ':' + str(self.port))
def closeConn(self):
self.close()
def closeConnections(conn):
#close connections
try:
conn.close()
log.info('Connection closed')
except:
log.info('Connection close failed')
def syncProcess():
src_file_names = []
tar_file_names = []
#open source connection
src_conn_set = estConnection("localhost","sftp_usr","Password1")
src_conn = src_conn_set.openConn()
#open target connection
tar_conn_set = estConnection("localhost","sftp_usr2","Password1",33)
tar_conn = tar_conn_set.openConn()
# Get the directory and file listing
data_src = src_conn.listdir()
data_tar = tar_conn.listdir()
# Add source files to sync list
for i in data_src:
if src_conn.isdir(i):
src_file_names.append(i)
# Add target to sync list
for i in data_tar:
if tar_conn.isdir(i):
tar_file_names.append(i)
#compare
copyList = list(set(src_file_names) - set(tar_file_names))
copyListLen = len(copyList)
if copyListLen > 0:
log.info(str(copyListLen) + ' directories to be copied...')
for copy_dir_name in copyList:
if src_conn.isdir(copy_dir_name):
#remove local dir
checkLocal_content(copy_dir_name)
src_conn.get_r(copy_dir_name, 'local')
tar_conn.put_r('local','')
log.info(copy_dir_name + ' downloaded')
else:
log.info('All folders in sync...nothing to do')
#close connections
log.info('Source connection status: ' + src_conn_set.host + ':' + str(src_conn_set.port))
closeConnections(src_conn)
log.info('Target connection status: ' + tar_conn_set.host + ':' + str(tar_conn_set.port))
closeConnections(tar_conn)
def checkLocal_main():
global local_dir
if not os.path.exists(local_dir):
os.makedirs(local_dir)
def checkLocal_content(content_name):
if os.path.exists(local_dir + "/" + content_name):
log.info('Local folder does not exist, creating...')
shutil.rmtree(local_dir + '/' + content_name)
log.info('Local folder created...')
def logShutdown(status):
if status == 1:
log.info('Application exited successfuly @ ' + str(datetime.datetime.now()))
else:
log.info('Application exited with failure @ ' + str(datetime.datetime.now()))
log.info('\n')
try:
#check and create local staging directory
checkLocal_main()
#initiate check and copy process
syncProcess()
logShutdown(1)
except:
logShutdown(0)