-
Notifications
You must be signed in to change notification settings - Fork 0
/
gsbackup.py
executable file
·269 lines (225 loc) · 7.19 KB
/
gsbackup.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
#!/usr/bin/env python
import json
import pprint
import os
import shutil
import StringIO
import sys
import tempfile
import time
import boto
import gcs_oauth2_boto_plugin
CONFIG = dict()
class Object(object):
def __init__(self, parent, name, size, full_path, uploaded=False):
self.parent = parent
self.name = name
self.full_path = full_path
self.uploaded = uploaded
self.size = size
def __eq__(self, other):
return self.parent == other.parent and self.name == other.name
def __hash__(self):
return hash(self.name) + hash(self.parent)
def to_json(self):
return {
"type": "Object",
"name": self.name,
"uploaded": self.uploaded,
"full_path": self.full_path,
"size": self.size
}
class Folder(object):
def __init__(self, parent, name):
self.parent = parent
self.name = name
self.children = []
def __eq__(self, other):
return self.parent == other.parent and self.name == other.name
def __hash__(self):
return hash(self.name) + hash(self.parent)
def to_json(self):
return {
"type": "Folder",
"name": self.name,
"children": [ c.to_json() for c in self.children ]
}
class State(object):
def __init__(self, root):
self.root = root
self.object_count = 0
self.folder_count = 0
def _add_folders(self, top, rest):
if not rest:
return top
f = Folder(top, rest[0])
if f not in top.children:
top.children.append(f)
self.folder_count += 1
else:
f = top.children[top.children.index(f)]
return self._add_folders(f, rest[1:])
def _add_directory(self, directory, filenames):
split_path = directory.split("/")[1:]
folder = self._add_folders(self.root, split_path)
for fname in filenames:
if fname == CONFIG['ignore_pattern']:
continue
full_path = os.path.join(CONFIG['source_strip_prefix'] + directory, fname)
size = os.path.getsize(full_path)
obj = Object(folder, fname, size, full_path)
if obj not in folder.children:
self.object_count += 1
folder.children.append(obj)
def _collect_objects(self, start_from):
objects = []
def _collect(root, objects):
for obj in root.children:
if isinstance(obj, Object):
objects.append(obj)
elif isinstance(obj, Folder):
_collect(obj, objects)
_collect(start_from, objects)
return objects
def _find_folder(self, folder_name):
def _find(root, path):
if not path:
return root
for c in root.children:
if isinstance(c, Folder) and c.name == path[0]:
return _find(c, path[1:])
return _find(self.root, folder_name.split('/'))
def mark_as_uploaded(self, folder_name):
folder = self._find_folder(folder_name)
objects = self._collect_objects(folder)
counter = 0
for o in objects:
o.uploaded = True
counter += 1
print '%d objects marked as uploaded' % counter
def upload(self, uploader):
for obj in [ o for o in self._collect_objects(self.root) if not o.uploaded ]:
uploader.upload(obj)
obj.uploaded = True
self.persist(skip_backup=True)
def print_not_uploaded(self):
for obj in [ o for o in self._collect_objects(self.root) ]:
if not obj.uploaded:
print obj.full_path, obj.uploaded
def stats(self):
objects = self._collect_objects(self.root)
print '%d objects' % len(objects)
print '%d objects uploaded' % len([ o for o in objects if o.uploaded ])
print '%d objects not uploaded' % len([ o for o in objects if not o.uploaded ])
print '%s uploaded' % format_size(sum([o.size for o in objects if o.uploaded]))
print '%s not uploaded' % format_size(sum([o.size for o in objects if not o.uploaded]))
def refresh(self):
for (dirpath, dirnames, filenames) in os.walk(CONFIG['source_directory']):
self._add_directory(dirpath.replace(CONFIG['source_strip_prefix'], ''), filenames)
def persist(self, skip_backup=False):
db = CONFIG['state_file']
if not skip_backup and os.path.exists(db):
backup = '.' + db + '.' + str(int(time.time()))
print 'Creating backup ' + backup
os.rename(db, backup)
open(db, 'w').write(json.dumps(state.root.to_json(), indent=True))
@staticmethod
def build():
state = State(Folder("/", "/"))
for (dirpath, dirnames, filenames) in os.walk(CONFIG['source_directory']):
state._add_directory(dirpath.replace(CONFIG['source_strip_prefix'], ''), filenames)
return state
@staticmethod
def from_file():
def _add(obj, parent):
if obj['type'] == 'Folder':
folder = Folder(parent, obj['name'])
state.folder_count += 1
for c in obj['children']:
_add(c, folder)
parent.children.append(folder)
elif obj['type'] == 'Object':
state.object_count += 1
name = obj['name']
uploaded = obj['uploaded']
size = None
full_path = None
if 'size' in obj: size = obj['size']
if 'full_path' in obj: full_path = obj['full_path']
parent.children.append(Object(parent, name, size, full_path, uploaded=uploaded))
print "Loading", CONFIG['state_file']
state = State(None)
tree = json.load(open(CONFIG['state_file']))
root = Folder("/", "/")
for obj in tree['children']:
_add(obj, root)
state.root = root
print "Loaded %d objects in %d folders" % (state.object_count, state.folder_count)
return state
class GoogleStorageUploader(object):
def __init__(self):
gcs_oauth2_boto_plugin.SetFallbackClientIdAndSecret(CONFIG['client_id'], CONFIG['client_secret'])
def upload(self, obj):
sys.stdout.write('Uploading %s/%s %s... ' % (obj.parent.name, obj.name, format_size(obj.size)))
sys.stdout.flush()
with open(obj.full_path, 'r') as localfile:
dest_uri = boto.storage_uri(CONFIG['bucket_id'] + obj.full_path, 'gs')
dest_uri.new_key().set_contents_from_file(localfile)
sys.stdout.write('Done!\n')
def format_size(num, suffix='B'):
for unit in ['','Ki','Mi','Gi','Ti','Pi','Ei','Zi']:
if abs(num) < 1024.0:
return "%3.1f%s%s" % (num, unit, suffix)
num /= 1024.0
return "%.1f%s%s" % (num, 'Yi', suffix)
def usage():
print 'gsbackup.py --config FILE COMMAND'
print ''
print 'Commands:'
print ' --initial Create initial database by scanning source directory'
print ' --refresh Refresh database by scanning source directory '
print ' --upload Upload to Google Storage, abort with Ctrl-C '
print ' --list-not-uploaded List files that have not been uploaded '
print ''
if __name__ == '__main__':
state = None
if len(sys.argv) > 3:
if sys.argv[1] != '--config':
usage()
sys.exit(1)
config_file = sys.argv[2]
CONFIG = json.load(open(config_file))
command = sys.argv[3]
if command == '--initial':
state = State.build()
state.persist()
elif command == '--refresh':
state = State.from_file()
state.refresh()
state.persist()
elif command == '--stats':
state = State.from_file()
state.stats()
elif command == '--list-not-uploaded':
state = State.from_file()
state.print_not_uploaded()
elif command == '--mark-as-uploaded':
state = State.from_file()
if len(sys.argv) != 5:
usage()
sys.exit(1)
folder = sys.argv[4]
state.mark_as_uploaded(folder)
state.persist()
elif command == '--upload':
try:
state = State.from_file()
state.upload(GoogleStorageUploader())
except KeyboardInterrupt:
state.persist()
else:
usage()
sys.exit(1)
else:
usage()
sys.exit(1)