barman.recovery_executor module#

This module contains the methods necessary to perform a recovery

class barman.recovery_executor.Assertion(filename, line, key, value)#

Bases: tuple

_asdict()#

Return a new dict which maps field names to their values.

_field_defaults = {}#
_fields = ('filename', 'line', 'key', 'value')#
classmethod _make(iterable)#

Make a new Assertion object from a sequence or iterable

_replace(**kwds)#

Return a new Assertion object replacing specified fields with new values

filename#

Alias for field number 0

key#

Alias for field number 2

line#

Alias for field number 1

value#

Alias for field number 3

class barman.recovery_executor.ConfigIssueDetectionView on GitHub#

Bases: object

DANGEROUS_OPTIONS = ['data_directory', 'config_file', 'hba_file', 'ident_file', 'external_pid_file', 'ssl_cert_file', 'ssl_key_file', 'ssl_ca_file', 'ssl_crl_file', 'unix_socket_directory', 'unix_socket_directories', 'include', 'include_dir', 'include_if_exists']#
detect_issues(filename)View on GitHub#

This method looks for any possible issue with PostgreSQL location options such as data_directory, config_file, etc. It returns a dictionary with the dangerous options that have been found.

Parameters:

str (filename) – the Postgres configuration file

:return clashes [Assertion]

class barman.recovery_executor.ConfigurationFileMangelerView on GitHub#

Bases: object

OPTIONS_TO_MANGLE = {'archive_command': 'false', 'recovery_target': None, 'recovery_target_action': None, 'recovery_target_inclusive': None, 'recovery_target_lsn': None, 'recovery_target_name': None, 'recovery_target_time': None, 'recovery_target_timeline': None, 'recovery_target_xid': None}#
mangle_options(filename, backup_filename=None, append_lines=None)View on GitHub#

This method modifies the given PostgreSQL configuration file, commenting out the given settings, and adding the ones generated by Barman.

If backup_filename is passed, keep a backup copy.

Parameters:
  • filename – the PostgreSQL configuration file

  • backup_filename – config file backup copy. Default is None.

  • append_lines – Additional lines to add to the config file

:return [Assertion]

class barman.recovery_executor.IncrementalRecoveryExecutor(backup_manager)View on GitHub#

Bases: RemoteConfigRecoveryExecutor

Recovery executor for recovery of Postgres incremental backups.

This class implements the combine backup process as well as the recovery of the newly combined backup by reusing some of the logic from the RecoveryExecutor class.

__init__(backup_manager)View on GitHub#

Constructor

Parameters:

backup_manager (barman.backup.BackupManager) – the BackupManager owner of the executor

_backup_copy(backup_info, dest, tablespaces=None, remote_command=None, **kwargs)View on GitHub#

Perform the actual copy/move of the synthetic backup to destination

Parameters:
  • backup_info (barman.infofile.SyntheticBackupInfo) – the synthetic backup info file

  • dest (str) – the destination directory

  • tablespaces (dict[str,str]|None) – a tablespace name -> location map (for relocation)

  • remote_command (str|None) – default None. The remote command to recover the backup, in case of remote backup

_combine_backups(backup_info, dest)View on GitHub#

Combines the backup chain into a single synthetic backup using the pg_combinebackup utility.

Parameters:
  • backup_info (barman.infofile.LocalBackupInfo) – the incremental backup to be recovered

  • dest (str) – the directory where the synthetic backup is going to be mounted on

Return barman.infofile.SyntheticBackupInfo:

the backup info file of the combined backup

_end_message(backup_info)View on GitHub#
_fetch_remote_status()View on GitHub#

Gather info from the remote server.

This method does not raise any exception in case of errors, but set the missing values to None in the resulting dictionary.

Return dict[str, str|bool]:

the pg_combinebackup client information of the remote server.

_get_backup_chain_paths(backup_info)View on GitHub#

Get the path of each backup in the chain, from the full backup to the specified incremental backup.

Parameters:

backup_info (barman.infofile.LocalBackupInfo) – The incremental backup

Return Iterator[barman.infofile.LocalBackupInfo]:

iterator of paths of the backups in the chain, going from the full to the incremental backup pointed by backup_info

_move_to_destination(source, destination, exclude_path_names={})View on GitHub#

Move all files and directories contained within source to destination.

Parameters:
  • source (str) – the source directory path from which underlying files and directories will be moved

  • destination (str) – the destination directory path where to move the files and directories contained within source

  • exclude_path_names (set[str]) – name of directories or files to be excluded from the moving action.

_prepare_destination(dest_dir)View on GitHub#

Prepare the destination directory or file before moving it.

This method is responsible for removing a directory if it already exists, then (re)creating it and ensuring the correct permissions on the directory.

Parameters:

dest_dir (str) – destination directory

_retry_handler(dest_dirs, attempt)View on GitHub#

Handler invoked during a combine backup in case of retry.

The method simply warn the user of the failure and remove the already existing directories of the backup.

Parameters:
  • dest_dirs (list[str]) – destination directories

  • attempt (int) – attempt number (starting from 0)

_start_message(backup_info)View on GitHub#
recover(backup_info, dest, remote_command=None, **kwargs)View on GitHub#

Performs the recovery of an incremental backup.

It first combines all backups in the backup chain, full to incremental, then proceeds with the recovery of the generated synthetic backup.

This method should be called in a contextlib.closing() context.

Parameters:
  • backup_info (barman.infofile.BackupInfo) – the incremental backup to recover

  • dest (str) – the destination directory

  • remote_command (str|None) – The remote command to recover the base backup, in case of remote backup.

Return dict:

recovery_info dictionary, holding the values related with the recovery process.

class barman.recovery_executor.RecoveryExecutor(backup_manager)View on GitHub#

Bases: object

Class responsible of recovery operations

__init__(backup_manager)View on GitHub#

Constructor

Parameters:

backup_manager (barman.backup.BackupManager) – the BackupManager owner of the executor

_analyse_temporary_config_files(recovery_info)View on GitHub#

Analyse temporary configuration files and identify dangerous options

Mark all the dangerous options for the user to review. This procedure also changes harmful options such as ‘archive_command’.

Parameters:

recovery_info (dict) – dictionary holding all recovery parameters

_backup_copy(backup_info, dest, tablespaces=None, remote_command=None, safe_horizon=None, recovery_info=None)View on GitHub#

Perform the actual copy of the base backup for recovery purposes

First, it copies one tablespace at a time, then the PGDATA directory.

Bandwidth limitation, according to configuration, is applied in the process.

TODO: manage configuration files if outside PGDATA.

Parameters:
  • backup_info (barman.infofile.LocalBackupInfo) – the backup to recover

  • dest (str) – the destination directory

  • tablespaces (dict[str,str]|None) – a tablespace name -> location map (for relocation)

  • remote_command (str|None) – default None. The remote command to recover the base backup, in case of remote backup.

  • safe_horizon (datetime.datetime|None) – anything after this time has to be checked with checksum

_backup_copy_failure_message(e)View on GitHub#

Write the backup failure message to the output.

_conf_files_exist(conf_files, backup_info, recovery_info)View on GitHub#

Determine whether the conf files in the supplied list exist in the backup represented by backup_info.

Returns a map of conf_file:exists.

_copy_conf_files_to_tempdir(backup_info, recovery_info, remote_command=None)View on GitHub#

Copy conf files from the backup location to a temporary directory so that they can be checked and mangled.

Returns a list of the paths to the temporary conf files.

_copy_temporary_config_files(dest, remote_command, recovery_info)View on GitHub#

Copy modified configuration files using rsync in case of remote recovery

Parameters:
  • dest (str) – destination directory of the recovery

  • remote_command (str) – ssh command for remote connection

  • recovery_info (dict) – Dictionary containing all the recovery parameters

_generate_archive_status(recovery_info, remote_command, required_xlog_files)View on GitHub#

Populate the archive_status directory

Parameters:
  • recovery_info (dict) – Dictionary containing all the recovery parameters

  • remote_command (str) – ssh command for remote connection

  • required_xlog_files (tuple) – list of required WAL segments

_generate_recovery_conf(recovery_info, backup_info, dest, immediate, exclusive, remote_command, target_name, target_time, target_tli, target_xid, target_lsn, standby_mode)View on GitHub#

Generate recovery configuration for PITR

Parameters:
  • recovery_info (dict) – Dictionary containing all the recovery parameters

  • backup_info (barman.infofile.LocalBackupInfo) – representation of a backup

  • dest (str) – destination directory of the recovery

  • immediate (bool|None) – end recovery as soon as consistency is reached

  • exclusive (boolean) – exclusive backup or concurrent

  • remote_command (str) – ssh command for remote connection

  • target_name (str) – recovery target name for PITR

  • target_time (str) – recovery target time for PITR

  • target_tli (str) – recovery target timeline for PITR

  • target_xid (str) – recovery target transaction id for PITR

  • target_lsn (str) – recovery target LSN for PITR

  • standby_mode (bool|None) – standby mode

_map_temporary_config_files(recovery_info, backup_info, remote_command)View on GitHub#

Map configuration files, by filling the ‘temporary_configuration_files’ array, depending on remote or local recovery. This array will be used by the subsequent methods of the class.

Parameters:
  • recovery_info (dict) – Dictionary containing all the recovery parameters

  • backup_info (barman.infofile.LocalBackupInfo) – a backup representation

  • remote_command (str) – ssh command for remote recovery

_prepare_tablespaces(backup_info, cmd, dest, tablespaces)View on GitHub#

Prepare the directory structure for required tablespaces, taking care of tablespaces relocation, if requested.

Parameters:
_retrieve_safe_horizon(recovery_info, backup_info, dest)View on GitHub#

Retrieve the safe_horizon for smart copy

If the target directory contains a previous recovery, it is safe to pick the least of the two backup “begin times” (the one we are recovering now and the one previously recovered in the target directory). Set the value in the given recovery_info dictionary.

Parameters:
  • recovery_info (dict) – Dictionary containing all the recovery parameters

  • backup_info (barman.infofile.LocalBackupInfo) – a backup representation

  • dest (str) – recovery destination directory

_set_pitr_targets(recovery_info, backup_info, dest, target_name, target_time, target_tli, target_xid, target_lsn, target_immediate, target_action)View on GitHub#

Set PITR targets - as specified by the user

Parameters:
  • recovery_info (dict) – Dictionary containing all the recovery parameters

  • backup_info (barman.infofile.LocalBackupInfo) – representation of a backup

  • dest (str) – destination directory of the recovery

  • target_name (str|None) – recovery target name for PITR

  • target_time (str|None) – recovery target time for PITR

  • target_tli (str|None) – recovery target timeline for PITR

  • target_xid (str|None) – recovery target transaction id for PITR

  • target_lsn (str|None) – recovery target LSN for PITR

  • target_immediate (bool|None) – end recovery as soon as consistency is reached

  • target_action (str|None) – recovery target action for PITR

_setup(backup_info, remote_command, dest, recovery_conf_filename)View on GitHub#

Prepare the recovery_info dictionary for the recovery, as well as temporary working directory

Parameters:
  • backup_info (barman.infofile.LocalBackupInfo) – representation of a backup

  • remote_command (str) – ssh command for remote connection

  • recovery_conf_filename (str|None) – filename for storing recovery configurations

Return dict:

recovery_info dictionary, holding the basic values for a recovery

_start_backup_copy_message()View on GitHub#

Write the start backup copy message to the output.

_xlog_copy(required_xlog_files, wal_dest, remote_command)View on GitHub#

Restore WAL segments

Parameters:
  • required_xlog_files – list of all required WAL files

  • wal_dest – the destination directory for xlog recover

  • remote_command – default None. The remote command to recover the xlog, in case of remote backup.

close()View on GitHub#

Cleanup operations for a recovery

recover(backup_info, dest, tablespaces=None, remote_command=None, target_tli=None, target_time=None, target_xid=None, target_lsn=None, target_name=None, target_immediate=False, exclusive=False, target_action=None, standby_mode=None, recovery_conf_filename=None)View on GitHub#

Performs a recovery of a backup

This method should be called in a closing context

Parameters:
  • backup_info (barman.infofile.BackupInfo) – the backup to recover

  • dest (str) – the destination directory

  • tablespaces (dict[str,str]|None) – a tablespace name -> location map (for relocation)

  • remote_command (str|None) – The remote command to recover the base backup, in case of remote backup.

  • target_tli (str|None) – the target timeline

  • target_time (str|None) – the target time

  • target_xid (str|None) – the target xid

  • target_lsn (str|None) – the target LSN

  • target_name (str|None) – the target name created previously with pg_create_restore_point() function call

  • target_immediate (str|None) – end recovery as soon as consistency is reached

  • exclusive (bool) – whether the recovery is exclusive or not

  • target_action (str|None) – The recovery target action

  • standby_mode (bool|None) – standby mode

  • recovery_conf_filename (str|None) – filename for storing recovery configurations

class barman.recovery_executor.RemoteConfigRecoveryExecutor(backup_manager)View on GitHub#

Bases: RecoveryExecutor

Recovery executor which retrieves config files from the recovery directory instead of the backup directory. Useful when the config files are not available in the backup directory (e.g. compressed backups).

_conf_files_exist(conf_files, backup_info, recovery_info)View on GitHub#

Determine whether the conf files in the supplied list exist in the backup represented by backup_info.

Parameters:
  • conf_files (list[str]) – List of config files to be checked.

  • backup_info (BackupInfo) – Backup information for the backup being recovered.

  • recovery_info (dict) – Dictionary of recovery information.

Return type:

dict[str,bool]

Returns:

A dict representing a map of conf_file:exists.

_copy_conf_files_to_tempdir(backup_info, recovery_info, remote_command=None)View on GitHub#

Copy conf files from the backup location to a temporary directory so that they can be checked and mangled.

Parameters:
  • backup_info (BackupInfo) – Backup information for the backup being recovered.

  • recovery_info (dict) – Dictionary of recovery information.

  • remote_command (str) – The ssh command to be used when copying the files.

Return type:

list[str]

Returns:

A list of paths to the destination conf files.

class barman.recovery_executor.SnapshotRecoveryExecutor(backup_manager)View on GitHub#

Bases: RemoteConfigRecoveryExecutor

Recovery executor which performs barman recovery tasks for a backup taken with backup_method snapshot.

It is responsible for:

  • Checking that disks cloned from the snapshots in the backup are attached to the recovery instance and that they are mounted at the correct location with the expected options.

  • Copying the backup_label into place.

  • Applying the requested recovery options to the PostgreSQL configuration.

It does not handle the creation of the recovery instance, the creation of new disks from the snapshots or the attachment of the disks to the recovery instance. These are expected to have been performed before the barman recover runs.

_backup_copy(backup_info, dest, remote_command=None, **kwargs)View on GitHub#

Copy any files from the backup directory which are required by the snapshot recovery (currently only the backup_label).

Parameters:
_backup_copy_failure_message(e)View on GitHub#

Write the backup failure message to the output.

_prepare_tablespaces(backup_info, cmd, dest, tablespaces)View on GitHub#

There is no need to prepare tablespace directories because they will already be present on the recovery instance through the cloning of disks from the backup snapshots.

This function is therefore a no-op.

_start_backup_copy_message()View on GitHub#

Write the start backup copy message to the output.

static check_mount_points(backup_info, attached_volumes, cmd)View on GitHub#

Check that each disk cloned from a snapshot is mounted at the same mount point as the original disk and with the same mount options.

Raises a RecoveryPreconditionException if any of the devices supplied in attached_snapshots are not mounted at the mount point or with the mount options specified in the snapshot metadata.

Parameters:
static check_recovery_dir_exists(recovery_dir, cmd)View on GitHub#

Verify that the recovery directory already exists.

Parameters:
  • recovery_dir (str) – Path to the recovery directory on the recovery instance

  • cmd (UnixLocalCommand) – The command wrapper for running commands on the recovery instance.

static get_attached_volumes_for_backup(snapshot_interface, backup_info, instance_name)View on GitHub#

Verifies that disks cloned from the snapshots specified in the supplied backup_info are attached to the named instance and returns them as a dict where the keys are snapshot names and the values are the names of the attached devices.

If any snapshot associated with this backup is not found as the source for any disk attached to the instance then a RecoveryPreconditionException is raised.

Parameters:
  • snapshot_interface (CloudSnapshotInterface) – Interface for managing snapshots via a cloud provider API.

  • backup_info (BackupInfo) – Backup information for the backup being recovered.

  • instance_name (str) – The name of the VM instance to which the disks to be backed up are attached.

Return type:

dict[str,str]

Returns:

A dict where the key is the snapshot name and the value is the device path for the source disk for that snapshot on the specified instance.

recover(backup_info, dest, tablespaces=None, remote_command=None, target_tli=None, target_time=None, target_xid=None, target_lsn=None, target_name=None, target_immediate=False, exclusive=False, target_action=None, standby_mode=None, recovery_conf_filename=None, recovery_instance=None)View on GitHub#

Performs a recovery of a snapshot backup.

This method should be called in a closing context.

Parameters:
  • backup_info (barman.infofile.BackupInfo) – the backup to recover

  • dest (str) – the destination directory

  • tablespaces (dict[str,str]|None) – a tablespace name -> location map (for relocation)

  • remote_command (str|None) – The remote command to recover the base backup, in case of remote backup.

  • target_tli (str|None) – the target timeline

  • target_time (str|None) – the target time

  • target_xid (str|None) – the target xid

  • target_lsn (str|None) – the target LSN

  • target_name (str|None) – the target name created previously with pg_create_restore_point() function call

  • target_immediate (str|None) – end recovery as soon as consistency is reached

  • exclusive (bool) – whether the recovery is exclusive or not

  • target_action (str|None) – The recovery target action

  • standby_mode (bool|None) – standby mode

  • recovery_conf_filename (str|None) – filename for storing recovery configurations

  • recovery_instance (str|None) – The name of the recovery node as it is known by the cloud provider

class barman.recovery_executor.TarballRecoveryExecutor(backup_manager, compression)View on GitHub#

Bases: RemoteConfigRecoveryExecutor

A specialised recovery method for compressed backups. Inheritence is not necessarily the best thing here since the two RecoveryExecutor classes only differ by this one method, and the same will be true for future RecoveryExecutors (i.e. ones which handle encryption). Nevertheless for a wip “make it work” effort this will do.

BASE_TARBALL_NAME = 'base'#
__init__(backup_manager, compression)View on GitHub#

Constructor

Parameters:

backup_manager (barman.backup.BackupManager) – the BackupManager owner of the executor

:param compression Compression.

_backup_copy(backup_info, dest, tablespaces=None, remote_command=None, safe_horizon=None, recovery_info=None)View on GitHub#

Perform the actual copy of the base backup for recovery purposes

First, it copies one tablespace at a time, then the PGDATA directory.

Bandwidth limitation, according to configuration, is applied in the process.

TODO: manage configuration files if outside PGDATA.

Parameters:
  • backup_info (barman.infofile.LocalBackupInfo) – the backup to recover

  • dest (str) – the destination directory

  • tablespaces (dict[str,str]|None) – a tablespace name -> location map (for relocation)

  • remote_command (str|None) – default None. The remote command to recover the base backup, in case of remote backup.

  • safe_horizon (datetime.datetime|None) – anything after this time has to be checked with checksum

barman.recovery_executor.recovery_executor_factory(backup_manager, command, backup_info)View on GitHub#

Method in charge of building adequate RecoveryExecutor depending on the context :param: backup_manager :param: command barman.fs.UnixLocalCommand :return: RecoveryExecutor instance