forked from Zettaknight/zettaknight
/
zettaknight_recover.py
298 lines (247 loc) · 10.4 KB
/
zettaknight_recover.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Import python libs
import zettaknight_utils
import zettaknight_globs
import sys
import os
import subprocess
import shlex
import inspect
import datetime
import re
def find_versions(dataset, filename, quiet=False):
'''
'''
zettaknight_utils.check_quiet(quiet)
snaps = {}
ret = {}
ret[dataset] = {}
snaplist_cmd = "/sbin/zfs list -r -t snapshot -o name -H {0}".format(dataset)
snaplist_run = subprocess.Popen(shlex.split(snaplist_cmd), stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
snaplist_run.wait()
snaplist_out = snaplist_run.stdout.read()
if not snaplist_out:
try:
out_dict = {}
out_dict[dataset] = {}
job = inspect.stack()[0][3]
if str(inspect.stack()[1][3]) is 'recover':
job = inspect.stack()[1][3]
out_dict[dataset][job] = {}
out_dict[dataset][job]['1'] = "No snapshots found."
raise Exception
except Exception as e:
zettaknight_utils.parse_output(out_dict)
sys.exit(0)
for snap in snaplist_out.split():
if snap.startswith("cannot"):
try:
raise Exception("{0}".format(snaplist_out))
except Exception as e:
print(zettaknight_utils.printcolors(e, "FAIL"))
sys.exit(0)
snapdiff_cmd = "/sbin/zfs diff {0}".format(snap)
gerp_cmd = "/bin/grep {0}".format(filename)
gerp_run = zettaknight_utils.pipe_this(snapdiff_cmd, gerp_cmd)
gerp_out = gerp_run.stdout.read()
gerp_list = []
if gerp_out:
for gerp in gerp_out.split('\n'):
if gerp.startswith("-") or gerp.startswith("M") or gerp.startswith("R"):
gerp_list.append(gerp)
if gerp_list:
snaps[snap] = gerp_list
gerp_msg = ""
for z in gerp_list:
if gerp_msg:
gerp_msg = "{0}\n{1}".format(gerp_msg, z)
else:
gerp_msg = str(z)
job = "Snapshot: {0}".format(snap)
ret[dataset][job] = {}
job_out = "Path:\n{0}".format(gerp_msg)
ret[dataset][job]['0'] = job_out
if not ret[dataset]:
ret[dataset]['Snapshot'] = {}
ret[dataset]['Snapshot']['1'] = "No modified versions of {0} found.".format(filename, dataset)
if not quiet:
zettaknight_utils.parse_output(ret)
return snaps
def recover(snapshot, filename, relocate=None):
'''
'''
file = filename
snap = snapshot
dataset, snap_date = snap.split('@', 1)
find_snap = find_versions(dataset, file, "quiet")
find_snap_keys = find_snap.iterkeys()
snap_basedir = "/{0}/.zfs/snapshot".format(dataset)
active_basedir = "/{0}".format(dataset)
dir_flag = False
ret = {}
ret[dataset] = {}
try:
if str(dataset) in str(file):
file = file.split(dataset, 1)[1]
last_ref = file.rsplit("/", 1)[1]
except Exception as e:
pass
if dataset in file.rsplit("/", 1):
file = file.split(dataset, 1)[1]
last_ref = file.rsplit("/",1)[1]
if snap not in find_snap_keys:
try:
raise Exception("Recoverable versions of {0} not found in dataset {1}".format(file, dataset))
except Exception as e:
print(zettaknight_utils.printcolors(e, "FAIL"))
path_list1 = find_snap[snap]
path_list = []
if os.path.isdir("/{0}{1}".format(dataset, file)):
dir_flag = True
for line in path_list1:
if line.startswith("-") or line.startswith("M") or line.startswith("R"):
a, b = line.rsplit("/", 1)
if not dir_flag:
try:
if str(b) == str(last_ref):
path_list.append(line)
except:
if str(b) == str(file):
path_list.append(line)
pass
else:
if a.startswith("-") or line.startswith("M") or line.startswith("R"):
a, b = line.rsplit(dataset, 2)
if os.path.isdir("{0}/{1}{2}".format(snap_basedir, snap_date, b)):
dir_flag = True
path_list.append(line)
if len(path_list) > 1:
exc_out = ""
for i in path_list:
if exc_out:
exc_out = "{0}\n\t{1}".format(exc_out, i)
else:
exc_out = str(i)
err_msg = "Matching files/folders in snapshot:"
print(zettaknight_utils.printcolors("\nAmbiguous recover request. Multiple matches for {0}.".format(file), "FAIL"))
print("{0} {1}".format(zettaknight_utils.printcolors(err_msg, "FAIL"), zettaknight_utils.printcolors(snap, "OKBLUE")))
print("\t{0}".format(zettaknight_utils.printcolors(exc_out, "WARNING")))
print(zettaknight_utils.printcolors("Re-run with explicit path to file.\n", "FAIL"))
try:
raise Exception("Ambiguous file reference.")
except Exception as e:
print(zettaknight_utils.printcolors(e, "FAIL"))
sys.exit(0)
if len(path_list) < 1:
try:
raise Exception("No restorable files or folders identified. \nRe-run with explicit path to file?")
except Exception as e:
print(zettaknight_utils.printcolors(e, "FAIL"))
sys.exit(0)
a, p = path_list[0].split(dataset, 1)
path, dirs = p.split(file, 1)
if dir_flag:
a, b = path_list[0].rsplit(dataset, 2)
file_loc = "{0}/{1}{2}".format(snap_basedir, snap_date, b)
#bad, dir_path = path_list[0].split(file, 1)
#list_dirs = dir_path.rsplit("/", 1)
#try:
# out_dir = list_dirs[0]
#except:
out_dir = ""
# pass
#file_loc = "{0}/{1}/{2}{3}/".format(snap_basedir, snap_date, file, dir_path )
#mkdir_cmd = "/bin/mkdir {0}/{1}.R".format(active_basedir, file)
#mkdir_run = zettaknight_utils.spawn_job(mkdir_cmd)
#mkdir_cmd = "/bin/mkdir -p {0}/{1}.R/{2}".format(active_basedir, file, dir_path)
#mkdir_run = zettaknight_utils.spawn_job(mkdir_cmd)
out_path = "{0}{1}{2}.R{3}/".format(active_basedir, str(path), file, out_dir)
else:
file_loc = "{0}/{1}/{2}".format(snap_basedir, snap_date, p)
out_path = "{0}{1}{2}.R".format(active_basedir, str(path), file)
if relocate:
out_path = relocate
rec_cmd = "/bin/cp ""-r -p ""{0}"" ""{1}".format(file_loc, out_path)
rec_run = zettaknight_utils.spawn_job(rec_cmd)
rec_dict = eval(str(rec_run))
for k,v in rec_dict.iteritems():
if str(k) is "0":
print("Recover operation succeeded.")
print("\tFilename: {0}".format(zettaknight_utils.printcolors(file, "OKBLUE")))
print("\t\tFile(s) restored to version: {0}".format(zettaknight_utils.printcolors(snap_date, "OKGREEN")))
print("\t\tFile(s) restored to: {0}".format(zettaknight_utils.printcolors(out_path, "OKGREEN")))
if str(k) is not "0":
print("Recover operation failed.")
print("\tFilename: {0}".format(zettaknight_utils.printcolors(file, "OKBLUE")))
print("\t\tRestore failed with error: {0}".format(zettaknight_utils.printcolors(v, "FAIL")))
return
def audit_last_snap(dataset):
'''
'''
snaps = {}
ret = {}
ret[dataset] = {}
job = inspect.stack()[0][3]
snaplist_run = zettaknight_utils.pipe_this2("/sbin/zfs list -r -t snapshot -o name -H {0} | tail -2".format(dataset))
snaplist_out = snaplist_run.stdout.read()
if not snaplist_out:
try:
ret[dataset][job] = {}
ret[dataset][job]['1'] = "No snapshots found."
raise Exception
except Exception as e:
return ret
snap_list = list(snaplist_out.split())
if snap_list[0].startswith("cannot"):
try:
ret[dataset][job] = {}
ret[dataset][job]['1'] = "{0}".format(snap_list[0])
raise Exception
except Exception as e:
return ret
if len(snap_list) == 2:
diff_cmd = "/sbin/zfs diff {0} {1} -H | cat".format(snap_list[0], snap_list[1])
job = "Audit of newly ingested snapshot {0}".format(snap_list[1])
else:
diff_cmd = "/sbin/zfs diff {0} -H | cat".format(snap_list[0])
job = "Audit of newly ingested snapshot {0}".format(snap_list[0])
diff_run = zettaknight_utils.pipe_this2("{0}".format(diff_cmd))
diff_out = diff_run.stdout.read()
if diff_out:
mod_count = 0
mod_list = ""
add_count = 0
add_list = ""
del_count = 0
del_list = ""
for diff in diff_out.split('\n'):
if diff.startswith("M"):
mod_count += 1
if mod_list:
mod_list = "{0}\n\t{1}".format(mod_list, diff)
else:
mod_list = "\t{0}".format(str(diff))
if diff.startswith("+"):
add_count += 1
if add_list:
add_list = "{0}\n\t{1}".format(add_list, diff)
else:
add_list = "\t{0}".format(str(diff))
if diff.startswith("-"):
del_count += 1
if del_list:
del_list = "{0}\n\t{1}".format(del_list, diff)
else:
del_list = "\t{0}".format(str(diff))
ret[dataset][job] = {}
job_out = ""
if mod_list:
job_out = "\nFile modifications in last ingested snapshot:\n{0}".format(mod_list)
if add_list:
job_out = "{0}\n\nNew Files in last ingested snapshot:\n{1}".format(job_out, add_list)
if del_list:
job_out = "{0}\n\nFile deletions in last ingested snapshot:\n{1}".format(job_out, del_list)
job_out = "{0}\n\nChange counts in last ingested snapshot:\n\tModified Files: {1}\n\tNew Files: {2}\n\tDeleted Files: {3}".format(job_out, str(mod_count), str(add_count), str(del_count))
ret[dataset][job]['0'] = job_out
return ret