-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths3sync.py
More file actions
330 lines (260 loc) · 11.5 KB
/
s3sync.py
File metadata and controls
330 lines (260 loc) · 11.5 KB
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
# Copyright 2011-2015 S J Consulting Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://www.apache.org/licenses/LICENSE-2.0.html
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
import argparse
#from opcode import op
import boto
import collections
import pprint
import os
import time
#import datetime
#import dateutil.parser
import hashlib
__version__ = "1.1.3"
RULER = "--------------------------------------------------------------------------------"
log = []
FileTuple = collections.namedtuple('filetuple', 'etag size')
def calculatemd5(files, path):
if not path in files:
return
if files[path].etag:
return files[path].etag
file = files['__prefix__'] + path
if not os.path.isfile(file):
return;
return hashlib.md5(open(file,'rb').read()).hexdigest()
def cleanpath(path):
if not path.endswith('/'):
return path + '/'
return path
def file_only_log_event(text):
global log
clock = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())
log.append("%s %s" % (clock, text))
def console_only_log_event(text):
print (text)
def log_event(text):
file_only_log_event(text)
console_only_log_event(text)
def commit_log(logfilename, position = None):
if logfilename == '' or logfilename == None : return
if position == 'top':
if os.path.exists(logfilename):
tempfilename = os.path.dirname(logfilename) + os.pathsep + "log.tmp"
tempfile = open(tempfilename, 'w')
for text in log:
tempfile.write(text + os.linesep)
logfile = open(logfilename, 'r')
for text in logfile:
tempfile.write(text)
logfile.close()
tempfile.close()
os.remove(logfilename)
os.rename(tempfilename, logfilename)
return
logfile = open(logfilename, 'a')
for text in log:
logfile.write(text + os.linesep)
logfile.close()
def cloud_with_prefix(bucket, cloud = None):
if cloud == None:
return
for obj in bucket.list(cloud['__prefix__']):
try:
key = str(obj.name)[len(cloud['__prefix__']):]
except:
pass
else:
if not key.endswith('/'):
data = bucket.get_key(obj.name)
cloud[key] = FileTuple(size=data.size, etag=data.etag[1:-1])
def local_with_prefix(local = None, folder = ''):
if local == None:
return
_folder = cleanpath(folder)
path = cleanpath(local['__prefix__'] + folder)
if not os.path.isdir(path):
return
for obj in os.listdir(path):
if not obj.startswith('_') and obj != '.DS_Store' and obj != 'Thumbs.db':
_file = path + obj
if os.path.isdir(_file):
local_with_prefix(local, _folder + obj)
else:
if os.path.isfile(_file):
local[_folder + obj] = FileTuple(size=os.path.getsize(_file), etag='')
def generate_actions(source, destination, direction, actions = None, maxactions = 0, md5 = False, delete = False):
if source == None:
return
if destination == None:
return
if actions == None:
actions = []
ActionTuple = collections.namedtuple('actiontuple','operation object param reason')
for key, obj in source.iteritems():
if key != '__prefix__':
if key in destination:
if obj.size != destination[key].size:
actions.append(ActionTuple(operation=direction, object=key, param=None, reason='different size'))
else:
if md5:
if calculatemd5(source, key) != calculatemd5(destination, key):
#hash = hashlib.md5(open(localpath + key,'rb').read()).hexdigest()
#if destination[key].etag[1:-1] != hash:
actions.append(ActionTuple(operation=direction, object=key, param=None, reason='different md5'))
del(destination[key])
else:
actions.append(ActionTuple(operation=direction, object=key, param=None, reason='missing'))
if len(actions) >= maxactions:
return
if delete:
for key, obj in destination.iteritems():
if key != '__prefix__':
if direction == 'upload':
actions.append(ActionTuple(operation='deletecloud', object=key, param=None, reason='no local file'))
else:
actions.append(ActionTuple(operation='deletelocal', object=key, param=None, reason='no cloud file'))
if len(actions) >= maxactions:
return
def perform_actions(bucket, actions, localpath, cloudpath, metrics = None, dryrun = False, maxretries = 3, acl = None):
if bucket == None or actions == None or localpath == '' or cloudpath == '':
return
if metrics == None:
metrics ={'errors': 0, 'uploads': 0, 'downloads': 0, 'deletes': 0}
for action in actions:
cloud = cloudpath + action.object
local = localpath + action.object
localparent = os.path.dirname(local)
reason = " (%s) " % action.reason
retries = 0
if action.operation == 'upload':
operation = action.operation
while retries < maxretries:
try:
log_event(operation + reason + local)
if not dryrun:
obj = boto.s3.key.Key(bucket)
obj.key = cloud
obj.set_contents_from_filename(local)
if(acl != None):
obj.set_acl(acl)
break
except:
operation = 're-' + action.operation
retries = retries + 1
if retries < maxretries:
metrics['uploads'] = metrics['uploads'] + 1
else:
metrics['errors'] = metrics['errors'] + 1
elif action.operation == 'download':
operation = action.operation
if os.path.exists(local):
os.remove(local)
else:
if not os.path.isdir(localparent):
os.makedirs(localparent)
while retries < maxretries:
try:
log_event(operation + reason + local)
if not dryrun:
obj = boto.s3.key.Key(bucket)
obj.key = cloud
obj.get_contents_to_filename(local)
break
except:
operation = 're-' + action.operation
retries = retries + 1
if retries < maxretries:
metrics['downloads'] = metrics['downloads'] + 1
else:
metrics['errors'] = metrics['errors'] + 1
elif action.operation == 'deletecloud':
try:
log_event(action.operation + reason + cloud)
if not dryrun:
obj = boto.s3.key.Key(bucket)
obj.key = cloud
bucket.delete_key(obj)
metrics['deletes'] = metrics['deletes'] + 1
except:
metrics['errors'] = metrics['errors'] + 1
elif action.operation == 'deletelocal':
try:
log_event(action.operation + reason + local)
if not dryrun:
os.remove(local)
metrics['deletes'] = metrics['deletes'] + 1
except:
metrics['errors'] = metrics['errors'] + 1
console_only_log_event("Amazon S3 Synchroniser %s" % __version__)
console_only_log_event("Copyright 2014 S. J. Consulting Ltd. All rights reserved")
parser = argparse.ArgumentParser()
parser.add_argument('-k','--awsaccesskeyid', help='AWS Access Key ID', required=True)
parser.add_argument('-s','--awssecretaccesskey', help='AWS Secret Access Key', required=True)
parser.add_argument('-b','--bucketname', help='AWS Bucket Name', required=True)
parser.add_argument('-c','--cloudpath', help='AWS cloud path', required=True)
parser.add_argument('-l','--localpath', help='local path', required=True)
parser.add_argument('-d','--direction', help='transfer direction (upload, download)', required=True)
parser.add_argument('--logfile', help='log file name')
parser.add_argument('--maxactions', help='maximum number of actions', default=0, type=int)
parser.add_argument('--md5', action='store_true', default=False, help='enable md5 hash file checking')
parser.add_argument('--dryrun',action='store_true', default=False, help='enable dryrun')
parser.add_argument('--delete', action='store_true', default=False, help='enable file deletion')
parser.add_argument('-a','--acl', help="default acl")
options = parser.parse_args()
console_only_log_event("awsaccesskeyid=%s" % options.awsaccesskeyid)
console_only_log_event("awssectaccesskey=%s" % options.awssecretaccesskey)
console_only_log_event("bucketname=%s" % options.bucketname)
console_only_log_event("localpath=%s" % options.localpath)
console_only_log_event("cloudpath=%s" % options.cloudpath)
console_only_log_event("direction=%s" % options.direction)
console_only_log_event("logfile=%s" % options.logfile)
console_only_log_event("maxactions=%s" % options.maxactions)
console_only_log_event("md5=%s" % options.md5)
console_only_log_event("dryrun=%s" % options.dryrun)
console_only_log_event("delete=%s" % options.delete)
console_only_log_event("acl=%s" % options.acl)
conn = boto.connect_s3(options.awsaccesskeyid, options.awssecretaccesskey)
bucket = conn.get_bucket(options.bucketname)
if bucket == None:
console_only_log_event("Error: invalid bucket %s" % options.bucketname)
conn.close()
exit(0)
if options.acl != None:
if not options.acl in boto.gs.acl.CannedACLStrings:
console_only_log_event("Error: invalid acl setting %s" % options.acl)
conn.close()
exit(0)
metrics ={'errors': 0, 'uploads': 0, 'downloads': 0, 'deletes': 0}
cloudfiles = {}
cloudfiles['__prefix__'] = options.cloudpath
cloud_with_prefix(bucket,cloudfiles)
#print "cloud"
#pprint.pprint(cloudfiles)
localfiles = {}
localfiles['__prefix__'] = options.localpath
local_with_prefix(localfiles)
#print "local"
#pprint.pprint(localfiles)
actions = []
if options.direction == 'upload':
generate_actions(localfiles, cloudfiles, 'upload', actions, options.maxactions, options.md5, delete=options.delete)
else:
generate_actions(cloudfiles , localfiles, 'download', actions, options.maxactions, options.md5, delete=options.delete)
#print "actions"
#pprint.pprint( actions)
perform_actions(bucket,actions,options.localpath, options.cloudpath, metrics, dryrun=options.dryrun, acl=options.acl)
conn.close()
log_event("%s uploads, %s downloads, %s deletes, %s errors" % (metrics['uploads'], metrics['downloads'], metrics['deletes'], metrics['errors']))
log_event(RULER)
commit_log(options.logfile, 'top')