-
Notifications
You must be signed in to change notification settings - Fork 256
/
BareosFdPluginLocalFilesetWithRestoreObjects.py
345 lines (315 loc) · 12.8 KB
/
BareosFdPluginLocalFilesetWithRestoreObjects.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# BAREOS - Backup Archiving REcovery Open Sourced
#
# Copyright (C) 2014-2020 Bareos GmbH & Co. KG
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of version three of the GNU Affero General Public
# License as published by the Free Software Foundation, which is
# listed in the file LICENSE.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
#
# Author: Maik Aussendorf
#
# Bareos python plugins class that adds files from a local list to
# the backup fileset
import bareosfd
from bareosfd import *
import os
import re
import hashlib
import time
import BareosFdPluginBaseclass
class BareosFdPluginLocalFilesetWithRestoreObjects(
BareosFdPluginBaseclass.BareosFdPluginBaseclass
):
"""
This Bareos-FD-Plugin-Class was created for automated test purposes only.
It is based on the BareosFdPluginLocalFileset class that parses a file
and backups all files listed there.
Filename is taken from plugin argument 'filename'.
In addition to the file backup and restore, this plugin also tests
restore objects of different sizes. As restore objects are compressed
automatically, when they are greater then 1000 bytes, both uncompressed
and compressed restore objects are tested.
"""
def __init__(self, plugindef):
bareosfd.DebugMessage(
100,
"Constructor called in module %s with plugindef=%s\n"
% (__name__, plugindef),
)
# Last argument of super constructor is a list of mandatory arguments
super(BareosFdPluginLocalFilesetWithRestoreObjects, self).__init__(
plugindef, ["filename"]
)
self.files_to_backup = []
self.allow = None
self.deny = None
self.object_index_seq = int((time.time() - 1546297200) * 10)
self.sha256sums_by_filename = {}
# bareosfd.SetValue(bVarSinceTime,999)
# triple_nine = bareosfd.GetValue(bVarSinceTime)
# if triple_nine != 999:
# bareosfd.JobMessage(
# M_ERROR,
# "Wrong value for since time (should be 999 but is %d)\n" % triple_nine
# )
def filename_is_allowed(self, filename, allowregex, denyregex):
"""
Check, if filename is allowed.
True, if matches allowreg and not denyregex.
If allowreg is None, filename always matches
If denyreg is None, it never matches
"""
if allowregex is None or allowregex.search(filename):
allowed = True
else:
allowed = False
if denyregex is None or not denyregex.search(filename):
denied = False
else:
denied = True
if not allowed or denied:
bareosfd.DebugMessage(100, "File %s denied by configuration\n" % (filename))
bareosfd.JobMessage(
M_ERROR,
"File %s denied by configuration\n" % (filename),
)
return False
else:
return True
def start_backup_job(self):
"""
At this point, plugin options were passed and checked already.
We try to read from filename and setup the list of file to backup
in self.files_to_backup
"""
bareosfd.DebugMessage(
100,
"Using %s to search for local files\n" % (self.options["filename"]),
)
if os.path.exists(self.options["filename"]):
try:
config_file = open(self.options["filename"], "r")
# config_file = open(self.options["filename"], "rb")
except:
bareosfd.DebugMessage(
100,
"Could not open file %s\n" % (self.options["filename"]),
)
return bRC_Error
else:
bareosfd.DebugMessage(
100, "File %s does not exist\n" % (self.options["filename"])
)
return bRC_Error
# Check, if we have allow or deny regular expressions defined
if "allow" in self.options:
self.allow = re.compile(self.options["allow"])
if "deny" in self.options:
self.deny = re.compile(self.options["deny"])
for listItem in config_file.read().splitlines():
if os.path.isfile(listItem) and self.filename_is_allowed(
listItem, self.allow, self.deny
):
self.files_to_backup.append(listItem)
if os.path.isdir(listItem):
for topdir, dirNames, fileNames in os.walk(listItem):
for fileName in fileNames:
if self.filename_is_allowed(
os.path.join(topdir, fileName),
self.allow,
self.deny,
):
self.files_to_backup.append(os.path.join(topdir, fileName))
if os.path.isfile(os.path.join(topdir, fileName)):
self.files_to_backup.append(
os.path.join(topdir, fileName) + ".sha256sum"
)
self.files_to_backup.append(
os.path.join(topdir, fileName) + ".abspath"
)
else:
if os.path.isfile(listItem):
self.files_to_backup.append(listItem + ".sha256sum")
self.files_to_backup.append(listItem + ".abspath")
for longrestoreobject_length in range(998, 1004):
self.files_to_backup.append(
"%s.longrestoreobject" % longrestoreobject_length
)
if not self.files_to_backup:
bareosfd.JobMessage(
M_ERROR,
"No (allowed) files to backup found\n",
)
return bRC_Error
else:
return bRC_Cancel
def start_backup_file(self, savepkt):
"""
Defines the file to backup and creates the savepkt. In this example
only files (no directories) are allowed
"""
bareosfd.DebugMessage(100, "start_backup_file() called\n")
if not self.files_to_backup:
bareosfd.DebugMessage(100, "No files to backup\n")
return bRC_Skip
file_to_backup = self.files_to_backup.pop()
statp = bareosfd.StatPacket()
savepkt.statp = statp
if file_to_backup.endswith(".sha256sum"):
checksum = self.get_sha256sum(os.path.splitext(file_to_backup)[0])
savepkt.type = FT_RESTORE_FIRST
savepkt.fname = file_to_backup
savepkt.object_name = file_to_backup
savepkt.object = bytearray(checksum.encode("utf-8"))
savepkt.object_len = len(savepkt.object)
savepkt.object_index = self.object_index_seq
self.object_index_seq += 1
elif file_to_backup.endswith(".abspath"):
savepkt.type = FT_RESTORE_FIRST
savepkt.fname = file_to_backup
savepkt.object_name = file_to_backup
savepkt.object = bytearray(
os.path.splitext(file_to_backup)[0].encode("utf-8")
)
savepkt.object_len = len(savepkt.object)
savepkt.object_index = self.object_index_seq
self.object_index_seq += 1
elif file_to_backup.endswith(".longrestoreobject"):
teststring_length = int(os.path.splitext(file_to_backup)[0])
savepkt.type = FT_RESTORE_FIRST
savepkt.fname = file_to_backup
savepkt.object_name = file_to_backup
savepkt.object = bytearray(b"a" * teststring_length)
savepkt.object_len = len(savepkt.object)
savepkt.object_index = self.object_index_seq
self.object_index_seq += 1
else:
savepkt.fname = file_to_backup
savepkt.type = FT_REG
bareosfd.JobMessage(
M_INFO,
"Starting backup of {}\n".format(file_to_backup),
)
return bRC_OK
def end_backup_file(self):
"""
Here we return 'bRC_More' as long as our list files_to_backup is not
empty and bRC_OK when we are done
"""
bareosfd.DebugMessage(100, "end_backup_file() entry point in Python called\n")
if self.files_to_backup:
return bRC_More
else:
return bRC_OK
def set_file_attributes(self, restorepkt):
bareosfd.DebugMessage(
100,
"set_file_attributes() entry point in Python called with %s\n"
% (str(restorepkt)),
)
orig_fname = "/" + os.path.relpath(restorepkt.ofname, restorepkt.where)
bareosfd.DebugMessage(
100, "set_file_attributes() orig_fname: {} \n".format(orig_fname)
)
restoreobject_sha256sum = self.sha256sums_by_filename[orig_fname]
file_sha256sum = self.get_sha256sum(orig_fname).encode("utf-8")
bareosfd.DebugMessage(
100,
"set_file_attributes() orig_fname: %s restoreobject_sha256sum: %s file_sha256sum: %s\n"
% (orig_fname, repr(restoreobject_sha256sum), repr(file_sha256sum)),
)
if file_sha256sum != restoreobject_sha256sum:
bareosfd.JobMessage(
M_ERROR,
"bad restoreobject orig_fname: %s restoreobject_sha256sum: %s file_sha256sum: %s\n"
% (orig_fname, repr(restoreobject_sha256sum), repr(file_sha256sum)),
)
return bRC_OK
def end_restore_file(self):
bareosfd.DebugMessage(100, "end_restore_file() self.FNAME: %s\n" % self.FNAME)
return bRC_OK
def restore_object_data(self, ROP):
"""
Note:
This is called in two cases:
- on diff/inc backup (should be called only once)
- on restore (for every job id being restored)
But at the point in time called, it is not possible
to distinguish which of them it is, because job type
is "I" until the bEventStartBackupJob event
"""
bareosfd.DebugMessage(
100,
"BareosFdPluginLocalFilesetWithRestoreObjects:restore_object_data() called with ROP:%s\n"
% (ROP),
)
bareosfd.DebugMessage(
100,
"ROP.object_name(%s): %s\n" % (type(ROP.object_name), ROP.object_name),
)
bareosfd.DebugMessage(
100,
"ROP.plugin_name(%s): %s\n" % (type(ROP.plugin_name), ROP.plugin_name),
)
bareosfd.DebugMessage(
100,
"ROP.object_len(%s): %s\n" % (type(ROP.object_len), ROP.object_len),
)
bareosfd.DebugMessage(
100,
"ROP.object_full_len(%s): %s\n"
% (type(ROP.object_full_len), ROP.object_full_len),
)
bareosfd.DebugMessage(
100, "ROP.object(%s): %s\n" % (type(ROP.object), repr(ROP.object))
)
orig_filename = os.path.splitext(ROP.object_name)[0]
if ROP.object_name.endswith(".sha256sum"):
self.sha256sums_by_filename[orig_filename] = ROP.object
# self.sha256sums_by_filename[orig_filename] = str(ROP.object)
elif ROP.object_name.endswith(".abspath"):
if ROP.object.decode() != orig_filename:
bareosfd.JobMessage(
M_ERROR,
"bad restoreobject orig_fname: %s restoreobject_fname: %s\n"
% (orig_filename, ROP.object.decode()),
)
elif ROP.object_name.endswith(".longrestoreobject"):
stored_length = int(os.path.splitext(ROP.object_name)[0])
if ROP.object.decode() != "a" * stored_length:
bareosfd.JobMessage(
M_ERROR,
"bad long restoreobject {} does not match stored object: {}, {}\n".format(
ROP.object_name, ROP.object.decode(), "a" * stored_length
),
)
else:
bareosfd.DebugMessage(
100,
"not checking restoreobject: %s\n" % (type(ROP.object_name)),
)
return bRC_OK
def get_sha256sum(self, filename):
f = open(filename, "rb")
m = hashlib.sha256()
while True:
d = f.read(8096)
if not d:
break
m.update(d)
f.close()
return m.hexdigest()
# vim: ts=4 tabstop=4 expandtab shiftwidth=4 softtabstop=4