Ejemplo n.º 1
0
    def start_backup(self, backup_info):
        """
        Manage the start of an exclusive backup

        The method performs all the preliminary operations required for an
        exclusive physical backup to start, as well as preparing the
        information on the backup for Barman.

        :param barman.infofile.BackupInfo backup_info: backup information
        """
        self.current_action = "connecting to database (%s)" % \
                              self.executor.config.conninfo
        output.debug(self.current_action)
        # Retrieve PostgreSQL server metadata
        self._pg_get_metadata(backup_info)

        # Issue pg_start_backup on the PostgreSQL server
        self.current_action = "issuing start backup command"
        _logger.debug(self.current_action)
        label = "Barman backup %s %s" % (
            backup_info.server_name, backup_info.backup_id)

        # Exclusive backup: issue a pg_start_Backup() command
        start_row = self.executor.server.postgres.start_exclusive_backup(label)
        start_xlog, start_file_name, start_file_offset, start_time = \
            start_row
        backup_info.set_attribute('status', "STARTED")
        backup_info.set_attribute('timeline',
                                  int(start_file_name[0:8], 16))
        backup_info.set_attribute('begin_xlog', start_xlog)
        backup_info.set_attribute('begin_wal', start_file_name)
        backup_info.set_attribute('begin_offset', start_file_offset)
        backup_info.set_attribute('begin_time', start_time)
Ejemplo n.º 2
0
    def _pg_get_metadata(self, backup_info):
        """
        Load PostgreSQL metadata into the backup_info parameter

        :param barman.infofile.BackupInfo backup_info: backup information
        """
        server = self.executor.server

        # Get the PostgreSQL data directory location
        self.current_action = 'detecting data directory'
        output.debug(self.current_action)
        data_directory = server.postgres.get_setting('data_directory')
        backup_info.set_attribute('pgdata', data_directory)

        # Set server version
        backup_info.set_attribute('version', server.postgres.server_version)

        # Set configuration files location
        cf = server.postgres.get_configuration_files()
        for key in cf:
            backup_info.set_attribute(key, cf[key])

        # Get tablespaces information
        self.current_action = 'detecting tablespaces'
        output.debug(self.current_action)
        tablespaces = server.postgres.get_tablespaces()
        if tablespaces and len(tablespaces) > 0:
            backup_info.set_attribute('tablespaces', tablespaces)
            for item in tablespaces:
                msg = "\t%s, %s, %s" % (item.oid, item.name, item.location)
                _logger.info(msg)
Ejemplo n.º 3
0
def archive_wal(args):
    """
    Execute maintenance operations on WAL files for a given server.
    This command processes any incoming WAL files for the server
    and archives them along the catalogue.

    """
    server = get_server(args)
    output.debug("Starting archive-wal for server %s", server.config.name)
    server.archive_wal()
    output.close_and_exit()
Ejemplo n.º 4
0
    def _write_backup_label(self, backup_info):
        """
        Write backup_label file inside PGDATA folder

        :param barman.infofile.BackupInfo  backup_info: tbackup information
        """
        label_file = os.path.join(backup_info.get_data_directory(),
                                  'backup_label')
        output.debug("Writing backup label: %s" % label_file)
        with open(label_file, 'w') as f:
            f.write(backup_info.backup_label)
Ejemplo n.º 5
0
    def _write_backup_label(self, backup_info):
        """
        Write backup_label file inside pgdata folder

        :param backup_info: the backup information structure
        """
        label_file = os.path.join(backup_info.get_data_directory(),
                                  'backup_label')
        output.debug("Writing backup label: %s" % label_file)
        with open(label_file, 'w') as f:
            f.write(backup_info.backup_label)
Ejemplo n.º 6
0
    def test_debug(self, caplog):
        # preparation
        writer = self._mock_writer()

        msg = 'test message'
        output.debug(msg)

        # logging test
        for record in caplog.records():
            assert record.levelname == 'DEBUG'
            assert record.name == __name__
        assert msg in caplog.text()

        # writer test
        assert not writer.error_occurred.called
        writer.debug.assert_called_once_with(msg)

        # global status test
        assert not output.error_occurred
Ejemplo n.º 7
0
    def test_debug(self, caplog):
        # preparation
        writer = self._mock_writer()

        msg = 'test message'
        output.debug(msg)

        # logging test
        for record in caplog.records:
            assert record.levelname == 'DEBUG'
            assert record.name == __name__
        assert msg in caplog.text

        # writer test
        assert not writer.error_occurred.called
        writer.debug.assert_called_once_with(msg)

        # global status test
        assert not output.error_occurred
Ejemplo n.º 8
0
    def test_debug_error(self, caplog):
        # preparation
        writer = self._mock_writer()

        msg = "test message"
        output.debug(msg, is_error=True)

        # logging test
        for record in caplog.records:
            assert record.levelname == "DEBUG"
            assert record.name == __name__
        assert msg in caplog.text

        # writer test
        writer.error_occurred.assert_called_once_with()
        writer.debug.assert_called_once_with(msg)

        # global status test
        assert output.error_occurred
Ejemplo n.º 9
0
    def test_debug_with_args(self, caplog):
        # preparation
        writer = self._mock_writer()

        msg = 'test format %02d %s'
        args = (1, '2nd')
        output.debug(msg, *args)

        # logging test
        for record in caplog.records():
            assert record.levelname == 'DEBUG'
            assert record.name == __name__
        assert msg % args in caplog.text()

        # writer test
        assert not writer.error_occurred.called
        writer.debug.assert_called_once_with(msg, *args)

        # global status test
        assert not output.error_occurred
Ejemplo n.º 10
0
    def test_debug_with_args(self, caplog):
        # preparation
        writer = self._mock_writer()

        msg = 'test format %02d %s'
        args = (1, '2nd')
        output.debug(msg, *args)

        # logging test
        for record in caplog.records:
            assert record.levelname == 'DEBUG'
            assert record.name == __name__
        assert msg % args in caplog.text

        # writer test
        assert not writer.error_occurred.called
        writer.debug.assert_called_once_with(msg, *args)

        # global status test
        assert not output.error_occurred
Ejemplo n.º 11
0
    def start_backup(self, backup_info):
        """
        Start of the backup.

        The method performs all the preliminary operations required for a
        backup to start.

        :param barman.infofile.BackupInfo backup_info: backup information
        """
        self.current_action = "connecting to database (%s)" % \
                              self.executor.config.conninfo
        output.debug(self.current_action)
        # with self.executor.server.pg_connect():
        # Retrieve PostgreSQL server metadata
        self._pg_get_metadata(backup_info)

        # Issue _pg_start_backup on the PostgreSQL server
        self.current_action = "issuing start backup command"
        _logger.debug(self.current_action)
        label = "Barman backup %s %s" % (
            backup_info.server_name, backup_info.backup_id)

        # Concurrent backup: issue a pgespresso_start_Backup() command

        postgres = self.executor.server.postgres
        start_row = postgres.pgespresso_start_backup(label)
        backup_data, start_time = start_row
        wal_re = re.compile(
            '^START WAL LOCATION: (.*) \(file (.*)\)',
            re.MULTILINE)
        wal_info = wal_re.search(backup_data)
        backup_info.set_attribute('status', "STARTED")
        backup_info.set_attribute('timeline',
                                  int(wal_info.group(2)[0:8], 16))
        backup_info.set_attribute('begin_xlog', wal_info.group(1))
        backup_info.set_attribute('begin_wal', wal_info.group(2))
        backup_info.set_attribute('begin_offset',
                                  xlog.get_offset_from_location(
                                      wal_info.group(1)))
        backup_info.set_attribute('backup_label', backup_data)
        backup_info.set_attribute('begin_time', start_time)
Ejemplo n.º 12
0
    def test_debug_with_kwargs(self):
        # preparation
        self._mock_writer()

        with pytest.raises(TypeError):
            output.debug('message', bad_arg=True)
Ejemplo n.º 13
0
    def test_debug_with_kwargs(self):
        # preparation
        self._mock_writer()

        with pytest.raises(TypeError):
            output.debug('message', bad_arg=True)
Ejemplo n.º 14
0
    def start_backup(self, backup_info):
        """
        Start of the backup.

        The method performs all the preliminary operations required for a
        backup to start.

        :param barman.infofile.BackupInfo backup_info: the backup information
        """
        self.current_action = "connecting to database (%s)" % \
                              self.config.conninfo
        output.debug(self.current_action)
        with self.server.pg_connect():

            # Get the PostgreSQL data directory location
            self.current_action = 'detecting data directory'
            output.debug(self.current_action)
            data_directory = self.server.get_pg_setting('data_directory')
            backup_info.set_attribute('pgdata', data_directory)

            # Set server version
            backup_info.set_attribute('version', self.server.server_version)

            # Set configuration files location
            cf = self.server.get_pg_configuration_files()
            if cf:
                for key in sorted(cf.keys()):
                    backup_info.set_attribute(key, cf[key])

            # Get tablespaces information
            self.current_action = 'detecting tablespaces'
            output.debug(self.current_action)
            tablespaces = self.server.get_pg_tablespaces()
            if tablespaces and len(tablespaces) > 0:
                backup_info.set_attribute('tablespaces', tablespaces)
                for item in tablespaces:
                    msg = "\t%s, %s, %s" % (item.oid, item.name, item.location)
                    _logger.info(msg)

            # Issue pg_start_backup on the PostgreSQL server
            self.current_action = "issuing start backup command"
            _logger.debug(self.current_action)
            label = "Barman backup %s %s" % (
                backup_info.server_name, backup_info.backup_id)

            # Exclusive backup: issue a pg_start_Backup() command
            if BackupOptions.CONCURRENT_BACKUP not in \
                    self.config.backup_options:
                start_row = self.pg_start_backup(label)
                start_xlog, start_file_name, start_file_offset, start_time = \
                    start_row
                backup_info.set_attribute('status', "STARTED")
                backup_info.set_attribute('timeline',
                                          int(start_file_name[0:8], 16))
                backup_info.set_attribute('begin_xlog', start_xlog)
                backup_info.set_attribute('begin_wal', start_file_name)
                backup_info.set_attribute('begin_offset', start_file_offset)
                backup_info.set_attribute('begin_time', start_time)

            # Concurrent backup: use pgespresso extension to start a the backup
            else:
                start_row = self.pgespresso_start_backup(label)
                backup_data, start_time = start_row
                wal_re = re.compile(
                    '^START WAL LOCATION: (.*) \(file (.*)\)',
                    re.MULTILINE)
                wal_info = wal_re.search(backup_data)
                backup_info.set_attribute('status', "STARTED")
                backup_info.set_attribute('timeline',
                                          int(wal_info.group(2)[0:8], 16))
                backup_info.set_attribute('begin_xlog', wal_info.group(1))
                backup_info.set_attribute('begin_wal', wal_info.group(2))
                backup_info.set_attribute('begin_offset',
                                          xlog.get_offset_from_location(
                                              wal_info.group(1)))
                backup_info.set_attribute('backup_label', backup_data)
                backup_info.set_attribute('begin_time', start_time)