-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaws_backup.py
371 lines (338 loc) · 11.1 KB
/
aws_backup.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
import boto3
import botocore
import os
import os.path
from os import path
import sys
import argparse
import hashlib
from pathlib import Path
from settings import cfg
import requests
###################################################################################################
# Program variables
###################################################################################################
if(cfg['CWD']=='DEFAULT'):
cwd = os.getcwd()
else:
cwd = cfg['CWD']
if(cfg['DOWNLOAD']=='DEFAULT'):
dld = os.getcwd()
else:
dld = cfg['DOWNLOAD']
URL_account = 'https://h1pkbtk97l.execute-api.ap-northeast-1.amazonaws.com/default/passcode1' # A bucket containing a dictionary of accounts and passcodes.
URL_backup = 'https://i7jaxa2cp9.execute-api.ap-northeast-1.amazonaws.com/default/checkbackup' # A bucket containing a dictionary of backup timestamps.
###################################################################################################
# End user interactions
###################################################################################################
# The User's actual bucket for storage.
def add_update_new_device(arg_list):
last_update = '0000'
passcode = arg_list[3]
aws_access_key_id = arg_list[0]
aws_secret_access_key = arg_list[1]
bucket = arg_list[2]
s3_c = boto3.client(
service_name = 's3',
aws_access_key_id = aws_access_key_id,
aws_secret_access_key = aws_secret_access_key,
#aws_session_token=SESSION_TOKEN,
)
s3_r = boto3.resource(
service_name = 's3',
aws_access_key_id = aws_access_key_id,
aws_secret_access_key = aws_secret_access_key,
#aws_session_token=SESSION_TOKEN,
)
if_new_user('user', aws_access_key_id, aws_secret_access_key)
PARAMS_a = {'passcode': passcode}
r_a = requests.get(url = URL_account, params = PARAMS_a)
account = r_a.json()
print('Account name: ', account)
PARAMS_b = {'passcode': passcode, 'account': account}
r_b = requests.get(url = URL_backup, params = PARAMS_b)
if_backup = r_b.json()
print('Last backup timestamp: ', if_backup)
#if_backup = read_object(proxy_r, cfg['B_BUCKET'], account+".txt")
if(if_backup>last_update):
sync(s3_c, s3_r, bucket)
print('Setting User data...')
set_user_timestamp('user', if_backup)
set_user_account('user', account)
set_user_bucket('user', bucket)
set_user_passcode('user', passcode)
else:
print('Already up to date...')
def update_device():
timestamp = get_user_timestamp('user')
account = get_user_account('user')
bucket = get_user_bucket('user')
id_key = get_user_cred('user')
passcode = get_user_passcode('user')
s3_c = set_client(id_key)
s3_r = set_resource(id_key)
print('Account: ', account)
PARAMS_b = {'passcode': passcode, 'account': account}
r_b = requests.get(url = URL_backup, params = PARAMS_b)
if_backup = r_b.json()
if(if_backup>timestamp):
sync(s3_c, s3_r, bucket)
set_user_timestamp('user', if_backup)
else:
print('Already up to date...')
###################################################################################################
# s3 User Managment
###################################################################################################
def if_new_user(name, ID, key):
old_file_path = save_old_dir()
_path = cwd+"\\"+name
if not path.exists(_path):
os.makedirs(_path)
os.chdir(_path)
make_cred_file(ID, key)
reset_working_dir(old_file_path)
def make_cred_file(ID, key):
f = open("passcode", 'w')
f.close()
f = open("bucket", 'w')
f.close()
f = open("timestamp", 'w')
f.close()
f = open("account", 'w')
f.close()
f = open("credentials", "w")
f.write("[default]\naws_access_key_id = "+ID+"\naws_secret_access_key = "+key)
f.close()
f = open("config", "w")
f.write("[default]\nregion = us-west-2\noutput = json")
f.close()
# String input - user id
def get_user_cred(input):
old_file_path = save_old_dir()
os.chdir(cwd+'\\'+input)
f = open('credentials', 'r')
id_key = []
if f.mode=='r':
contents = f.readlines()
for s in contents:
if 'aws_access_key_id' in s or 'aws_secret_access_key' in s:
a = s.split(" = ")
#print(a)
a = a[1]
a = a.replace("\n", "")
id_key.append(a)
print("Resetting working dir...")
reset_working_dir(old_file_path)
return id_key
def get_user_timestamp(input):
old_file_path = save_old_dir()
os.chdir(cwd+'\\'+input)
with open('timestamp', 'r') as f:
timestamp = f.read()
reset_working_dir(old_file_path)
return timestamp
def set_user_timestamp(input, timestamp):
old_file_path = save_old_dir()
os.chdir(cwd+'\\'+input)
with open('timestamp', 'w') as f:
f.write(timestamp)
reset_working_dir(old_file_path)
def get_user_account(input):
old_file_path = save_old_dir()
os.chdir(cwd+'\\'+input)
with open('account', 'r') as f:
account = f.read()
reset_working_dir(old_file_path)
return account
def set_user_account(input, account):
old_file_path = save_old_dir()
os.chdir(cwd+'\\'+input)
with open('account', 'w') as f:
f.write(account)
reset_working_dir(old_file_path)
def get_user_bucket(input):
old_file_path = save_old_dir()
os.chdir(cwd+'\\'+input)
with open('bucket', 'r') as f:
bucket = f.read()
reset_working_dir(old_file_path)
return bucket
def set_user_bucket(input, bucket):
old_file_path = save_old_dir()
os.chdir(cwd+'\\'+input)
with open('bucket', 'w') as f:
f.write(bucket)
reset_working_dir(old_file_path)
def get_user_passcode(input):
old_file_path = save_old_dir()
os.chdir(cwd+'\\'+input)
with open('passcode', 'r') as f:
passcode = f.read()
print("Resetting working dir...")
reset_working_dir(old_file_path)
return passcode
def set_user_passcode(input, passcode):
old_file_path = save_old_dir()
os.chdir(cwd+'\\'+input)
with open('passcode', 'w') as f:
f.write(passcode)
print("Resetting working dir...")
reset_working_dir(old_file_path)
###################################################################################################
# s3 Utility
###################################################################################################
def set_client(id_key):
client = boto3.client(
's3',
aws_access_key_id = id_key[0],
aws_secret_access_key = id_key[1],
#aws_session_token=SESSION_TOKEN,
)
return client
def set_resource(id_key):
resource = boto3.resource(
's3',
aws_access_key_id = id_key[0],
aws_secret_access_key = id_key[1],
#aws_session_token=SESSION_TOKEN,
)
return resource
# input from save_old_dir
def reset_working_dir(input):
os.chdir(input)
#print(os.getcwd())
def get_cwd():
return os.getcwd()
def get_bucket_list():
for bucket in s3.buckets.all():
print(bucket.name)
def read_object(s3, bucket, key):
obj = s3.Object(bucket, key)
obj = obj.get()['Body'].read().decode('utf-8')
#print(obj)
return obj
# String input
def save_old_dir():
return os.getcwd()
###################################################################################################
# Sync files from root directory
###################################################################################################
def sync(s3_c, s3_r, bucket):
print("Syncing...")
old_file_path = save_old_dir()
print('old path', old_file_path)
# traverse root directory, and list directories as dirs and files as files
#bucket = s3.Bucket(bucket)
# THIS SHOULD BE cwd+"\\.."
os.chdir('..')
cwd_temp = os.getcwd()
for root, dirs, files in os.walk(cwd_temp):
print('root', root)
print('dirs', dirs)
print('files', files)
path = root.split(os.sep)
print((len(path) - 1) * '---', os.path.basename(root))
for file in files:
print(root)
if(old_file_path not in root):
#print(len(path) * '---', file)
print(path,file)
file_path = root
f = open(root+"\\"+file, 'r')
with open(root+"\\"+file, 'r'):
#print(path+file)
contents = f.read()
print('Checking if '+file+' has been modified...')
check_and_update(s3_c, s3_r, file, contents, bucket, file_path)
print("Synced!")
reset_working_dir(old_file_path)
# md5 checksum has 5GB file limit.
def check_and_update(s3_c, s3_r, file, contents, bucket, file_path):
#print('bucket', bucket)
md5_file = hashlib.md5(contents.encode()).hexdigest()
print('md5_file', md5_file)
if(search_bucket(s3_r, file, bucket)):
md5_bucket = get_bucket_md5(s3_c, file, bucket)
print('md5_bucket', md5_bucket)
if(md5_file!=md5_bucket):
print('Updating files...')
print('File_path:', file_path)
upload(s3_r, file, bucket, file_path)
else:
print('File not modified!')
else:
print('Updating files...')
print('File_path', file_path)
upload(s3_r, file, bucket, file_path)
def get_bucket_md5(s3_c, file, bucket):
key = s3_c.head_object(Bucket = bucket, Key = file)
#print(key)
s3_etag = key['ETag'].strip('"').strip("'")
return s3_etag
def search_bucket(s3_r, file, bucket):
try:
s3_r.Object(bucket, file).load()
except botocore.exceptions.ClientError as e:
if e.response['Error']['Code'] == "404":
# The object does not exist.
print("404 Not found...")
else:
# Something else has gone wrong.
print("Well, shit.")
raise
return False
else:
# The object does exist.
return True
###################################################################################################
# File Transfering
###################################################################################################
# Note resource not client..
def upload(s3, filename, bucket, file_path):
old_file_path = save_old_dir()
os.chdir(file_path)
print("Uploading...")
s3.meta.client.upload_file(filename, bucket, filename)
print("Done uploading!")
reset_working_dir(old_file_path)
# Uses resource...
def download(s3, filename, bucket):
old_file_path = save_old_dir()
download_dir = dld
reset_working_dir(download_dir)
try:
print('Downloading...')
s3.Bucket(bucket).download_file(filename, filename)
print('File downloaded!')
except botocore.exceptions.ClientError as e:
if e.response['Error']['Code'] == "404":
print("The object does not exist.")
else:
raise
reset_working_dir(old_file_path)
###################################################################################################
# Pending
###################################################################################################
def bucket_details(s3, bucket):
pass
def list_buckets():
pass
def make_bucket():
pass
def parse_id_key():
pass
###################################################################################################
# Main Program
###################################################################################################
def parse_args():
parser = argparse.ArgumentParser()
# Set default profile for uploading? Last used account...
parser.add_argument('--user', dest='user',
default='default', type=str)
args = parser.parse_args()
return args
"""
if __name__ == '__main__':
#add_update_new_device()
#update_device()
"""