CedarBackup3.extend.mysql — cedar-backup3 3.10.0 documentation

# CedarBackup3.extend.mysql

Provides an extension to back up MySQL databases.

This is a Cedar Backup extension used to back up MySQL databases via the Cedar Backup command line. It requires a new configuration section <mysql> and is intended to be run either immediately before or immediately after the standard collect action. Aside from its own configuration, it requires the options and collect configuration sections in the standard Cedar Backup configuration file.

The backup is done via the `mysqldump` command included with the MySQL product. Output can be compressed using `gzip` or `bzip2`. Administrators can configure the extension either to back up all databases or to back up only specific databases. Note that this code always produces a full backup. There is currently no facility for making incremental backups. If/when someone has a need for this and can describe how to do it, I’ll update this extension or provide another.

The extension assumes that all configured databases can be backed up by a single user. Often, the “root” database user will be used. An alternative is to create a separate MySQL “backup” user and grant that user rights to read (but not write) various databases as needed. This second option is probably the best choice.

The extension accepts a username and password in configuration. However, you probably do not want to provide those values in Cedar Backup configuration. This is because Cedar Backup will provide these values to `mysqldump` via the command-line `--user` and `--password` switches, which will be visible to other users in the process listing.

Instead, you should configure the username and password in one of MySQL’s configuration files. Typically, that would be done by putting a stanza like this in `/root/.my.cnf`:

    [mysqldump]
    user     = root
    password = <secret>

Regardless of whether you are using `~/.my.cnf` or `/etc/cback3.conf` to store database login and password information, you should be careful about who is allowed to view that information. Typically, this means locking down permissions so that only the file owner can read the file contents (i.e. use mode `0600`).

author:

  Kenneth J. Pronovici <pronovic@ieee.org>

## Module Contents

CedarBackup3.extend.mysql.logger

CedarBackup3.extend.mysql.MYSQLDUMP_COMMAND = ['mysqldump']

class CedarBackup3.extend.mysql.MysqlConfig(user=None, password=None, compressMode=None, all=None, databases=None)

  Class representing MySQL configuration.

  The MySQL configuration information is used for backing up MySQL databases.

  The following restrictions exist on data in this class:

  > - The compress mode must be one of the values in `VALID_COMPRESS_MODES`.

  > - The ‘all’ flag must be ‘Y’ if no databases are defined.

  > - The ‘all’ flag must be ‘N’ if any databases are defined.

  > - Any values in the databases list must be strings.

  user = None
  password = None
  compressMode = None
  all = None
  databases = None
  __repr__()

    Official string representation for class instance.

  __str__()

    Informal string representation for class instance.

  __eq__(other)

    Equals operator, iplemented in terms of original Python 2 compare operator.

  __lt__(other)

    Less-than operator, iplemented in terms of original Python 2 compare operator.

  __gt__(other)

    Greater-than operator, iplemented in terms of original Python 2 compare operator.

  __cmp__(other)

    Original Python 2 comparison operator. :param other: Other object to compare to

    Returns:

      -1/0/1 depending on whether self is `<`, `=` or `>` other

class CedarBackup3.extend.mysql.LocalConfig(xmlData=None, xmlPath=None, validate=True)

  Class representing this extension’s configuration document.

  This is not a general-purpose configuration object like the main Cedar Backup configuration object. Instead, it just knows how to parse and emit MySQL-specific configuration values. Third parties who need to read and write configuration related to this extension should access it through the constructor, `validate` and `addConfig` methods.

  Note: Lists within this class are “unordered” for equality comparisons.

  mysql = None
  __repr__()

    Official string representation for class instance.

  __str__()

    Informal string representation for class instance.

  __eq__(other)

    Equals operator, iplemented in terms of original Python 2 compare operator.

  __lt__(other)

    Less-than operator, iplemented in terms of original Python 2 compare operator.

  __gt__(other)

    Greater-than operator, iplemented in terms of original Python 2 compare operator.

  __cmp__(other)

    Original Python 2 comparison operator. Lists within this class are “unordered” for equality comparisons. :param other: Other object to compare to

    Returns:

      -1/0/1 depending on whether self is `<`, `=` or `>` other

  validate()

    Validates configuration represented by the object.

    The compress mode must be filled in. Then, if the ‘all’ flag is set, no databases are allowed, and if the ‘all’ flag is not set, at least one database is required.

    Raises:

      ValueError – If one of the validations fails

  addConfig(xmlDom, parentNode)

    Adds a <mysql> configuration section as the next child of a parent.

    Third parties should use this function to write configuration related to this extension.

    We add the following fields to the document:

        user           //cb_config/mysql/user
        password       //cb_config/mysql/password
        compressMode   //cb_config/mysql/compress_mode
        all            //cb_config/mysql/all

    We also add groups of the following items, one list element per item:

        database       //cb_config/mysql/database

    Parameters:

      - xmlDom – DOM tree as from `impl.createDocument()`

      - parentNode – Parent that the section should be appended to

CedarBackup3.extend.mysql.executeAction(configPath, options, config)

  Executes the MySQL backup action.

  Parameters:

    - configPath (String representing a path on disk) – Path to configuration file on disk

    - options (Options object) – Program command-line options

    - config (Config object) – Program configuration

  Raises:

    - ValueError – Under many generic error conditions

    - IOError – If a backup could not be written for some reason

CedarBackup3.extend.mysql.backupDatabase(user, password, backupFile, database=None)

  Backs up an individual MySQL database, or all databases.

  This function backs up either a named local MySQL database or all local MySQL databases, using the passed-in user and password (if provided) for connectivity. This function call always results a full backup. There is no facility for incremental backups.

  The backup data will be written into the passed-in backup file. Normally, this would be an object as returned from `open`, but it is possible to use something like a `GzipFile` to write compressed output. The caller is responsible for closing the passed-in backup file.

  Often, the “root” database user will be used when backing up all databases. An alternative is to create a separate MySQL “backup” user and grant that user rights to read (but not write) all of the databases that will be backed up.

  This function accepts a username and password. However, you probably do not want to pass those values in. This is because they will be provided to `mysqldump` via the command-line `--user` and `--password` switches, which will be visible to other users in the process listing.

  Instead, you should configure the username and password in one of MySQL’s configuration files. Typically, this would be done by putting a stanza like this in `/root/.my.cnf`, to provide `mysqldump` with the root database username and its password:

      [mysqldump]
      user     = root
      password = <secret>

  If you are executing this function as some system user other than root, then the `.my.cnf` file would be placed in the home directory of that user. In either case, make sure to set restrictive permissions (typically, mode `0600`) on `.my.cnf` to make sure that other users cannot read the file.

  Parameters:

    - user (String representing MySQL username, or `None`) – User to use for connecting to the database (if any)

    - password (String representing MySQL password, or `None`) – Password associated with user (if any)

    - backupFile (Python file object as from `open` or `file`) – File use for writing backup

    - database (String representing database name, or `None` for all databases) – Name of the database to be backed up

  Raises:

    - ValueError – If some value is missing or invalid

    - IOError – If there is a problem executing the MySQL dump