def get_connection(self): """ Connect to MySQL host and yield a connection. :return: MySQL connection :raise MySQLSourceError: if can't connect to server """ connection = None try: connection = pymysql.connect( host=self.hostname, read_default_file=self.defaults_file, connect_timeout=self.connect_timeout, cursorclass=pymysql.cursors.DictCursor ) yield connection except OperationalError: LOG.error( "Can't connect to MySQL server on %s", self.hostname) raise MySQLSourceError( "Can't connect to MySQL server on %s" % self.hostname) finally: if connection: connection.close()
def __init__(self, mysql_connect_info, run_type, backup_type, **kwargs): """ MySQLSource constructor :param mysql_connect_info: MySQL connection details :type mysql_connect_info: MySQLConnectInfo :param run_type: daily, weekly, etc :param backup_type: full or incremental :type backup_type: str :param dst: """ if run_type not in INTERVALS: raise MySQLSourceError('Incorrect run type %r' % run_type) self._parent_lsn = kwargs.get('parent_lsn', None) class _BackupInfo(object): # pylint: disable=too-few-public-methods """class to store details about backup copy""" def __init__(self, lsn=None, binlog_coordinate=None): self.lsn = lsn self.binlog_coordinate = binlog_coordinate # MySQL if not isinstance(mysql_connect_info, MySQLConnectInfo): raise MySQLSourceError('mysql_connect_info must be ' 'instance of MySQLConnectInfo') self._connect_info = mysql_connect_info self._backup_info = _BackupInfo() if backup_type in ['full', 'incremental']: self._type = backup_type else: raise MySQLSourceError('Unrecognized backup type %s' % backup_type) self._suffix = 'xbstream' self._media_type = 'mysql' self._file_name_prefix = 'mysql' self.dst = kwargs.get('dst', None) self._xtrabackup = kwargs.get('xtrabackup_binary', XTRABACKUP_BINARY) super(MySQLSource, self).__init__(run_type)
def __init__(self, mysql_connect_info, run_type, full_backup, dst=None): """ MySQLSource constructor :param mysql_connect_info: MySQL connection details :type mysql_connect_info: MySQLConnectInfo :param run_type: :param full_backup: When to do full backup e.g. daily, weekly :type full_backup: str :param dst: """ class _BackupInfo(object): # pylint: disable=too-few-public-methods """class to store details about backup copy""" def __init__(self, lsn=None, binlog_coordinate=None): self.lsn = lsn self.binlog_coordinate = binlog_coordinate # MySQL if not isinstance(mysql_connect_info, MySQLConnectInfo): raise MySQLSourceError('mysql_connect_info must be ' 'instance of MySQLConnectInfo') self._connect_info = mysql_connect_info self._backup_info = _BackupInfo() if full_backup not in INTERVALS: raise MySQLSourceError('full_backup must be one of %r. ' 'Got %r instead.' % (INTERVALS, full_backup)) if run_type not in INTERVALS: raise MySQLSourceError('run_type must be one of %r. ' 'Got %r instead.' % (INTERVALS, run_type)) self.suffix = 'xbstream' self._media_type = 'mysql' self.full_backup = full_backup self.dst = dst super(MySQLSource, self).__init__(run_type)
def _get_lsn(err_log_path): """Find LSN up to which the backup is taken :param err_log_path: path to Innobackupex error log :return: lsn :rtype: int """ with open(err_log_path) as error_log: for line in error_log: pattern = ("xtrabackup: " "The latest check point (for incremental):") if line.startswith(pattern): lsn = line.split()[7].strip("'") return int(lsn) raise MySQLSourceError("Could not find LSN" " in XtraBackup error output %s" % err_log_path)