forked from tsileo/bakthat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bakthat.py
452 lines (353 loc) · 14.1 KB
/
bakthat.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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
import tarfile
import tempfile
import os
import sys
import ConfigParser
from datetime import datetime
from getpass import getpass
import logging
import shelve
import json
import boto
from boto.s3.key import Key
import boto.glacier
import boto.glacier.layer2
from boto.glacier.exceptions import UnexpectedHTTPResponseError
from boto.exception import S3ResponseError
from beefish import decrypt, encrypt_file
import aaargh
DEFAULT_LOCATION = "us-east-1"
app = aaargh.App(description="Compress, encrypt and upload files directly to Amazon S3/Glacier.")
log = logging.getLogger(__name__)
if not log.handlers:
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s: %(message)s')
config = ConfigParser.SafeConfigParser()
config.read(os.path.expanduser("~/.bakthat.conf"))
class glacier_shelve(object):
"""
Context manager for shelve
"""
def __enter__(self):
self.shelve = shelve.open(os.path.expanduser("~/.bakthat.db"))
return self.shelve
def __exit__(self, exc_type, exc_value, traceback):
self.shelve.close()
class S3Backend:
"""
Backend to handle S3 upload/download
"""
def __init__(self, conf):
if conf is None:
try:
access_key = config.get("aws", "access_key")
secret_key = config.get("aws", "secret_key")
bucket = config.get("aws", "s3_bucket")
try:
region_name = config.get("aws", "region_name")
except ConfigParser.NoOptionError:
region_name = DEFAULT_LOCATION
except ConfigParser.NoOptionError:
log.error("Configuration file not available.")
log.info("Use 'bakthat configure' to create one.")
return
else:
access_key = conf.get("access_key")
secret_key = conf.get("secret_key")
bucket = conf.get("bucket")
region_name = conf.get("region_name", DEFAULT_LOCATION)
con = boto.connect_s3(access_key, secret_key)
if region_name == DEFAULT_LOCATION:
region_name = ""
try:
self.bucket = con.get_bucket(bucket)
except S3ResponseError, e:
if e.code == "NoSuchBucket":
self.bucket = con.create_bucket(bucket, location=region_name)
else:
raise e
self.container = "S3 Bucket: {}".format(bucket)
def download(self, keyname):
k = Key(self.bucket)
k.key = keyname
encrypted_out = tempfile.TemporaryFile()
k.get_contents_to_file(encrypted_out)
encrypted_out.seek(0)
return encrypted_out
def cb(self, complete, total):
percent = int(complete * 100.0 / total)
log.info("Upload completion: {}%".format(percent))
def upload(self, keyname, filename, cb=True):
k = Key(self.bucket)
k.key = keyname
upload_kwargs = {}
if cb:
upload_kwargs = dict(cb=self.cb, num_cb=10)
k.set_contents_from_filename(filename, **upload_kwargs)
k.set_acl("private")
def ls(self):
return [key.name for key in self.bucket.get_all_keys()]
def delete(self, keyname):
k = Key(self.bucket)
k.key = keyname
self.bucket.delete_key(k)
class GlacierBackend:
"""
Backend to handle Glacier upload/download
"""
def __init__(self, conf):
if conf is None:
try:
access_key = config.get("aws", "access_key")
secret_key = config.get("aws", "secret_key")
vault_name = config.get("aws", "glacier_vault")
try:
region_name = config.get("aws", "region_name")
except ConfigParser.NoOptionError:
region_name = DEFAULT_LOCATION
except ConfigParser.NoOptionError:
log.error("Configuration file not available.")
log.info("Use 'bakthat configure' to create one.")
return
else:
access_key = conf.get("access_key")
secret_key = conf.get("secret_key")
vault_name = conf.get("vault")
region_name = conf.get("region_name", DEFAULT_LOCATION)
con = boto.connect_glacier(aws_access_key_id=access_key,
aws_secret_access_key=secret_key, region_name=region_name)
self.conf = conf
self.vault = con.create_vault(vault_name)
self.backup_key = "bakthat_glacier_inventory"
self.container = "Glacier vault: {}".format(vault_name)
def backup_inventory(self):
"""
Backup the local inventory from shelve as a json string to S3
"""
with glacier_shelve() as d:
if not d.has_key("archives"):
d["archives"] = dict()
archives = d["archives"]
s3_bucket = S3Backend(self.conf).bucket
k = Key(s3_bucket)
k.key = self.backup_key
k.set_contents_from_string(json.dumps(archives))
k.set_acl("private")
def restore_inventory(self):
"""
Restore inventory from S3 to local shelve
"""
s3_bucket = S3Backend(self.conf).bucket
k = Key(s3_bucket)
k.key = self.backup_key
loaded_archives = json.loads(k.get_contents_as_string())
with glacier_shelve() as d:
if not d.has_key("archives"):
d["archives"] = dict()
archives = loaded_archives
d["archives"] = archives
def upload(self, keyname, filename):
archive_id = self.vault.concurrent_create_archive_from_file(filename) # , keyname => second argument not on pypi yet
# Storing the filename => archive_id data.
with glacier_shelve() as d:
if not d.has_key("archives"):
d["archives"] = dict()
archives = d["archives"]
archives[keyname] = archive_id
d["archives"] = archives
self.backup_inventory()
def get_archive_id(self, filename):
"""
Get the archive_id corresponding to the filename
"""
with glacier_shelve() as d:
if not d.has_key("archives"):
d["archives"] = dict()
archives = d["archives"]
if filename in archives:
return archives[filename]
return None
def download(self, keyname):
"""
Initiate a Job, check its status, and download the archive if it's completed.
"""
archive_id = self.get_archive_id(keyname)
if not archive_id:
return
with glacier_shelve() as d:
if not d.has_key("jobs"):
d["jobs"] = dict()
jobs = d["jobs"]
job = None
if keyname in jobs:
# The job is already in shelve
job_id = jobs[keyname]
try:
job = self.vault.get_job(job_id)
except UnexpectedHTTPResponseError: # Return a 404 if the job is no more available
del job[keyname]
if not job:
# Job initialization
job = self.vault.retrieve_archive(archive_id)
jobs[keyname] = job.id
job_id = job.id
# Commiting changes in shelve
d["jobs"] = jobs
log.info("Job {action}: {status_code} ({creation_date}/{completion_date})".format(**job.__dict__))
if job.completed:
log.info("Downloading...")
encrypted_out = tempfile.TemporaryFile()
encrypted_out.write(job.get_output().read())
encrypted_out.seek(0)
return encrypted_out
else:
log.info("Not completed yet")
return None
def retrieve_inventory(self, jobid):
"""
Initiate a job to retrieve Galcier inventory or output inventory
"""
if jobid is None:
return self.vault.retrieve_inventory(sns_topic=None, description="Bakthat inventory job")
else:
return self.vault.get_job(jobid)
def retrieve_archive(self, archive_id, jobid):
"""
Initiate a job to retrieve Galcier archive or download archive
"""
if jobid is None:
return self.vault.retrieve_archive(archive_id, sns_topic=None, description='Retrieval job')
else:
return self.vault.get_job(jobid)
def ls(self):
with glacier_shelve() as d:
if not d.has_key("archives"):
d["archives"] = dict()
return d["archives"].keys()
def delete(self, keyname):
archive_id = self.get_archive_id(keyname)
if archive_id:
self.vault.delete_archive(archive_id)
with glacier_shelve() as d:
archives = d["archives"]
if keyname in archives:
del archives[keyname]
d["archives"] = archives
self.backup_inventory()
storage_backends = dict(s3=S3Backend, glacier=GlacierBackend)
@app.cmd(help="Backup a file or a directory, backup the current directory if no arg is provided.")
@app.cmd_arg('-f', '--filename', type=str, default=os.getcwd())
@app.cmd_arg('-d', '--destination', type=str, default="s3", help="s3|glacier")
@app.cmd_arg('-s', '--description', type=str, default=None)
def backup(filename, destination="glacier", description=None, **kwargs):
conf = kwargs.get("conf", None)
storage_backend = storage_backends[destination](conf)
log.info("Backing up " + filename)
arcname = filename.strip('/').split('/')[-1]
stored_filename = arcname + datetime.now().strftime("%Y%m%d%H%M%S") + ".tgz"
password = kwargs.get("password")
if not password:
password = getpass("Password (blank to disable encryption): ")
if password:
password2 = getpass("Password confirmation: ")
if password != password2:
log.error("Password confirmation doesn't match")
return
log.info("Compressing...")
with tempfile.NamedTemporaryFile(delete=False) as out:
with tarfile.open(fileobj=out, mode="w:gz") as tar:
tar.add(filename, arcname=arcname)
outname = out.name
if password:
log.info("Encrypting...")
encrypted_out = tempfile.NamedTemporaryFile(delete=False)
encrypt_file(outname, encrypted_out.name, password)
stored_filename += ".enc"
os.remove(outname) # remove non-encrypted tmp file
outname = encrypted_out.name
log.info("Uploading...")
storage_backend.upload(stored_filename, outname)
os.remove(outname)
@app.cmd(help="Set AWS S3/Glacier credentials.")
def configure():
config.add_section("aws")
config.set("aws", "access_key", raw_input("AWS Access Key: "))
config.set("aws", "secret_key", raw_input("AWS Secret Key: "))
config.set("aws", "s3_bucket", raw_input("S3 Bucket Name: "))
config.set("aws", "glacier_vault", raw_input("Glacier Vault Name: "))
region_name = raw_input("Region Name (" + DEFAULT_LOCATION + "): ")
if not region_name:
region_name = DEFAULT_LOCATION
config.set("aws", "region_name", region_name)
config.write(open(os.path.expanduser("~/.bakthat.conf"), "w"))
log.info("Config written in %s" % os.path.expanduser("~/.bakthat.conf"))
@app.cmd(help="Restore backup in the current directory.")
@app.cmd_arg('-f', '--filename', type=str, default="")
@app.cmd_arg('-d', '--destination', type=str, default="s3", help="s3|glacier")
def restore(filename, destination="s3", **kwargs):
conf = kwargs.get("conf", None)
storage_backend = storage_backends[destination](conf)
if not filename:
log.error("No file to restore, use -f to specify one.")
return
keys = [name for name in storage_backend.ls() if name.startswith(filename)]
if not keys:
log.error("No file matched.")
return
key_name = sorted(keys, reverse=True)[0]
log.info("Restoring " + key_name)
# Asking password before actually download to avoid waiting
if key_name and key_name.endswith(".enc"):
password = kwargs.get("password")
if not password:
password = getpass()
log.info("Downloading...")
out = storage_backend.download(key_name)
if out and key_name.endswith(".enc"):
log.info("Decrypting...")
decrypted_out = tempfile.TemporaryFile()
decrypt(out, decrypted_out, password)
out = decrypted_out
if out:
log.info("Uncompressing...")
out.seek(0)
tar = tarfile.open(fileobj=out)
tar.extractall()
tar.close()
@app.cmd(help="Delete a backup.")
@app.cmd_arg('-f', '--filename', type=str, default="")
@app.cmd_arg('-d', '--destination', type=str, default="s3", help="s3|glacier")
def delete(filename, destination="s3", **kwargs):
conf = kwargs.get("conf", None)
storage_backend = storage_backends[destination](conf)
if not filename:
log.error("No file to delete, use -f to specify one.")
return
keys = [name for name in storage_backend.ls() if name.startswith(filename)]
if not keys:
log.error("No file matched.")
return
key_name = sorted(keys, reverse=True)[0]
log.info("Deleting " + key_name)
storage_backend.delete(key_name)
@app.cmd(help="List stored backups.")
@app.cmd_arg('-d', '--destination', type=str, default="s3", help="s3|glacier")
def ls(destination="s3", **kwargs):
conf = kwargs.get("conf", None)
storage_backend = storage_backends[destination](conf)
log.info(storage_backend.container)
for filename in storage_backend.ls():
log.info(filename)
@app.cmd(help="Backup Glacier inventory to S3")
def backup_glacier_inventory(**kwargs):
conf = kwargs.get("conf", None)
glacier_backend = GlacierBackend(conf)
glacier_backend.backup_inventory()
@app.cmd(help="Restore Glacier inventory from S3")
def restore_glacier_inventory(**kwargs):
conf = kwargs.get("conf", None)
glacier_backend = GlacierBackend(conf)
glacier_backend.restore_inventory()
def main():
app.run()
if __name__ == '__main__':
main()