def test_get_my_cnf_2_cnf(tmpdir): status = MySQLStatus() mycnf_1 = tmpdir.join('my-1.cnf') mycnf_1.write('some_content_1') mycnf_2 = tmpdir.join('my-2.cnf') mycnf_2.write('some_content_2') backup_copy = MySQLCopy('master1', 'daily', 'foo.txt', binlog='binlog1', position=101, type='full', lsn=1230, backup_started=123, backup_finished=456, config_files=[str(mycnf_1), str(mycnf_2)]) status.add(backup_copy) expected = {str(mycnf_1): 'some_content_1', str(mycnf_2): 'some_content_2'} for path, content in get_my_cnf(status, backup_copy.key): assert path in expected expected_value = expected.pop(path) assert content == expected_value assert expected == {}
def test_add(status_raw_empty, tmpdir): status = MySQLStatus(status_raw_empty) mycnf_1 = tmpdir.join('my-1.cnf') mycnf_1.write('some_content_1') mycnf_2 = tmpdir.join('my-2.cnf') mycnf_2.write('some_content_2') backup_copy = MySQLCopy( 'master1', 'daily', 'foo.txt', binlog='binlog1', position=101, type='full', lsn=1230, backup_started=123, backup_finished=456, config_files=[str(mycnf_1), str(mycnf_2)] ) status.add(backup_copy) assert len(status.daily) == 1 assert status.daily[backup_copy.key].binlog == 'binlog1' assert status.daily[backup_copy.key].position == 101 assert status.daily[backup_copy.key].type == 'full' assert status.daily[backup_copy.key].lsn == 1230 assert status.daily[backup_copy.key].backup_started == 123 assert status.daily[backup_copy.key].backup_finished == 456 assert status.daily[backup_copy.key].duration == 333 config_content = status.daily[backup_copy.key].config[str(mycnf_1)] assert config_content == 'some_content_1' config_content = status.daily[backup_copy.key].config[str(mycnf_2)] assert config_content == 'some_content_2'
def test_add(status_raw_empty, tmpdir): status = MySQLStatus(status_raw_empty) assert status.valid mycnf_1 = tmpdir.join('my-1.cnf') mycnf_1.write('some_content_1') mycnf_2 = tmpdir.join('my-2.cnf') mycnf_2.write('some_content_2') backup_copy = MySQLCopy('master1', 'daily', 'foo.txt', binlog='binlog1', position=101, type='full', lsn=1230, backup_started=123, backup_finished=456, config_files=[str(mycnf_1), str(mycnf_2)]) status.add(backup_copy) assert len(status.daily) == 1 assert status.daily[backup_copy.key].binlog == 'binlog1' assert status.daily[backup_copy.key].position == 101 assert status.daily[backup_copy.key].type == 'full' assert status.daily[backup_copy.key].lsn == 1230 assert status.daily[backup_copy.key].backup_started == 123 assert status.daily[backup_copy.key].backup_finished == 456 assert status.daily[backup_copy.key].duration == 333 assert { str(mycnf_1): 'some_content_1' } in status.daily[backup_copy.key].config assert { str(mycnf_2): 'some_content_2' } in status.daily[backup_copy.key].config
def test_remove(status_raw_empty): status = MySQLStatus(status_raw_empty) copy = MySQLCopy('foo', 'daily', 'some_file.txt', type='full') status.add(copy) assert len(status.daily) == 1 status.remove(copy.key) assert len(status.daily) == 0
def test_get_my_cnf_2_cnf(tmpdir): status = MySQLStatus() mycnf_1 = tmpdir.join('my-1.cnf') mycnf_1.write('some_content_1') mycnf_2 = tmpdir.join('my-2.cnf') mycnf_2.write('some_content_2') backup_copy = MySQLCopy( 'master1', 'daily', 'foo.txt', binlog='binlog1', position=101, type='full', lsn=1230, backup_started=123, backup_finished=456, config_files=[str(mycnf_1), str(mycnf_2)] ) status.add(backup_copy) expected = { str(mycnf_1): 'some_content_1', str(mycnf_2): 'some_content_2' } for path, content in get_my_cnf(status, backup_copy.key): assert path in expected expected_value = expected.pop(path) assert content == expected_value assert expected == {}
def backup_mysql(run_type, config): """Take backup of local MySQL instance :param run_type: Run type :type run_type: str :param config: Tool configuration :type config: TwinDBBackupConfig """ if config.backup_mysql is False: LOG.debug("Not backing up MySQL") return dst = config.destination() try: full_backup = config.mysql.full_backup except configparser.NoOptionError: full_backup = "daily" backup_start = time.time() status = MySQLStatus(dst=dst) kwargs = { "backup_type": status.next_backup_type(full_backup, run_type), "dst": dst, "xtrabackup_binary": config.mysql.xtrabackup_binary, } parent = status.candidate_parent(run_type) if kwargs["backup_type"] == "incremental": kwargs["parent_lsn"] = parent.lsn LOG.debug("Creating source %r", kwargs) src = MySQLSource(MySQLConnectInfo(config.mysql.defaults_file), run_type, **kwargs) callbacks = [] try: _backup_stream(config, src, dst, callbacks=callbacks) except (DestinationError, SourceError, SshClientException) as err: raise OperationError(err) LOG.debug("Backup copy name: %s", src.get_name()) kwargs = { "type": src.type, "binlog": src.binlog_coordinate[0], "position": src.binlog_coordinate[1], "lsn": src.lsn, "backup_started": backup_start, "backup_finished": time.time(), "config_files": my_cnfs(MY_CNF_COMMON_PATHS), } if src.incremental: kwargs["parent"] = parent.key backup_copy = MySQLCopy(src.host, run_type, src.basename, **kwargs) status.add(backup_copy) status = src.apply_retention_policy(dst, config, run_type, status) LOG.debug("status after apply_retention_policy():\n%s", status) backup_duration = backup_copy.duration export_info( config, data=backup_duration, category=ExportCategory.mysql, measure_type=ExportMeasureType.backup, ) status.save(dst) LOG.debug("Callbacks are %r", callbacks) for callback in callbacks: callback[0].callback(**callback[1])
def backup_mysql(run_type, config): """Take backup of local MySQL instance :param run_type: Run type :type run_type: str :param config: Tool configuration :type config: TwinDBBackupConfig """ if config.backup_mysql is False: LOG.debug('Not backing up MySQL') return dst = config.destination() try: full_backup = config.mysql.full_backup except ConfigParser.NoOptionError: full_backup = 'daily' backup_start = time.time() status = MySQLStatus(dst=dst) kwargs = { 'backup_type': status.next_backup_type(full_backup, run_type), 'dst': dst, 'xtrabackup_binary': config.mysql.xtrabackup_binary } parent = status.candidate_parent(run_type) if kwargs['backup_type'] == 'incremental': kwargs['parent_lsn'] = parent.lsn LOG.debug('Creating source %r', kwargs) src = MySQLSource(MySQLConnectInfo(config.mysql.defaults_file), run_type, **kwargs) callbacks = [] try: _backup_stream(config, src, dst, callbacks=callbacks) except (DestinationError, SourceError, SshClientException) as err: raise OperationError(err) LOG.debug('Backup copy name: %s', src.get_name()) kwargs = { 'type': src.type, 'binlog': src.binlog_coordinate[0], 'position': src.binlog_coordinate[1], 'lsn': src.lsn, 'backup_started': backup_start, 'backup_finished': time.time(), 'config_files': my_cnfs(MY_CNF_COMMON_PATHS) } if src.incremental: kwargs['parent'] = parent.key backup_copy = MySQLCopy(src.host, run_type, src.basename, **kwargs) status.add(backup_copy) status = src.apply_retention_policy(dst, config, run_type, status) LOG.debug('status after apply_retention_policy():\n%s', status) backup_duration = backup_copy.duration export_info(config, data=backup_duration, category=ExportCategory.mysql, measure_type=ExportMeasureType.backup) status.save(dst) LOG.debug('Callbacks are %r', callbacks) for callback in callbacks: callback[0].callback(**callback[1])
def backup_mysql(run_type, config): """Take backup of local MySQL instance :param run_type: Run type :type run_type: str :param config: Tool configuration :type config: TwinDBBackupConfig """ if config.backup_mysql is False: LOG.debug('Not backing up MySQL') return dst = config.destination() try: full_backup = config.mysql.full_backup except ConfigParser.NoOptionError: full_backup = 'daily' backup_start = time.time() status = MySQLStatus(dst=dst) kwargs = { 'backup_type': status.next_backup_type(full_backup, run_type), 'dst': dst, 'xtrabackup_binary': config.mysql.xtrabackup_binary } parent = status.candidate_parent(run_type) if kwargs['backup_type'] == 'incremental': kwargs['parent_lsn'] = parent.lsn LOG.debug('Creating source %r', kwargs) src = MySQLSource( MySQLConnectInfo(config.mysql.defaults_file), run_type, **kwargs ) callbacks = [] try: _backup_stream(config, src, dst, callbacks=callbacks) except (DestinationError, SourceError, SshClientException) as err: raise OperationError(err) LOG.debug('Backup copy name: %s', src.get_name()) kwargs = { 'type': src.type, 'binlog': src.binlog_coordinate[0], 'position': src.binlog_coordinate[1], 'lsn': src.lsn, 'backup_started': backup_start, 'backup_finished': time.time(), 'config_files': my_cnfs(MY_CNF_COMMON_PATHS) } if src.incremental: kwargs['parent'] = parent.key backup_copy = MySQLCopy( src.host, run_type, src.basename, **kwargs ) status.add(backup_copy) status = src.apply_retention_policy(dst, config, run_type, status) LOG.debug('status after apply_retention_policy():\n%s', status) backup_duration = backup_copy.duration export_info( config, data=backup_duration, category=ExportCategory.mysql, measure_type=ExportMeasureType.backup ) status.save(dst) LOG.debug('Callbacks are %r', callbacks) for callback in callbacks: callback[0].callback(**callback[1])