本文整理汇总了Python中barman.recovery_executor.RecoveryExecutor类的典型用法代码示例。如果您正苦于以下问题:Python RecoveryExecutor类的具体用法?Python RecoveryExecutor怎么用?Python RecoveryExecutor使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了RecoveryExecutor类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_recover_basebackup_copy
def test_recover_basebackup_copy(self, rsync_pg_mock, tmpdir):
"""
Test the copy of a content of a backup during a recovery
:param rsync_pg_mock: Mock rsync object for the purpose if this test
"""
# Build basic folder/files structure
dest = tmpdir.mkdir('destination')
server = testing_helpers.build_real_server()
backup_info = testing_helpers.build_test_backup_info(
server=server,
tablespaces=[('tbs1', 16387, '/fake/location')])
# Build a executor
executor = RecoveryExecutor(server.backup_manager)
executor.config.tablespace_bandwidth_limit = {'tbs1': ''}
executor.config.bandwidth_limit = 10
executor.basebackup_copy(
backup_info, dest.strpath, tablespaces=None)
rsync_pg_mock.assert_called_with(
network_compression=False, bwlimit=10, ssh=None, path=None,
exclude_and_protect=['/pg_tblspc/16387'])
rsync_pg_mock.assert_any_call(
network_compression=False, bwlimit='', ssh=None, path=None,
check=True)
rsync_pg_mock.return_value.smart_copy.assert_any_call(
'/some/barman/home/main/base/1234567890/16387/',
'/fake/location', None)
rsync_pg_mock.return_value.smart_copy.assert_called_with(
'/some/barman/home/main/base/1234567890/data/',
dest.strpath, None)
示例2: recover
def recover(self, backup_info, dest, tablespaces=None, target_tli=None,
target_time=None, target_xid=None, target_name=None,
exclusive=False, remote_command=None):
"""
Performs a recovery of a backup
:param barman.infofile.BackupInfo backup_info: the backup to recover
:param str dest: the destination directory
:param dict[str,str]|None tablespaces: a tablespace name -> location map
(for relocation)
:param str|None target_tli: the target timeline
:param str|None target_time: the target time
:param str|None target_xid: the target xid
:param str|None target_name: the target name created previously with
pg_create_restore_point() function call
:param bool exclusive: whether the recovery is exclusive or not
:param str|None remote_command: default None. The remote command to recover
the base backup, in case of remote backup.
"""
# Archive every WAL files in the incoming directory of the server
self.server.archive_wal(verbose=False)
# Delegate the recovery operation to a RecoveryExecutor object
executor = RecoveryExecutor(self)
recovery_info = executor.recover(backup_info,
dest, tablespaces,
target_tli, target_time,
target_xid, target_name,
exclusive, remote_command)
# Output recovery results
output.result('recovery', recovery_info['results'])
示例3: test_set_pitr_targets
def test_set_pitr_targets(self, tmpdir):
"""
Evaluate targets for point in time recovery
"""
# Build basic folder/files structure
tempdir = tmpdir.mkdir("temp_dir")
dest = tmpdir.mkdir("dest")
wal_dest = tmpdir.mkdir("wal_dest")
recovery_info = {
"configuration_files": ["postgresql.conf", "postgresql.auto.conf"],
"tempdir": tempdir.strpath,
"results": {"changes": [], "warnings": []},
"is_pitr": False,
"wal_dest": wal_dest.strpath,
"get_wal": False,
}
backup_info = testing_helpers.build_test_backup_info()
backup_manager = testing_helpers.build_backup_manager()
# Build a recovery executor
executor = RecoveryExecutor(backup_manager)
executor._set_pitr_targets(recovery_info, backup_info, dest.strpath, "", "", "", "")
# Test with empty values (no PITR)
assert recovery_info["target_epoch"] is None
assert recovery_info["target_datetime"] is None
assert recovery_info["wal_dest"] == wal_dest.strpath
# Test for PITR targets
executor._set_pitr_targets(
recovery_info, backup_info, dest.strpath, "target_name", "2015-06-03 16:11:03.71038+02", "2", None
)
target_datetime = dateutil.parser.parse("2015-06-03 16:11:03.710380+02:00")
target_epoch = time.mktime(target_datetime.timetuple()) + (target_datetime.microsecond / 1000000.0)
assert recovery_info["target_datetime"] == target_datetime
assert recovery_info["target_epoch"] == target_epoch
assert recovery_info["wal_dest"] == dest.join("barman_xlog").strpath
示例4: test_map_temporary_config_files
def test_map_temporary_config_files(self, tmpdir):
"""
Test the method that prepares configuration files
for the final steps of a recovery
"""
# Build directory/files structure for testing
tempdir = tmpdir.mkdir("tempdir")
recovery_info = {
"configuration_files": ["postgresql.conf", "postgresql.auto.conf"],
"tempdir": tempdir.strpath,
"temporary_configuration_files": [],
"results": {"changes": [], "warnings": [], "missing_files": []},
}
backup_info = testing_helpers.build_test_backup_info()
backup_info.config.basebackups_directory = tmpdir.strpath
datadir = tmpdir.mkdir(backup_info.backup_id).mkdir("data")
postgresql_conf_local = datadir.join("postgresql.conf")
postgresql_auto_local = datadir.join("postgresql.auto.conf")
postgresql_conf_local.write("archive_command = something\n" "data_directory = something")
postgresql_auto_local.write("archive_command = something\n" "data_directory = something")
# Build a RecoveryExecutor object (using a mock as server and backup
# manager.
backup_manager = testing_helpers.build_backup_manager()
executor = RecoveryExecutor(backup_manager)
executor._map_temporary_config_files(recovery_info, backup_info, "[email protected]")
# check that configuration files have been moved by the method
assert tempdir.join("postgresql.conf").check()
assert tempdir.join("postgresql.conf").computehash() == postgresql_conf_local.computehash()
assert tempdir.join("postgresql.auto.conf").check()
assert tempdir.join("postgresql.auto.conf").computehash() == postgresql_auto_local.computehash()
assert recovery_info["results"]["missing_files"] == ["pg_hba.conf", "pg_ident.conf"]
示例5: test_prepare_tablespaces
def test_prepare_tablespaces(self, tmpdir):
"""
Test tablespaces preparation for recovery
"""
# Prepare basic directory/files structure
dest = tmpdir.mkdir('destination')
wals = tmpdir.mkdir('wals')
backup_info = testing_helpers.build_test_backup_info(
tablespaces=[('tbs1', 16387, '/fake/location')])
# build an executor
server = testing_helpers.build_real_server(
main_conf={'wals_directory': wals.strpath})
executor = RecoveryExecutor(server.backup_manager)
# use a mock as cmd obj
cmd_mock = Mock()
executor.prepare_tablespaces(backup_info, cmd_mock, dest.strpath, {})
cmd_mock.create_dir_if_not_exists.assert_any_call(
dest.join('pg_tblspc').strpath)
cmd_mock.create_dir_if_not_exists.assert_any_call(
'/fake/location')
cmd_mock.delete_if_exists.assert_called_once_with(
dest.join('pg_tblspc').join('16387').strpath)
cmd_mock.create_symbolic_link.assert_called_once_with(
'/fake/location',
dest.join('pg_tblspc').join('16387').strpath)
示例6: test_recover_xlog
def test_recover_xlog(self, rsync_pg_mock, tmpdir):
"""
Test the recovery of the xlogs of a backup
:param rsync_pg_mock: Mock rsync object for the purpose if this test
"""
# Build basic folders/files structure
dest = tmpdir.mkdir('destination')
wals = tmpdir.mkdir('wals')
xlog_dir = wals.mkdir(xlog.hash_dir('000000000000000000000002'))
xlog_file = xlog_dir.join('000000000000000000000002')
xlog_file.write('dummy content')
server = testing_helpers.build_real_server(
main_conf={'wals_directory': wals.strpath})
# build executor
executor = RecoveryExecutor(server.backup_manager)
required_wals = (WalFileInfo.from_xlogdb_line(
'000000000000000000000002\t42\t43\tNone\n'),)
executor.xlog_copy(required_wals, dest.strpath, None)
# check for a correct invocation of rsync using local paths
rsync_pg_mock.from_file_list.assert_called_once(
['000000000000000000000002'],
xlog_dir.strpath,
dest.strpath)
# reset mock calls
rsync_pg_mock.reset_mock()
required_wals = (WalFileInfo.from_xlogdb_line(
'000000000000000000000002\t42\t43\tNone\n'),)
executor.backup_manager.compression_manager = Mock()
executor.xlog_copy(required_wals, dest.strpath, 'remote_command')
# check for the invocation of rsync on a remote call
rsync_pg_mock.assert_called_once(network_compression=False,
bwlimit=None,
ssh='remote_command')
示例7: test_recover_xlog
def test_recover_xlog(self, rsync_pg_mock, cm_mock, tmpdir):
"""
Test the recovery of the xlogs of a backup
:param rsync_pg_mock: Mock rsync object for the purpose if this test
"""
# Build basic folders/files structure
dest = tmpdir.mkdir("destination")
wals = tmpdir.mkdir("wals")
# Create 3 WAL files with different compressions
xlog_dir = wals.mkdir(xlog.hash_dir("000000000000000000000002"))
xlog_plain = xlog_dir.join("000000000000000000000001")
xlog_gz = xlog_dir.join("000000000000000000000002")
xlog_bz2 = xlog_dir.join("000000000000000000000003")
xlog_plain.write("dummy content")
xlog_gz.write("dummy content gz")
xlog_bz2.write("dummy content bz2")
server = testing_helpers.build_real_server(main_conf={"wals_directory": wals.strpath})
# Prepare compressors mock
c = {"gzip": mock.Mock(name="gzip"), "bzip2": mock.Mock(name="bzip2")}
cm_mock.return_value.get_compressor = lambda compression=None, path=None: c[compression]
# touch destination files to avoid errors on cleanup
c["gzip"].decompress.side_effect = lambda src, dst: open(dst, "w")
c["bzip2"].decompress.side_effect = lambda src, dst: open(dst, "w")
# Build executor
executor = RecoveryExecutor(server.backup_manager)
# Test: local copy
required_wals = (
WalFileInfo.from_xlogdb_line("000000000000000000000001\t42\t43\tNone\n"),
WalFileInfo.from_xlogdb_line("000000000000000000000002\t42\t43\tgzip\n"),
WalFileInfo.from_xlogdb_line("000000000000000000000003\t42\t43\tbzip2\n"),
)
executor._xlog_copy(required_wals, dest.strpath, None)
# Check for a correct invocation of rsync using local paths
rsync_pg_mock.assert_called_once_with(network_compression=False, bwlimit=None, path=None, ssh=None)
assert not rsync_pg_mock.return_value.from_file_list.called
c["gzip"].decompress.assert_called_once_with(xlog_gz.strpath, mock.ANY)
c["bzip2"].decompress.assert_called_once_with(xlog_bz2.strpath, mock.ANY)
# Reset mock calls
rsync_pg_mock.reset_mock()
c["gzip"].reset_mock()
c["bzip2"].reset_mock()
# Test: remote copy
executor._xlog_copy(required_wals, dest.strpath, "remote_command")
# Check for the invocation of rsync on a remote call
rsync_pg_mock.assert_called_once_with(
network_compression=False, bwlimit=None, path=mock.ANY, ssh="remote_command"
)
rsync_pg_mock.return_value.from_file_list.assert_called_once_with(
["000000000000000000000001", "000000000000000000000002", "000000000000000000000003"], mock.ANY, mock.ANY
)
c["gzip"].decompress.assert_called_once_with(xlog_gz.strpath, mock.ANY)
c["bzip2"].decompress.assert_called_once_with(xlog_bz2.strpath, mock.ANY)
示例8: test_analyse_temporary_config_files
def test_analyse_temporary_config_files(self, tmpdir):
"""
Test the method that identifies dangerous options into the configuration
files
"""
# Build directory/files structure for testing
tempdir = tmpdir.mkdir('tempdir')
recovery_info = {
'configuration_files': ['postgresql.conf', 'postgresql.auto.conf'],
'tempdir': tempdir.strpath,
'temporary_configuration_files': [],
'results': {'changes': [], 'warnings': []}
}
postgresql_conf = tempdir.join('postgresql.conf')
postgresql_auto = tempdir.join('postgresql.auto.conf')
postgresql_conf.write('archive_command = something\n'
'data_directory = something')
postgresql_auto.write('archive_command = something\n'
'data_directory = something')
local_rec = tmpdir.mkdir('local_rec')
recovery_info['temporary_configuration_files'].append(
postgresql_conf.strpath)
recovery_info['temporary_configuration_files'].append(
postgresql_auto.strpath)
# Build a RecoveryExecutor object (using a mock as server and backup
# manager.
server = testing_helpers.build_mocked_server()
backup_manager = Mock(server=server, config=server.config)
executor = RecoveryExecutor(backup_manager)
# Identify dangerous options into config files for remote recovery
executor.analyse_temporary_config_files(recovery_info)
assert len(recovery_info['results']['changes']) == 2
assert len(recovery_info['results']['warnings']) == 2
# Prepare for a local recovery test
recovery_info['results']['changes'] = []
recovery_info['results']['warnings'] = []
postgresql_conf_local = local_rec.join('postgresql.conf')
postgresql_auto_local = local_rec.join('postgresql.auto.conf')
postgresql_conf_local.write('archive_command = something\n'
'data_directory = something')
postgresql_auto_local.write('archive_command = something\n'
'data_directory = something')
# Identify dangerous options for local recovery
executor.analyse_temporary_config_files(recovery_info)
assert len(recovery_info['results']['changes']) == 2
assert len(recovery_info['results']['warnings']) == 2
示例9: test_generate_recovery_conf
def test_generate_recovery_conf(self, rsync_pg_mock, tmpdir):
"""
Test the generation of recovery.conf file
"""
# Build basic folder/files structure
recovery_info = {
"configuration_files": ["postgresql.conf", "postgresql.auto.conf"],
"tempdir": tmpdir.strpath,
"results": {"changes": [], "warnings": []},
"get_wal": False,
}
backup_info = testing_helpers.build_test_backup_info()
dest = tmpdir.mkdir("destination")
# Build a recovery executor using a real server
server = testing_helpers.build_real_server()
executor = RecoveryExecutor(server.backup_manager)
executor._generate_recovery_conf(
recovery_info,
backup_info,
dest.strpath,
True,
"[email protected]",
"target_name",
"2015-06-03 16:11:03.71038+02",
"2",
"",
)
# Check that the recovery.conf file exists
recovery_conf_file = tmpdir.join("recovery.conf")
assert recovery_conf_file.check()
# Parse the generated recovery.conf
recovery_conf = {}
for line in recovery_conf_file.readlines():
key, value = (s.strip() for s in line.strip().split("=", 1))
recovery_conf[key] = value
# check for contents
assert "recovery_end_command" in recovery_conf
assert "recovery_target_time" in recovery_conf
assert "recovery_target_timeline" in recovery_conf
assert "recovery_target_xid" not in recovery_conf
assert "recovery_target_name" in recovery_conf
assert recovery_conf["recovery_end_command"] == "'rm -fr barman_xlog'"
assert recovery_conf["recovery_target_time"] == "'2015-06-03 16:11:03.71038+02'"
assert recovery_conf["recovery_target_timeline"] == "2"
assert recovery_conf["recovery_target_name"] == "'target_name'"
示例10: test_set_pitr_targets
def test_set_pitr_targets(self, tmpdir):
"""
Evaluate targets for point in time recovery
"""
# Build basic folder/files structure
tempdir = tmpdir.mkdir('temp_dir')
dest = tmpdir.mkdir('dest')
wal_dest = tmpdir.mkdir('wal_dest')
recovery_info = {
'configuration_files': ['postgresql.conf', 'postgresql.auto.conf'],
'tempdir': tempdir.strpath,
'results': {'changes': [], 'warnings': []},
'is_pitr': False,
'wal_dest': wal_dest.strpath,
'get_wal': False,
}
backup_info = testing_helpers.build_test_backup_info()
server = testing_helpers.build_mocked_server()
backup_manager = Mock(server=server, config=server.config)
# Build a recovery executor
executor = RecoveryExecutor(backup_manager)
executor.set_pitr_targets(recovery_info, backup_info,
dest.strpath,
'', '', '', '')
# Test with empty values (no PITR)
assert recovery_info['target_epoch'] is None
assert recovery_info['target_datetime'] is None
assert recovery_info['wal_dest'] == wal_dest.strpath
# Test for PITR targets
executor.set_pitr_targets(recovery_info, backup_info,
dest.strpath,
'target_name',
'2015-06-03 16:11:03.71038+02',
'2',
None,)
target_datetime = dateutil.parser.parse(
'2015-06-03 16:11:03.710380+02:00')
target_epoch = (time.mktime(target_datetime.timetuple()) +
(target_datetime.microsecond / 1000000.))
assert recovery_info['target_datetime'] == target_datetime
assert recovery_info['target_epoch'] == target_epoch
assert recovery_info['wal_dest'] == dest.join('barman_xlog').strpath
示例11: test_setup
def test_setup(self):
"""
Test the method that set up a recovery
"""
backup_info = testing_helpers.build_test_backup_info()
server = testing_helpers.build_mocked_server()
backup_manager = Mock(server=server, config=server.config)
executor = RecoveryExecutor(backup_manager)
backup_info.version = 90300
# no postgresql.auto.conf on version 9.3
ret = executor.setup(backup_info, None, "/tmp")
assert "postgresql.auto.conf" not in ret['configuration_files']
# Check the present for postgresql.auto.conf on version 9.4
backup_info.version = 90400
ret = executor.setup(backup_info, None, "/tmp")
assert "postgresql.auto.conf" in ret['configuration_files']
# Receive a error if the remote command is invalid
with pytest.raises(SystemExit):
executor.setup(backup_info, "invalid", "/tmp")
示例12: test_generate_recovery_conf
def test_generate_recovery_conf(self, rsync_pg_mock, tmpdir):
"""
Test the generation of recovery.conf file
"""
# Build basic folder/files structure
recovery_info = {
'configuration_files': ['postgresql.conf', 'postgresql.auto.conf'],
'tempdir': tmpdir.strpath,
'results': {'changes': [], 'warnings': []},
'get_wal': False,
}
backup_info = testing_helpers.build_test_backup_info()
dest = tmpdir.mkdir('destination')
# Build a recovery executor using a real server
server = testing_helpers.build_real_server()
executor = RecoveryExecutor(server.backup_manager)
executor.generate_recovery_conf(recovery_info, backup_info,
dest.strpath,
True, '[email protected]',
'target_name',
'2015-06-03 16:11:03.71038+02', '2',
'')
# Check that the recovery.conf file exists
recovery_conf_file = tmpdir.join("recovery.conf")
assert recovery_conf_file.check()
# Parse the generated recovery.conf
recovery_conf = {}
for line in recovery_conf_file.readlines():
key, value = (s.strip() for s in line.strip().split('=', 1))
recovery_conf[key] = value
# check for contents
assert 'recovery_end_command' in recovery_conf
assert 'recovery_target_time' in recovery_conf
assert 'recovery_target_timeline' in recovery_conf
assert 'recovery_target_xid' not in recovery_conf
assert 'recovery_target_name' in recovery_conf
assert recovery_conf['recovery_end_command'] == "'rm -fr barman_xlog'"
assert recovery_conf['recovery_target_time'] == \
"'2015-06-03 16:11:03.71038+02'"
assert recovery_conf['recovery_target_timeline'] == '2'
assert recovery_conf['recovery_target_name'] == "'target_name'"
示例13: test_recover_backup_copy
def test_recover_backup_copy(self, copy_controller_mock, tmpdir):
"""
Test the copy of a content of a backup during a recovery
"""
# Build basic folder/files structure
dest = tmpdir.mkdir("destination")
server = testing_helpers.build_real_server()
backup_info = testing_helpers.build_test_backup_info(
server=server, tablespaces=[("tbs1", 16387, "/fake/location")]
)
# Build a executor
executor = RecoveryExecutor(server.backup_manager)
executor.config.tablespace_bandwidth_limit = {"tbs1": ""}
executor.config.bandwidth_limit = 10
executor._backup_copy(backup_info, dest.strpath, tablespaces=None)
# Check the calls
assert copy_controller_mock.mock_calls == [
mock.call(
network_compression=False, path=None, safe_horizon=None, ssh_command=None, retry_sleep=30, retry_times=0
),
mock.call().add_directory(
bwlimit="",
dst="/fake/location",
item_class=copy_controller_mock.return_value.TABLESPACE_CLASS,
label="tbs1",
src=backup_info.get_data_directory(16387) + "/",
),
mock.call().add_directory(
bwlimit=10,
dst=dest.strpath,
exclude=["/pg_xlog/*", "/pg_log/*", "/recovery.conf", "/postmaster.pid"],
exclude_and_protect=["/pg_tblspc/16387"],
item_class=copy_controller_mock.return_value.PGDATA_CLASS,
label="pgdata",
src=backup_info.get_data_directory() + "/",
),
mock.call().copy(),
]
示例14: test_analyse_temporary_config_files
def test_analyse_temporary_config_files(self, tmpdir):
"""
Test the method that identifies dangerous options into
the configuration files
"""
# Build directory/files structure for testing
tempdir = tmpdir.mkdir("tempdir")
recovery_info = {
"configuration_files": ["postgresql.conf", "postgresql.auto.conf"],
"tempdir": tempdir.strpath,
"temporary_configuration_files": [],
"results": {"changes": [], "warnings": []},
}
postgresql_conf = tempdir.join("postgresql.conf")
postgresql_auto = tempdir.join("postgresql.auto.conf")
postgresql_conf.write(
"archive_command = something\n"
"data_directory = something\n"
"include = something\n"
'include "without braces"'
)
postgresql_auto.write("archive_command = something\n" "data_directory = something")
recovery_info["temporary_configuration_files"].append(postgresql_conf.strpath)
recovery_info["temporary_configuration_files"].append(postgresql_auto.strpath)
# Build a RecoveryExecutor object (using a mock as server and backup
# manager.
backup_manager = testing_helpers.build_backup_manager()
executor = RecoveryExecutor(backup_manager)
# Identify dangerous options into config files for remote recovery
executor._analyse_temporary_config_files(recovery_info)
assert len(recovery_info["results"]["changes"]) == 2
assert len(recovery_info["results"]["warnings"]) == 4
# Clean for a local recovery test
recovery_info["results"]["changes"] = []
recovery_info["results"]["warnings"] = []
# Identify dangerous options for local recovery
executor._analyse_temporary_config_files(recovery_info)
assert len(recovery_info["results"]["changes"]) == 2
assert len(recovery_info["results"]["warnings"]) == 4
示例15: test_map_temporary_config_files
def test_map_temporary_config_files(self, tmpdir):
"""
Test the method that prepares configuration files
for the final steps of a recovery
"""
# Build directory/files structure for testing
tempdir = tmpdir.mkdir('tempdir')
recovery_info = {
'configuration_files': ['postgresql.conf', 'postgresql.auto.conf'],
'tempdir': tempdir.strpath,
'temporary_configuration_files': [],
'results': {'changes': [], 'warnings': [], 'missing_files': []},
}
backup_info = testing_helpers.build_test_backup_info()
backup_info.config.basebackups_directory = tmpdir.strpath
datadir = tmpdir.mkdir(backup_info.backup_id).mkdir('data')
postgresql_conf_local = datadir.join('postgresql.conf')
postgresql_auto_local = datadir.join('postgresql.auto.conf')
postgresql_conf_local.write('archive_command = something\n'
'data_directory = something')
postgresql_auto_local.write('archive_command = something\n'
'data_directory = something')
# Build a RecoveryExecutor object (using a mock as server and backup
# manager.
server = testing_helpers.build_mocked_server()
backup_manager = Mock(server=server, config=server.config)
executor = RecoveryExecutor(backup_manager)
executor.map_temporary_config_files(recovery_info,
backup_info, '[email protected]')
# check that configuration files have been moved by the method
assert tempdir.join('postgresql.conf').check()
assert tempdir.join('postgresql.conf').computehash() == \
postgresql_conf_local.computehash()
assert tempdir.join('postgresql.auto.conf').check()
assert tempdir.join('postgresql.auto.conf').computehash() == \
postgresql_auto_local.computehash()
assert recovery_info['results']['missing_files'] == [
'pg_hba.conf', 'pg_ident.conf']