| 1 |
# Bacula(R) - The Network Backup Solution |
| 2 |
# |
| 3 |
# Copyright (C) 2000-2022 Kern Sibbald |
| 4 |
# |
| 5 |
# The original author of Bacula is Kern Sibbald, with contributions |
| 6 |
# from many others, a complete list can be found in the file AUTHORS. |
| 7 |
# |
| 8 |
# You may use this file and others of this release according to the |
| 9 |
# license defined in the LICENSE file, which includes the Affero General |
| 10 |
# Public License, v3.0 ("AGPLv3") and some additional permissions and |
| 11 |
# terms pursuant to its AGPLv3 Section 7. |
| 12 |
# |
| 13 |
# This notice must be preserved when any source code is |
| 14 |
# conveyed and/or propagated. |
| 15 |
# |
| 16 |
# Bacula(R) is a registered trademark of Kern Sibbald. |
| 17 |
|
| 18 |
import re |
| 19 |
import sys |
| 20 |
from enum import Enum |
| 21 |
import logging |
| 22 |
|
| 23 |
from baculak8s.entities.file_info import FileInfo |
| 24 |
from baculak8s.io.default_io import DefaultIO |
| 25 |
from baculak8s.io.log import Log |
| 26 |
from baculak8s.io.packet_definitions import ACL_DATA_START, XATTR_DATA_START, EOD_PACKET, STATUS_DATA |
| 27 |
from baculak8s.plugins.k8sbackend.k8sfileinfo import k8sfileobjecttype |
| 28 |
|
| 29 |
RESTORE_START = "RestoreStart" |
| 30 |
INVALID_RESTORE_START_PACKET = "Invalid restore job start packet" |
| 31 |
RESTORE_END_PACKET = "FINISH" |
| 32 |
SUCCESS_PACKET = "OK" |
| 33 |
SKIP_PACKET = "SKIP" |
| 34 |
FILE_TRANSFER_START = "DATA" |
| 35 |
INVALID_XATTRS_TRANSFER_START_PACKET = "Invalid extended attributes transfer start packet. Aborting" |
| 36 |
INVALID_ACL_TRANSFER_START_PACKET = "Invalid access control list transfer start packet. Aborting" |
| 37 |
RESTORE_LOOP_ERROR = "Invalid packet during restore loop." |
| 38 |
FNAME_WITHOUT_FSOURCE_ERROR = "Invalid FNAME packet. It should have information about the Files Source." |
| 39 |
|
| 40 |
XATTR_ERROR_TEMPLATE = "Error while transferring files extended attributes to the chosen Data Source" \ |
| 41 |
"\nFile: {}\nBucket: {}.\n" |
| 42 |
|
| 43 |
ACL_ERROR_TEMPLATE = "Error while transferring files access control list to the chosen Data Source" \ |
| 44 |
"\nFile: {}\nBucket: {}.\n" |
| 45 |
|
| 46 |
FILE_ERROR_TEMPLATE = "Error while transferring file content to the chosen Data Source" \ |
| 47 |
"\n\tFile: {}\n\tNamespace: {}\n\tdetails: {}" |
| 48 |
|
| 49 |
BUCKET_ERROR_TEMPLATE = "Error while creating a Bucket on the chosen Data Source" \ |
| 50 |
"Bucket: {}.\n" |
| 51 |
|
| 52 |
COMMA_SEPARATOR_NOT_SUPPORTED = "Comma separator not supported yet." |
| 53 |
|
| 54 |
|
| 55 |
class RestorePacket(Enum): |
| 56 |
FILE_INFO = 1 |
| 57 |
ACL_START = 2 |
| 58 |
XATTR_START = 3 |
| 59 |
RESTORE_END = 4 |
| 60 |
INVALID_PACKET = 5 |
| 61 |
|
| 62 |
|
| 63 |
class RestoreIO(DefaultIO): |
| 64 |
def next_loop_packet(self, onError): |
| 65 |
_, packet = self.read_packet() |
| 66 |
logging.debug('next_loop_packet:packet:' + str(packet)) |
| 67 |
if packet is None: |
| 68 |
return RestorePacket.INVALID_PACKET, None |
| 69 |
|
| 70 |
if packet == RESTORE_END_PACKET: |
| 71 |
return RestorePacket.RESTORE_END, None |
| 72 |
|
| 73 |
if packet.startswith("FNAME:"): |
| 74 |
file_info = self.__read_file_info(packet, onError) |
| 75 |
return RestorePacket.FILE_INFO, file_info |
| 76 |
|
| 77 |
if packet == ACL_DATA_START: |
| 78 |
return RestorePacket.ACL_START, None |
| 79 |
|
| 80 |
if packet == XATTR_DATA_START: |
| 81 |
return RestorePacket.XATTR_START, None |
| 82 |
|
| 83 |
return RestorePacket.INVALID_PACKET, None |
| 84 |
|
| 85 |
def __read_file_info(self, full_fname, onError): |
| 86 |
""" |
| 87 |
Reads four packages from stdin: |
| 88 |
1 - The files FNAME packet |
| 89 |
2 - The files STAT packet |
| 90 |
3 - The files TSTAMP packet |
| 91 |
4 - An EOD packet |
| 92 |
|
| 93 |
:return: The file_info data structure |
| 94 |
""" |
| 95 |
|
| 96 |
full_fname = full_fname.replace("FNAME:", "").rstrip("/") |
| 97 |
|
| 98 |
if "@" not in full_fname: |
| 99 |
_, full_stat = self.read_packet() |
| 100 |
_, full_tstamp = self.read_packet() |
| 101 |
self.read_eod() |
| 102 |
self.send_abort(FNAME_WITHOUT_FSOURCE_ERROR) |
| 103 |
onError() |
| 104 |
return |
| 105 |
|
| 106 |
where_param = self.__read_where_parameter(full_fname) |
| 107 |
|
| 108 |
if where_param is not None: |
| 109 |
full_fname = full_fname.replace(where_param, '', 1).lstrip('/') |
| 110 |
|
| 111 |
# creates an array like: |
| 112 |
# ['@kubernetes', 'namespaces', '$namespace', '$object', '$file.yaml'] |
| 113 |
# ['@kubernetes', 'namespaces', '$namespace', '$object', '$file.tar'] |
| 114 |
# ['@kubernetes', 'namespaces', '$namespace', '$file.yaml'] |
| 115 |
# ['@kubernetes', 'persistentvolumes', '$file.yaml'] |
| 116 |
fname = full_fname.split("/", 4) |
| 117 |
|
| 118 |
_, full_stat = self.read_packet() |
| 119 |
|
| 120 |
# creates an array with [$type$, $size$, $uid$, $gid$, $mode$, $nlink$] |
| 121 |
fstat = re.sub("STAT:", '', full_stat).split(' ') |
| 122 |
|
| 123 |
_, full_tstamp = self.read_packet() |
| 124 |
|
| 125 |
# creates an array with [$atime$, $mtime$, $ctime$] |
| 126 |
ftstamp = re.sub("TSTAMP:", '', full_tstamp).split(' ') |
| 127 |
|
| 128 |
self.read_eod() |
| 129 |
|
| 130 |
# debugging |
| 131 |
logging.debug("fstat:" + str(fstat)) |
| 132 |
logging.debug("fname:" + str(fname)) |
| 133 |
logging.debug("ftstamp:" + str(ftstamp)) |
| 134 |
|
| 135 |
objtype = k8sfileobjecttype(fname) |
| 136 |
return FileInfo( |
| 137 |
# The name may be empty if we have a bucket file |
| 138 |
name=fname[-1], |
| 139 |
ftype=fstat[0], |
| 140 |
size=int(fstat[1]), |
| 141 |
uid=fstat[2], |
| 142 |
gid=fstat[3], |
| 143 |
mode=fstat[4], |
| 144 |
nlink=fstat[5], |
| 145 |
index=fstat[6], |
| 146 |
accessed_at=int(ftstamp[0]), |
| 147 |
modified_at=int(ftstamp[1]), |
| 148 |
created_at=int(ftstamp[2]), |
| 149 |
namespace=objtype['namespace'], |
| 150 |
objtype=objtype['obj'], |
| 151 |
fullfname=fname |
| 152 |
) |
| 153 |
|
| 154 |
def __read_where_parameter(self, full_fname): |
| 155 |
if full_fname.startswith("@"): |
| 156 |
where_param = None |
| 157 |
else: |
| 158 |
# we read the where param part of the FNAME packet |
| 159 |
index_fsource = full_fname.index("/@") |
| 160 |
where_param = full_fname[0:index_fsource] |
| 161 |
where_param = where_param.rstrip("/") |
| 162 |
|
| 163 |
return where_param |
| 164 |
|
| 165 |
def read_data(self): |
| 166 |
""" |
| 167 |
Reads a data packet from the stdio: |
| 168 |
|
| 169 |
"D000123\n" (Data packet header) |
| 170 |
"chunk" (Data packet) |
| 171 |
|
| 172 |
:return: The data packet content, or |
| 173 |
None if an EOD packet (F000000) is found instead |
| 174 |
""" |
| 175 |
header = self.read_line() |
| 176 |
if not header: |
| 177 |
raise ValueError("Packet Header not found") |
| 178 |
logging.debug('io.read_data: {}'.format(header)) |
| 179 |
if header == EOD_PACKET: |
| 180 |
Log.save_received_eod(header) |
| 181 |
return None |
| 182 |
|
| 183 |
# Removes the status of the header to obtain data content length |
| 184 |
chunk_length = int(header.decode().replace(STATUS_DATA, '')) |
| 185 |
chunk = sys.stdin.buffer.read(chunk_length) |
| 186 |
Log.save_received_data(header) |
| 187 |
return chunk |
| 188 |
|
| 189 |
|
| 190 |
class FileContentReader(RestoreIO): |
| 191 |
""" |
| 192 |
Class used to read chunks of file content from standard input. |
| 193 |
""" |
| 194 |
|
| 195 |
def __init__(self): |
| 196 |
self.finished = False |
| 197 |
|
| 198 |
def read(self, size=None): |
| 199 |
if self.finished: |
| 200 |
return None |
| 201 |
|
| 202 |
data = self.read_data() |
| 203 |
if not data: |
| 204 |
self.finished = True |
| 205 |
return data |
| 206 |
|
| 207 |
def finished_transfer(self): |
| 208 |
return self.finished |