Example #1
0
    def backup(self):
        """Run a MySQL backup"""

        if self.schema.timestamp is None:
            self._fast_refresh_schema()

        mock_env = None
        if self.dry_run:
            mock_env = MockEnvironment()
            mock_env.replace_environment()
            LOG.info("Running in dry-run mode.")

        try:
            if self.config['mysqldump']['stop-slave']:
                self.client = connect(self.mysql_config['client'])
                if self.client.show_status('Slave_running',
                                           session=None) != 'ON':
                    raise BackupError(
                        "stop-slave enabled, but replication is "
                        "either not configured or the slave is not "
                        "running.")
                self.config.setdefault('mysql:replication', {})
                _stop_slave(self.client, self.config['mysql:replication'])
            self._backup()
        finally:
            if self.config['mysqldump']['stop-slave'] and \
                'mysql:replication' in self.config:
                _start_slave(self.client, self.config['mysql:replication'])
            if mock_env:
                mock_env.restore_environment()
Example #2
0
    def __init__(self, name, config, target_directory, dry_run=False):
        self.name = name
        self.config = config
        self.target_directory = target_directory
        self.dry_run = dry_run
        self.config.validate_config(self.CONFIGSPEC)  # -> ValidationError

        # Setup a discovery shell to find schema items
        # This will iterate over items during the estimate
        # or backup phase, which will call schema.refresh()
        self.schema = MySQLSchema()
        config = self.config["mysqldump"]
        self.schema.add_database_filter(include_glob(*config["databases"]))
        self.schema.add_database_filter(
            exclude_glob(*config["exclude-databases"]))

        self.schema.add_table_filter(include_glob_qualified(*config["tables"]))
        self.schema.add_table_filter(
            exclude_glob_qualified(*config["exclude-tables"]))
        self.schema.add_engine_filter(include_glob(*config["engines"]))
        self.schema.add_engine_filter(exclude_glob(*config["exclude-engines"]))

        self.mysql_config = build_mysql_config(self.config["mysql:client"])
        self.client = connect(self.mysql_config["client"])

        self.mock_env = None
Example #3
0
    def backup(self):
        """Run a MySQL backup"""

        if self.schema.timestamp is None:
            self._fast_refresh_schema()

        mock_env = None
        if self.dry_run:
            mock_env = MockEnvironment()
            mock_env.replace_environment()
            LOG.info("Running in dry-run mode.")

        try:
            if self.config["mysqldump"]["stop-slave"]:
                self.client = connect(self.mysql_config["client"])
                if self.client.show_status("Slave_running", session=None) != "ON":
                    raise BackupError(
                        "stop-slave enabled, but replication is "
                        "either not configured or the slave is not "
                        "running."
                    )
                self.config.setdefault("mysql:replication", {})
                _stop_slave(self.client, self.config["mysql:replication"])
            self._backup()
        finally:
            if self.config["mysqldump"]["stop-slave"] and "mysql:replication" in self.config:
                _start_slave(self.client, self.config["mysql:replication"])
            if mock_env:
                mock_env.restore_environment()
Example #4
0
def connect_simple(config):
    """Create a MySQLClientConnection given a mysql:client config
    section from a holland mysql backupset
    """
    try:
        mysql_config = build_mysql_config(config)
        LOG.debug("mysql_config => %r", mysql_config)
        connection = connect(mysql_config["client"], PassiveMySQLClient)
        connection.connect()
        return connection
    except MySQLError, exc:
        raise BackupError("[%d] %s" % exc.args)
Example #5
0
 def estimate_backup_size(self):
     """Return estimated backup size"""
     mysql_config = build_mysql_config(self.config["mysql:client"])
     client = connect(mysql_config["client"])
     try:
         datadir = client.show_variable("datadir")
         return directory_size(datadir)
     except OSError as exc:
         raise BackupError("Failed to calculate directory size: [%d] %s" %
                           (exc.errno, exc.strerror))
     finally:
         client.close()
Example #6
0
def connect_simple(config):
    """Create a MySQLClientConnection given a mysql:client config
    section from a holland mysql backupset
    """
    try:
        mysql_config = build_mysql_config(config)
        LOG.debug("mysql_config => %r", mysql_config)
        connection = connect(mysql_config['client'], PassiveMySQLClient)
        connection.connect()
        return connection
    except MySQLError, exc:
        raise BackupError("[%d] %s" % exc.args)
Example #7
0
def wait_for_mysqld(config, mysqld):
    client = connect(config, PassiveMySQLClient)
    LOG.debug("connect via client %r", client)
    while mysqld.process.poll() is None:
        try:
            client.connect()
            client.ping()
            LOG.debug("Ping succeeded")
        except MySQLError:
            time.sleep(0.75)
            continue
        else:
            break
    client.disconnect()
Example #8
0
def connect_simple(config):
    """Create a MySQLClientConnection given a mysql:client config
    section from a holland mysql backupset
    """
    try:
        mysql_config = build_mysql_config(config)
        connection = connect(mysql_config['client'], PassiveMySQLClient)
        sanitized = copy.deepcopy(mysql_config)
        sanitized['client']['password'] = "******"
        LOG.debug("mysql_config => %s", sanitized)
        connection.connect()
        return connection
    except MySQLError as exc:
        raise BackupError("[%d] %s" % exc.args)
Example #9
0
def wait_for_mysqld(config):
    client = connect(config, PassiveMySQLClient)
    LOG.debug("connect via client %r", client)
    while True:
        try:
            client.connect()
            client.ping()
            LOG.debug("Ping succeeded")
        except MySQLError:
            time.sleep(0.75)
            continue
        else:
            break
    client.disconnect()
Example #10
0
def connect_simple(config):
    """Create a MySQLClientConnection given a mysql:client config
    section from a holland mysql backupset
    """
    try:
        mysql_config = build_mysql_config(config)
        connection = connect(mysql_config["client"], MySQLClient)
        sanitized = copy.deepcopy(mysql_config)
        sanitized["client"]["password"] = "******"
        LOG.debug("mysql_config => %s", sanitized)
        connection.connect()
        return connection
    except MySQLError as exc:
        raise BackupError("[%d] %s" % exc.args)
Example #11
0
def wait_for_mysqld(config, mysqld):
    """Wait for new mysql instance to come online"""
    client = connect(config, MySQLClient)
    LOG.debug("connect via client %r", config['socket'])
    while mysqld.process.poll() is None:
        try:
            client.connect()
            client.ping()
            LOG.debug("Ping succeeded")
        except MySQLError:
            time.sleep(0.75)
            continue
        else:
            break
    client.disconnect()
Example #12
0
def connect_simple(config):
    """Create a MySQLClientConnection given a mysql:client config
    section from a holland mysql backupset
    """
    try:
        mysql_config = build_mysql_config(config)
        sanitized = {k: v for k, v in mysql_config.items()}
        if 'password' in mysql_config.keys():
            sanitized['password'] = "******"
        LOG.debug("mysql_config => %r", sanitized)
        connection = connect(mysql_config['client'], PassiveMySQLClient)
        connection.connect()
        return connection
    except MySQLError, exc:
        raise BackupError("[%d] %s" % exc.args)
Example #13
0
def wait_for_mysqld(config, mysqld):
    """Wait for new mysql instance to come online"""
    client = connect(config, MySQLClient)
    LOG.debug("connect via client %r", config["socket"])
    while mysqld.process.poll() is None:
        try:
            client.connect()
            client.ping()
            LOG.debug("Ping succeeded")
        except MySQLError:
            time.sleep(0.75)
            continue
        else:
            break
    client.disconnect()
Example #14
0
    def backup(self):
        """Run a MySQL backup"""
        if self.schema.timestamp is None:
            self._fast_refresh_schema()

        try:
            self.client = connect(self.mysql_config["client"])
        except Exception as ex:
            LOG.debug("%s", ex)
            raise BackupError("Failed connecting to database'")

        if self.dry_run:
            self.mock_env = MockEnvironment()
            self.mock_env.replace_environment()
            LOG.info("Running in dry-run mode.")
            status = self.client.show_databases()
            if not status:
                raise BackupError("Failed to run 'show databases'")
        try:
            if self.config["mysqldump"]["stop-slave"]:
                slave_status = self.client.show_slave_status()
                if not slave_status:
                    raise BackupError("stop-slave enabled, but 'show slave "
                                      "status' failed")
                if slave_status and slave_status["slave_sql_running"] != "Yes":
                    raise BackupError("stop-slave enabled, but replication is "
                                      "not running")
                if not self.dry_run:
                    _stop_slave(self.client, self.config)
            elif self.config["mysqldump"]["bin-log-position"]:
                self.config["mysql:replication"] = {}
                repl_cfg = self.config["mysql:replication"]
                try:
                    master_info = self.client.show_master_status()
                    if master_info:
                        repl_cfg["master_log_file"] = master_info["file"]
                        repl_cfg["master_log_pos"] = master_info["position"]
                except MySQLError as exc:
                    raise BackupError(
                        "Failed to acquire master status [%d] %s" % exc.args)
            self._backup()
        finally:
            if self.config["mysqldump"][
                    "stop-slave"] and "mysql:replication" in self.config:
                _start_slave(self.client, self.config["mysql:replication"])
            if self.mock_env:
                self.mock_env.restore_environment()
Example #15
0
    def backup(self):
        """Run a MySQL backup"""
        if self.schema.timestamp is None:
            self._fast_refresh_schema()

        try:
            self.client = connect(self.mysql_config["client"])
        except Exception as ex:
            LOG.debug("%s", ex)
            raise BackupError("Failed connecting to database'")

        if self.dry_run:
            self.mock_env = MockEnvironment()
            self.mock_env.replace_environment()
            LOG.info("Running in dry-run mode.")
            status = self.client.show_databases()
            if not status:
                raise BackupError("Failed to run 'show databases'")
        try:
            if self.config["mysqldump"]["stop-slave"]:
                slave_status = self.client.show_slave_status()
                if not slave_status:
                    raise BackupError("stop-slave enabled, but 'show slave " "status' failed")
                if slave_status and slave_status["slave_sql_running"] != "Yes":
                    raise BackupError("stop-slave enabled, but replication is " "not running")
                if not self.dry_run:
                    _stop_slave(self.client, self.config)
            elif self.config["mysqldump"]["bin-log-position"]:
                self.config["mysql:replication"] = {}
                repl_cfg = self.config["mysql:replication"]
                try:
                    master_info = self.client.show_master_status()
                    if master_info:
                        repl_cfg["master_log_file"] = master_info["file"]
                        repl_cfg["master_log_pos"] = master_info["position"]
                except MySQLError as exc:
                    raise BackupError("Failed to acquire master status [%d] %s" % exc.args)
            self._backup()
        finally:
            if self.config["mysqldump"]["stop-slave"] and "mysql:replication" in self.config:
                _start_slave(self.client, self.config["mysql:replication"])
            if self.mock_env:
                self.mock_env.restore_environment()
Example #16
0
    def backup(self):
        """Run a MySQL backup"""
        if self.schema.timestamp is None:
            self._fast_refresh_schema()

        mock_env = None
        if self.dry_run:
            mock_env = MockEnvironment()
            mock_env.replace_environment()
            LOG.info("Running in dry-run mode.")

        try:
            if self.config['mysqldump']['stop-slave']:
                self.client = connect(self.mysql_config['client'])
                slave_status = self.client.show_slave_status()
                if slave_status is None:
                    raise BackupError("stop-slave enabled, but 'show slave "
                                      "status' failed")
                elif slave_status['slave_sql_running'] != 'Yes':
                    raise BackupError("stop-slave enabled, but replication is "
                                      "not running")
                _stop_slave(self.client, self.config)
            elif self.config['mysqldump']['bin-log-position']:
                self.config['mysql:replication'] = {}
                repl_cfg = self.config['mysql:replication']
                try:
                    master_info = self.client.show_master_status()
                    if master_info:
                        repl_cfg['master_log_file'] = master_info['file']
                        repl_cfg['master_log_pos'] = master_info['position']
                except MySQLError as exc:
                    raise BackupError(
                        "Failed to acquire master status [%d] %s" % exc.args)
            self._backup()
        finally:
            if self.config['mysqldump']['stop-slave'] and \
                'mysql:replication' in self.config:
                _start_slave(self.client, self.config['mysql:replication'])
            if mock_env:
                mock_env.restore_environment()
Example #17
0
    def __init__(self, name, config, target_directory, dry_run=False):
        self.name = name
        self.config = config
        self.target_directory = target_directory
        self.dry_run = dry_run
        self.config.validate_config(self.CONFIGSPEC)  # -> ValidationError

        # Setup a discovery shell to find schema items
        # This will iterate over items during the estimate
        # or backup phase, which will call schema.refresh()
        self.schema = MySQLSchema()
        config = self.config["mysqldump"]
        self.schema.add_database_filter(include_glob(*config["databases"]))
        self.schema.add_database_filter(exclude_glob(*config["exclude-databases"]))

        self.schema.add_table_filter(include_glob_qualified(*config["tables"]))
        self.schema.add_table_filter(exclude_glob_qualified(*config["exclude-tables"]))
        self.schema.add_engine_filter(include_glob(*config["engines"]))
        self.schema.add_engine_filter(exclude_glob(*config["exclude-engines"]))

        self.mysql_config = build_mysql_config(self.config["mysql:client"])
        self.client = connect(self.mysql_config["client"])