forked from JeremyGrosser/tablesnap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tableslurp
executable file
·287 lines (252 loc) · 10.4 KB
/
tableslurp
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
#!/usr/bin/env python
#
# -*- mode:python; sh-basic-offset:4; indent-tabs-mode:nil; coding:utf-8 -*-
# vim:set tabstop=4 softtabstop=4 expandtab shiftwidth=4 fileencoding=utf-8:
#
# Copyright (c) 2012, Jorge A Gallegos <[email protected]>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
import argparse
import boto
from dateutil import parser
import grp
import json
import logging
import threading
import os
import pwd
import socket
import sys
from Queue import Queue
log = logging.getLogger('tableslurp')
stderr = logging.StreamHandler()
stderr.setFormatter(logging.Formatter(
'%(name)s [%(asctime)s] %(levelname)s %(message)s'))
log.addHandler(stderr)
if os.environ.get('TDEBUG', False):
log.setLevel(logging.DEBUG)
else:
log.setLevel(logging.INFO)
class DownloadCounter(object):
filename = None
attemptcount = 0
def __init__(self, filename):
self.filename = filename
def increment(self):
self.attemptcount += 1
class DownloadHandler(object):
key = None
secret = None
bucket_name = None
owner = None
group = None
preserve = False
target = None
origin = None
prefix = None
force = False
name = socket.getfqdn()
fileset = []
queue = Queue()
num_threads = 4
threads = {}
def __init__(self, args):
self.target = args.target[0]
self.origin = args.origin[0]
self.preserve = args.preserve
self.key = args.aws_key
self.secret = args.aws_secret
self.bucket_name = args.bucket[0]
self.num_threads = args.threads
self.force = args.force
if args.name:
self.name = args.name
self.prefix = '%s:%s' % (self.name, self.origin)
# It may be a bit sub-optimal, but I rather fail sooner than later
(owner, group) = self._build_file_set(args.file)
if not self.preserve:
owner = args.owner
group = args.group
try:
self.owner = pwd.getpwnam(owner).pw_uid
self.group = grp.getgrnam(group).gr_gid
except Exception as e:
log.error(e)
raise OSError('User/Group pair %s:%s does not exist' %
(owner, group,))
def _get_bucket(self):
# unsure if boto is thread-safe, will reconnect every time
log.debug('Connecting to s3')
conn = boto.connect_s3(self.key, self.secret)
bucket = conn.get_bucket(self.bucket_name)
log.debug('Connected to s3')
return bucket
def _build_file_set(self, target_file=None):
log.info('Building fileset')
key = None
# If you want to restore a file-set in particular
bucket = self._get_bucket()
if target_file:
key = bucket.get_key('%s/%s-listdir.json' %
(self.prefix, target_file))
# Otherwise try to fetch the most recent one
else:
keys = [_ for _ in bucket.get_all_keys(prefix='%s/' %
(self.prefix,)) if _.name.endswith('-listdir.json')]
if keys:
keys.sort(key=lambda l: parser.parse(l.last_modified))
key = keys.pop()
if not key:
raise LookupError('Cannot find anything to restore from %s:%s/%s' %
(bucket.name, self.prefix, target_file or ''))
json_data = json.loads(key.get_contents_as_string())
self.fileset = json_data[self.origin]
log.info('Fileset contains %d files to download' % (len(self.fileset)))
k = bucket.get_key('%s/%s' % (self.prefix, self.fileset[0]))
# The librato branch introduced this
meta = k.get_metadata('stat')
log.debug('Metadata is %s' % (meta,))
owner = None
group = None
if meta:
try:
json_data = json.loads(meta)
owner = json_data['user']
group = json_data['group']
except TypeError as te:
log.debug(te)
log.warning('Could not parse stat metadata for %s' % (k.name,))
except KeyError as ke:
log.debug(ke)
log.warning('Incomplete stat metadata for %s, will ignore' %
(k.name,))
return (owner, group)
def _test_permissions(self):
log.info('Will now try to test writing to the target dir %s' %
(self.target,))
try:
if os.path.isdir(self.target) == False:
log.debug('Creating temp file in %s' % (self.target,))
os.makedirs(self.target)
log.debug('Changing owner:group for %s to %s:%s' %
(self.target, self.owner, self.group,))
os.chown(self.target, self.owner, self.group)
except Exception as e:
log.debug(e)
log.exception('%s exists' % (self.target,))
log.info('Will write to %s' % (self.target,))
def _worker(self, idx, queue):
log.info('Thread #%d processing items' % (idx, ))
bucket = self._get_bucket()
while queue.empty() == False:
queueddownload = queue.get()
fname = queueddownload.filename
keypath = '%s/%s' % (self.prefix, fname,)
destfile = os.path.join(self.target, os.path.basename(fname))
log.debug('Checking if we need to download %s to %s' %
(keypath, destfile,))
if queueddownload.attemptcount < 5:
download = False
#Retry downloading until we succeed
try:
key = bucket.get_key(keypath)
log.debug('Key objectd is %s' % key)
if os.path.isfile(destfile):
stat = os.stat(destfile)
if self.force:
download = True
elif stat.st_size != key.size:
log.info('%s and %s size differs, will '
're-download' % (key.name, destfile,))
download = True
else:
download = True
if download:
log.info('Downloading %s from %s to %s' %
(key.name, bucket.name, destfile))
key.get_contents_to_filename(destfile)
except Exception as e:
log.debug(e)
log.exception('Failed to download `%s` retrying' %
(fname,))
#We can't download, try again
queueddownload.increment()
queue.put(queueddownload)
else:
log.info('Tried to download %s too many times. Giving up' %
fname)
#Pop the task regardless of state. If it fails we've put it back
queue.task_done()
log.info('Thread #%d finished processing' % (idx,))
def run(self):
self._test_permissions()
log.info('Running')
#queue up the filesets
for filename in self.fileset:
log.info('Pushing file %s onto queue' % filename)
self.queue.put(DownloadCounter(filename))
# launch threads and attach an event to them
for idx in range(0, self.num_threads):
self.threads[idx] = {}
# e = threading.Event()
t = threading.Thread(target=self._worker,
kwargs={'idx': idx, 'queue': self.queue})
t.setDaemon(True)
self.threads[idx] = t
t.start()
#Wait for everything to finish downloading
self.queue.join()
log.info('My job is done.')
def main():
p = pwd.getpwnam(os.environ['USER'])
owner = p.pw_name
group = [_.gr_name for _ in grp.getgrall() if _.gr_gid == p.pw_gid][0]
ap = argparse.ArgumentParser(
description='This is the companion script to the `tablesnap` program '
'which you can use to restore files from an Amazon S3 bucket to any '
'given local directory which you have write-permissions on. While the '
'code is straightforward, the program assumes the files you are '
'restoring got previously backed up with `tablesnap`',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
ap.add_argument('-k', '--aws-key',
required=True, help='Amazon S3 key')
ap.add_argument('-s', '--aws-secret',
required=True, help='Amazon S3 secret')
ap.add_argument('-p', '--preserve', default=False, action='store_true',
help='Preserve the permissions (if they exist) from the source. '
'This overrides -o and -g')
ap.add_argument('-o', '--owner', default=owner,
help='After download, chown files to this user.')
ap.add_argument('-g', '--group', default=group,
help='After download, chgrp files to this group.')
ap.add_argument('-t', '--threads', type=int, default=4,
help='Split the download between this many threads')
ap.add_argument('-f', '--file',
help='If specified, will download the file-set this file belongs to '
'instead of the latest one available.')
ap.add_argument('--force', default=False, action='store_true',
help='Force download files even if they exist')
ap.add_argument('-n', '--name', default=socket.getfqdn(),
help='Use this name instead of the FQDN to prefix the bucket dir')
ap.add_argument('bucket', nargs=1,
help='S3 bucket to download files from')
ap.add_argument('origin', nargs=1,
help='Path inside the bucket to the directory you want to download '
'files from')
ap.add_argument('target', nargs=1,
help='Path in the local FS where files should be downloaded to')
args = ap.parse_args()
dh = DownloadHandler(args)
dh.run()
if __name__ == '__main__':
sys.exit(main())