barman.compression module#

This module is responsible to manage the compression features of Barman

class barman.compression.BZip2Compressor(config, compression, path=None)View on GitHub#

Bases: CommandCompressor

Predefined compressor with BZip2

LEVEL_HIGH = 9#
LEVEL_LOW = 1#
LEVEL_MAX = 9#
LEVEL_MEDIUM = 5#
LEVEL_MIN = 1#
MAGIC = b'BZh'#
__init__(config, compression, path=None)View on GitHub#
Parameters:
  • config – barman.config.ServerConfig

  • compression – str compression name

  • path – str|None

_abc_impl = <_abc._abc_data object>#
class barman.compression.CommandCompressor(config, compression, path=None)View on GitHub#

Bases: Compressor

Base class for compressors built on external commands

__init__(config, compression, path=None)View on GitHub#
Parameters:
  • config – barman.config.ServerConfig

  • compression – str compression name

  • path – str|None

_abc_impl = <_abc._abc_data object>#
_build_command(pipe_command)View on GitHub#

Build the command string and create the actual Command object

Parameters:

pipe_command – the command used to compress/decompress

Return type:

Command

compress(src, dst)View on GitHub#

Compress using the specific command defined in the subclass

Parameters:
  • src – source file to compress

  • dst – destination of the decompression

decompress(src, dst)View on GitHub#

Decompress using the specific command defined in the subclass

Parameters:
  • src – source file to decompress

  • dst – destination of the decompression

class barman.compression.CompressionView on GitHub#

Bases: object

Abstract class meant to represent compression interface

_abc_impl = <_abc._abc_data object>#
abstract property file_extensionView on GitHub#
Returns:

abstractmethod get_file_content(filename, archive)View on GitHub#
Parameters:
  • filename – str file to search for in the archive (requires its full path within the archive)

  • archive – str archive path/name without extension

Returns:

string content

abstract property nameView on GitHub#
Returns:

abstractmethod uncompress(src, dst, exclude=None, include_args=None)View on GitHub#
Parameters:
  • src – source file path without compression extension

  • dst – destination path

  • exclude – list of filepath in the archive to exclude from the extraction

  • include_args – list of filepath in the archive to extract.

Returns:

validate_dst(dst)View on GitHub#
validate_src_and_dst(src)View on GitHub#
class barman.compression.CompressionManager(config, path)View on GitHub#

Bases: object

__init__(config, path)View on GitHub#
Parameters:
  • config – barman.config.ServerConfig

  • path – str

check(compression=None)View on GitHub#

This method returns True if the compression specified in the configuration file is present in the register, otherwise False

get_compressor(compression)View on GitHub#

Returns a new compressor instance

Parameters:

compression (str) – Compression name or none

get_default_compressor()View on GitHub#

Returns a new default compressor instance

identify_compression(filename)View on GitHub#

Try to guess the compression algorithm of a file

Parameters:

filename (str) – the path of the file to identify

Return type:

str

class barman.compression.Compressor(config, compression, path=None)View on GitHub#

Bases: object

Base class for all the compressors

Variables:
  • MAGIC – Magic bytes used to identify the compression format

  • LEVEL_MIN – Minimum compression level supported by the compression algorithm

  • LEVEL_MAX – Maximum compression level supported by the compression algorithm

  • LEVEL_LOW – Mapped level to low

  • LEVEL_MEDIUM – Mapped level to medium

  • LEVEL_HIGH – Mapped level to high

LEVEL_HIGH = None#
LEVEL_LOW = None#
LEVEL_MAX = None#
LEVEL_MEDIUM = None#
LEVEL_MIN = None#
MAGIC = None#
__init__(config, compression, path=None)View on GitHub#
Parameters:
  • config – barman.config.ServerConfig

  • compression – str compression name

  • path – str|None

_abc_impl = <_abc._abc_data object>#
abstractmethod compress(src, dst)View on GitHub#

Abstract Method for compression method

Parameters:
  • src (str) – source file path

  • dst (str) – destination file path

abstractmethod decompress(src, dst)View on GitHub#

Abstract method for decompression method

Parameters:
  • src (str) – source file path

  • dst (str) – destination file path

classmethod validate(file_start)View on GitHub#

Guess if the first bytes of a file are compatible with the compression implemented by this class

Parameters:

file_start – a binary string representing the first few bytes of a file

Return type:

bool

class barman.compression.CustomCompressor(config, compression, path=None)View on GitHub#

Bases: CommandCompressor

Custom compressor

__init__(config, compression, path=None)View on GitHub#
Parameters:
  • config – barman.config.ServerConfig

  • compression – str compression name

  • path – str|None

_abc_impl = <_abc._abc_data object>#
class barman.compression.GZipCompression(command)View on GitHub#

Bases: Compression

__init__(command)View on GitHub#
Parameters:

command – barman.fs.UnixLocalCommand

_abc_impl = <_abc._abc_data object>#
file_extension = 'tar.gz'#
get_file_content(filename, archive)View on GitHub#
Parameters:
  • filename – str file to search for in the archive (requires its full path within the archive)

  • archive – str archive path/name without extension

Returns:

string content

name = 'gzip'#
uncompress(src, dst, exclude=None, include_args=None)View on GitHub#
Parameters:
  • src – source file path without compression extension

  • dst – destination path

  • exclude – list of filepath in the archive to exclude from the extraction

  • include_args – list of filepath in the archive to extract.

Returns:

class barman.compression.GZipCompressor(config, compression, path=None)View on GitHub#

Bases: CommandCompressor

Predefined compressor with GZip

LEVEL_HIGH = 9#
LEVEL_LOW = 1#
LEVEL_MAX = 9#
LEVEL_MEDIUM = 6#
LEVEL_MIN = 1#
MAGIC = b'\x1f\x8b\x08'#
__init__(config, compression, path=None)View on GitHub#
Parameters:
  • config – barman.config.ServerConfig

  • compression – str compression name

  • path – str|None

_abc_impl = <_abc._abc_data object>#
class barman.compression.GZipPgBaseBackupCompressionOption(pg_base_backup_config)View on GitHub#

Bases: PgBaseBackupCompressionOption

validate(pg_server_version, remote_status)View on GitHub#

Validate gzip-specific options.

Parameters:
  • int (pg_server_version) – the server for which the compression options should be validated.

  • remote_status (dict) – the status of the pg_basebackup command

Return List:

List of Issues (str) or empty list

class barman.compression.InternalCompressor(config, compression, path=None)View on GitHub#

Bases: Compressor

Base class for compressors built on python libraries

_abc_impl = <_abc._abc_data object>#
abstractmethod _compressor(dst)View on GitHub#

Abstract compressor factory method

Parameters:

dst – destination file path

Returns:

a file-like writable compressor object

abstractmethod _decompressor(src)View on GitHub#

Abstract decompressor factory method

Parameters:

src – source file path

Returns:

a file-like readable decompressor object

compress(src, dst)View on GitHub#

Compress using the object defined in the subclass

Parameters:
  • src – source file to compress

  • dst – destination of the decompression

abstractmethod compress_in_mem(fileobj)View on GitHub#

Compresses the given file-object in memory

Parameters:

fileobj – source file-object to be compressed

Returns:

a compressed file-object

Note

When implementing this method, the compressed file-object position must be set to 0 before returning it, as it is likely to be read again afterwards.

decompress(src, dst)View on GitHub#

Decompress using the object defined in the subclass

Parameters:
  • src – source file to decompress

  • dst – destination of the decompression

abstractmethod decompress_in_mem(fileobj)View on GitHub#

Decompresses the given file-object in memory

Parameters:

fileobj – source file-object to be decompressed

Returns:

a decompressed file-object

decompress_to_fileobj(src_fileobj, dest_fileobj)View on GitHub#

Decompresses the given file-object on the especified file-object

Parameters:
  • src_fileobj – source file-object to be decompressed

  • dest_fileobj – destination file-object to have the decompressed content

class barman.compression.LZ4Compression(command)View on GitHub#

Bases: Compression

__init__(command)View on GitHub#
Parameters:

command – barman.fs.UnixLocalCommand

_abc_impl = <_abc._abc_data object>#
file_extension = 'tar.lz4'#
get_file_content(filename, archive)View on GitHub#
Parameters:
  • filename – str file to search for in the archive (requires its full path within the archive)

  • archive – str archive path/name without extension

Returns:

string content

name = 'lz4'#
uncompress(src, dst, exclude=None, include_args=None)View on GitHub#
Parameters:
  • src – source file path without compression extension

  • dst – destination path

  • exclude – list of filepath in the archive to exclude from the extraction

  • include_args – list of filepath in the archive to extract.

Returns:

class barman.compression.LZ4Compressor(config, compression, path=None)View on GitHub#

Bases: InternalCompressor

Predefined compressor with lz4

LEVEL_HIGH = 10#
LEVEL_LOW = 0#
LEVEL_MAX = 16#
LEVEL_MEDIUM = 6#
LEVEL_MIN = 0#
MAGIC = b'\x04"M\x18'#
__init__(config, compression, path=None)View on GitHub#

Constructor. :param config: barman.config.ServerConfig :param compression: str compression name :param path: str|None

_abc_impl = <_abc._abc_data object>#
_compressor(dst)View on GitHub#

Abstract compressor factory method

Parameters:

dst – destination file path

Returns:

a file-like writable compressor object

_decompressor(src)View on GitHub#

Abstract decompressor factory method

Parameters:

src – source file path

Returns:

a file-like readable decompressor object

compress_in_mem(fileobj)View on GitHub#

Compresses the given file-object in memory

Parameters:

fileobj – source file-object to be compressed

Returns:

a compressed file-object

Note

When implementing this method, the compressed file-object position must be set to 0 before returning it, as it is likely to be read again afterwards.

decompress_in_mem(fileobj)View on GitHub#

Decompresses the given file-object in memory

Parameters:

fileobj – source file-object to be decompressed

Returns:

a decompressed file-object

property lz4View on GitHub#
class barman.compression.LZ4PgBaseBackupCompressionOption(pg_base_backup_config)View on GitHub#

Bases: PgBaseBackupCompressionOption

validate(pg_server_version, remote_status)View on GitHub#

Validate lz4-specific options.

Parameters:
  • int (pg_server_version) – the server for which the compression options should be validated.

  • remote_status (dict) – the status of the pg_basebackup command

Return List:

List of Issues (str) or empty list

class barman.compression.NoneCompression(command)View on GitHub#

Bases: Compression

__init__(command)View on GitHub#
Parameters:

command – barman.fs.UnixLocalCommand

_abc_impl = <_abc._abc_data object>#
file_extension = 'tar'#
get_file_content(filename, archive)View on GitHub#
Parameters:
  • filename – str file to search for in the archive (requires its full path within the archive)

  • archive – str archive path/name without extension

Returns:

string content

name = 'none'#
uncompress(src, dst, exclude=None, include_args=None)View on GitHub#
Parameters:
  • src – source file path without compression extension

  • dst – destination path

  • exclude – list of filepath in the archive to exclude from the extraction

  • include_args – list of filepath in the archive to extract.

Returns:

class barman.compression.NonePgBaseBackupCompressionOption(pg_base_backup_config)View on GitHub#

Bases: PgBaseBackupCompressionOption

validate(pg_server_version, remote_status)View on GitHub#

Validate none compression specific options.

Parameters:
  • int (pg_server_version) – the server for which the compression options should be validated.

  • remote_status (dict) – the status of the pg_basebackup command

Return List:

List of Issues (str) or empty list

class barman.compression.PgBaseBackupCompression(pg_basebackup_compression_cfg, pg_basebackup_compression_option, compression)View on GitHub#

Bases: object

Represents the pg_basebackup compression options and provides functionality required by the backup process which depends on those options. This is a facade that interacts with appropriate classes

__init__(pg_basebackup_compression_cfg, pg_basebackup_compression_option, compression)View on GitHub#

Constructor for the PgBaseBackupCompression facade that handles base_backup class related.

Parameters:
  • PgBaseBackupCompressionConfig (pg_basebackup_compression_cfg) – pg_basebackup compression configuration

  • PgBaseBackupCompressionOption (pg_basebackup_compression_option)

  • Compression (compression)

get_file_content(filename, archive)View on GitHub#

Returns archive specific file content :param filename: str :param archive: str :return: str

validate(pg_server_version, remote_status)View on GitHub#

Validate pg_basebackup compression options.

Parameters:
  • int (pg_server_version) – the server for which the compression options should be validated.

  • remote_status (dict) – the status of the pg_basebackup command

Return List:

List of Issues (str) or empty list

with_suffix(basename)View on GitHub#

Append the suffix to the supplied basename.

Parameters:

basename (str) – The basename (without compression suffix) of the file to be opened.

class barman.compression.PgBaseBackupCompressionConfig(backup_compression, backup_compression_format, backup_compression_level, backup_compression_location, backup_compression_workers)View on GitHub#

Bases: object

Should become a dataclass

__init__(backup_compression, backup_compression_format, backup_compression_level, backup_compression_location, backup_compression_workers)View on GitHub#
class barman.compression.PgBaseBackupCompressionOption(pg_base_backup_config)View on GitHub#

Bases: object

This class is in charge of validating pg_basebackup compression options

__init__(pg_base_backup_config)View on GitHub#
Parameters:

pg_base_backup_config – PgBaseBackupCompressionConfig

validate(pg_server_version, remote_status)View on GitHub#

Validate pg_basebackup compression options.

Parameters:
  • int (pg_server_version) – the server for which the compression options should be validated.

  • remote_status (dict) – the status of the pg_basebackup command

Return List:

List of Issues (str) or empty list

class barman.compression.PigzCompressor(config, compression, path=None)View on GitHub#

Bases: CommandCompressor

Predefined compressor with Pigz

Note that pigz on-disk is the same as gzip, so the MAGIC value of this class is the same

LEVEL_HIGH = 9#
LEVEL_LOW = 1#
LEVEL_MAX = 9#
LEVEL_MEDIUM = 6#
LEVEL_MIN = 1#
MAGIC = b'\x1f\x8b\x08'#
__init__(config, compression, path=None)View on GitHub#
Parameters:
  • config – barman.config.ServerConfig

  • compression – str compression name

  • path – str|None

_abc_impl = <_abc._abc_data object>#
class barman.compression.PyBZip2Compressor(config, compression, path=None)View on GitHub#

Bases: InternalCompressor

Predefined compressor with BZip2 Python libraries

LEVEL_HIGH = 9#
LEVEL_LOW = 1#
LEVEL_MAX = 9#
LEVEL_MEDIUM = 5#
LEVEL_MIN = 1#
MAGIC = b'BZh'#
_abc_impl = <_abc._abc_data object>#
_compressor(name)View on GitHub#

Abstract compressor factory method

Parameters:

dst – destination file path

Returns:

a file-like writable compressor object

_decompressor(name)View on GitHub#

Abstract decompressor factory method

Parameters:

src – source file path

Returns:

a file-like readable decompressor object

compress_in_mem(fileobj)View on GitHub#

Compresses the given file-object in memory

Parameters:

fileobj – source file-object to be compressed

Returns:

a compressed file-object

Note

When implementing this method, the compressed file-object position must be set to 0 before returning it, as it is likely to be read again afterwards.

decompress_in_mem(fileobj)View on GitHub#

Decompresses the given file-object in memory

Parameters:

fileobj – source file-object to be decompressed

Returns:

a decompressed file-object

class barman.compression.PyGZipCompressor(config, compression, path=None)View on GitHub#

Bases: InternalCompressor

Predefined compressor that uses GZip Python libraries

LEVEL_HIGH = 9#
LEVEL_LOW = 1#
LEVEL_MAX = 9#
LEVEL_MEDIUM = 6#
LEVEL_MIN = 1#
MAGIC = b'\x1f\x8b\x08'#
__init__(config, compression, path=None)View on GitHub#
Parameters:
  • config – barman.config.ServerConfig

  • compression – str compression name

  • path – str|None

_abc_impl = <_abc._abc_data object>#
_compressor(name)View on GitHub#

Abstract compressor factory method

Parameters:

dst – destination file path

Returns:

a file-like writable compressor object

_decompressor(name)View on GitHub#

Abstract decompressor factory method

Parameters:

src – source file path

Returns:

a file-like readable decompressor object

compress_in_mem(fileobj)View on GitHub#

Compresses the given file-object in memory

Parameters:

fileobj – source file-object to be compressed

Returns:

a compressed file-object

Note

When implementing this method, the compressed file-object position must be set to 0 before returning it, as it is likely to be read again afterwards.

decompress_in_mem(fileobj)View on GitHub#

Decompresses the given file-object in memory

Parameters:

fileobj – source file-object to be decompressed

Returns:

a decompressed file-object

class barman.compression.SnappyCompressor(config, compression, path=None)View on GitHub#

Bases: InternalCompressor

MAGIC = b'\xff\x06\x00\x00sNaPpY'#
__init__(config, compression, path=None)View on GitHub#

Constructor. :param config: barman.config.ServerConfig :param compression: str compression name :param path: str|None

_abc_impl = <_abc._abc_data object>#
_compressor(dst)View on GitHub#

Snappy library does not provide an interface which returns file-objects

_decompressor(src)View on GitHub#

Snappy library does not provide an interface which returns file-objects

compress(src, dst)View on GitHub#

Snappy-compress the source file-object to the destination file-object

Parameters:
  • src – source file to compress

  • dst – destination of the decompression

compress_in_mem(fileobj)View on GitHub#

Compresses the given file-object in memory

Parameters:

fileobj – source file-object to be compressed

Returns:

a compressed file-object

Note

When implementing this method, the compressed file-object position must be set to 0 before returning it, as it is likely to be read again afterwards.

decompress(src, dst)View on GitHub#

Decompress the source file-object to the destination file-object

Parameters:
  • src – source file to decompress

  • dst – destination of the decompression

decompress_in_mem(fileobj)View on GitHub#

Decompresses the given file-object in memory

Parameters:

fileobj – source file-object to be decompressed

Returns:

a decompressed file-object

decompress_to_fileobj(src_fileobj, dest_fileobj)View on GitHub#

Decompresses the given file-object on the especified file-object

Parameters:
  • src_fileobj – source file-object to be decompressed

  • dest_fileobj – destination file-object to have the decompressed content

Note

We override this method to avoid redundant work. As Snappy can stream the result directly to a specified object, there is no need for intermediate objects as used in the parent class implementation.

property snappyView on GitHub#
class barman.compression.XZCompressor(config, compression, path=None)View on GitHub#

Bases: InternalCompressor

Predefined compressor with XZ Python library

LEVEL_HIGH = 5#
LEVEL_LOW = 1#
LEVEL_MAX = 9#
LEVEL_MEDIUM = 3#
LEVEL_MIN = 1#
MAGIC = b'\xfd7zXZ\x00'#
_abc_impl = <_abc._abc_data object>#
_compressor(dst)View on GitHub#

Abstract compressor factory method

Parameters:

dst – destination file path

Returns:

a file-like writable compressor object

_decompressor(src)View on GitHub#

Abstract decompressor factory method

Parameters:

src – source file path

Returns:

a file-like readable decompressor object

compress_in_mem(fileobj)View on GitHub#

Compresses the given file-object in memory

Parameters:

fileobj – source file-object to be compressed

Returns:

a compressed file-object

Note

When implementing this method, the compressed file-object position must be set to 0 before returning it, as it is likely to be read again afterwards.

decompress_in_mem(fileobj)View on GitHub#

Decompresses the given file-object in memory

Parameters:

fileobj – source file-object to be decompressed

Returns:

a decompressed file-object

class barman.compression.ZSTDCompression(command)View on GitHub#

Bases: Compression

__init__(command)View on GitHub#
Parameters:

command – barman.fs.UnixLocalCommand

_abc_impl = <_abc._abc_data object>#
file_extension = 'tar.zst'#
get_file_content(filename, archive)View on GitHub#
Parameters:
  • filename – str file to search for in the archive (requires its full path within the archive)

  • archive – str archive path/name without extension

Returns:

string content

name = 'zstd'#
uncompress(src, dst, exclude=None, include_args=None)View on GitHub#
Parameters:
  • src – source file path without compression extension

  • dst – destination path

  • exclude – list of filepath in the archive to exclude from the extraction

  • include_args – list of filepath in the archive to extract.

Returns:

class barman.compression.ZSTDCompressor(config, compression, path=None)View on GitHub#

Bases: InternalCompressor

Predefined compressor with zstd

LEVEL_HIGH = 9#
LEVEL_LOW = 1#
LEVEL_MAX = 22#
LEVEL_MEDIUM = 4#
LEVEL_MIN = -22#
MAGIC = b'(\xb5/\xfd'#
__init__(config, compression, path=None)View on GitHub#

Constructor. :param config: barman.config.ServerConfig :param compression: str compression name :param path: str|None

_abc_impl = <_abc._abc_data object>#
_compressor(dst)View on GitHub#

Abstract compressor factory method

Parameters:

dst – destination file path

Returns:

a file-like writable compressor object

_decompressor(src)View on GitHub#

Abstract decompressor factory method

Parameters:

src – source file path

Returns:

a file-like readable decompressor object

compress_in_mem(fileobj)View on GitHub#

Compresses the given file-object in memory

Parameters:

fileobj – source file-object to be compressed

Returns:

a compressed file-object

Note

When implementing this method, the compressed file-object position must be set to 0 before returning it, as it is likely to be read again afterwards.

decompress_in_mem(fileobj)View on GitHub#

Decompresses the given file-object in memory

Parameters:

fileobj – source file-object to be decompressed

Returns:

a decompressed file-object

property zstdView on GitHub#
class barman.compression.ZSTDPgBaseBackupCompressionOption(pg_base_backup_config)View on GitHub#

Bases: PgBaseBackupCompressionOption

validate(pg_server_version, remote_status)View on GitHub#

Validate zstd-specific options.

Parameters:
  • int (pg_server_version) – the server for which the compression options should be validated.

  • remote_status (dict) – the status of the pg_basebackup command

Return List:

List of Issues (str) or empty list

barman.compression._try_import_lz4()View on GitHub#
barman.compression._try_import_snappy()View on GitHub#
barman.compression._try_import_zstd()View on GitHub#
barman.compression.get_internal_compressor(compression, compression_level=None)View on GitHub#

Get a barman.compression.InternalCompressor for the specified compression algorithm

Parameters:
  • compression (str) – a valid compression algorithm

  • str|int|None – a compression level for the specified algorithm

Returns:

the respective internal compressor

Return type:

barman.compression.InternalCompressor

Raises:

ValueError – if the compression received is unkown to Barman

barman.compression.get_pg_basebackup_compression(server)View on GitHub#

Factory method which returns an instantiated PgBaseBackupCompression subclass for the backup_compression option in config for the supplied server.

Parameters:

server (barman.server.Server) – the server for which the PgBaseBackupCompression should be constructed

:return GZipPgBaseBackupCompression

barman.compression.get_server_config_minimal(compression, compression_level)View on GitHub#

Returns a placeholder for a ServerConfig object with all compression parameters relevant to barman.compression.CompressionManager filled.

Parameters:
  • compression (str) – a valid compression algorithm option

  • str|int|None – a compression level for the specified algorithm

Returns:

a fake server config object

Return type:

SimpleNamespace