You are here

class backup_migrate_source_db in Backup and Migrate 7.3

Same name and namespace in other branches
  1. 8.3 includes/sources.db.inc \backup_migrate_source_db
  2. 6.3 includes/sources.db.inc \backup_migrate_source_db

A destination type for saving to a database server.

Hierarchy

Expanded class hierarchy of backup_migrate_source_db

1 string reference to 'backup_migrate_source_db'
backup_migrate_backup_migrate_source_subtypes in includes/sources.inc
Implements hook_backup_migrate_source_subtypes().

File

includes/sources.db.inc, line 13
Functions to handle the direct to database destination.

View source
class backup_migrate_source_db extends backup_migrate_source_remote {
  public $supported_ops = array(
    'configure',
    'source',
  );
  public $db_target = 'default';
  public $connection = NULL;

  /**
   *
   */
  public function type_name() {
    return t("Database");
  }

  /**
   * Save the info by importing it into the database.
   */
  public function save_file($file, $settings) {
    require_once dirname(__FILE__) . '/files.inc';

    // Set the source_id to the destination_id in the settings since for a
    // restore, the source_id is the database that gets restored to.
    $settings
      ->set_source($this
      ->get_id());

    // Restore the file to the source database.
    $file = backup_migrate_perform_restore($this
      ->get_id(), $file, $settings);
    return $file;
  }

  /**
   * Destination configuration callback.
   */
  public function edit_form() {
    $form = parent::edit_form();
    $form['scheme']['#default_value'] = $this
      ->default_scheme();
    $form['scheme']['#access'] = FALSE;
    $form['path']['#title'] = t('Database name');
    $form['path']['#description'] = t('The name of the database. The database must exist, it will not be created for you.');
    $form['user']['#description'] = t('Enter the name of a user who has write access to the database.');
    return $form;
  }

  /**
   * Validate the configuration form. Make sure the db info is valid.
   */
  public function edit_form_validate($form, &$form_state) {
    if (!preg_match('/[a-zA-Z0-9_\\$]+/', $form_state['values']['path'])) {
      form_set_error('path', t('The database name is not valid.'));
    }
    parent::edit_form_validate($form, $form_state);
  }

  /**
   * Get the default settings for this object.
   *
   * @return array
   *   The default tables whose data can be ignored. These tables mostly
   *   contain info which can be easily reproducted (such as cache or search
   *   index) but also tables which can become quite bloated but are not
   *   necessarily extremely important to back up or migrate during development
   *   (such as access log and watchdog).
   */
  public function backup_settings_default() {
    $all_tables = $this
      ->_get_table_names();

    // Basic modules that should be excluded.
    $basic = array(
      // Default core tables.
      'accesslog',
      'sessions',
      'watchdog',
      // Search module.
      'search_dataset',
      'search_index',
      'search_keywords_log',
      'search_total',
      // Devel module.
      'devel_queries',
      'devel_times',
    );

    // Identify all cache tables.
    $cache = array(
      'cache',
    );
    foreach ($all_tables as $table_name) {
      if (strpos($table_name, 'cache_') === 0) {
        $cache[] = $table_name;
      }
    }

    // Simpletest can create a lot of tables that do not need to be backed up,
    // but all of them start with the string 'simpletest' so they can be easily
    // excluded.
    $simpletest = array();
    foreach ($all_tables as $table_name) {
      if (strpos($table_name, 'simpletest') === 0) {
        $simpletest[] = $table_name;
      }
    }
    return array(
      'nodata_tables' => drupal_map_assoc(array_merge($basic, $cache, module_invoke_all('devel_caches'))),
      'exclude_tables' => $simpletest,
      'utils_lock_tables' => FALSE,
    );
  }

  /**
   * Get the form for the backup settings for this destination.
   */
  public function backup_settings_form($settings) {
    $objects = $this
      ->get_object_names();
    $form['#description'] = t("You may omit specific tables, or specific table data from the backup file. Only omit data that you know you will not need such as cache data, or tables from other applications. Excluding tables can break your Drupal install, so <strong>do not change these settings unless you know what you're doing</strong>.");
    $form['exclude_tables'] = array(
      "#type" => "select",
      "#multiple" => TRUE,
      "#title" => t("Exclude the following tables altogether"),
      "#options" => $objects,
      "#default_value" => $settings['exclude_tables'],
      "#description" => t("The selected tables will not be added to the backup file."),
    );
    $form['nodata_tables'] = array(
      "#type" => "select",
      "#multiple" => TRUE,
      "#title" => t("Exclude the data from the following tables"),
      "#options" => $objects,
      "#default_value" => $settings['nodata_tables'],
      "#description" => t("The selected tables will have their structure backed up but not their contents. This is useful for excluding cache data to reduce file size."),
    );
    $form['utils_lock_tables'] = array(
      '#type' => 'checkbox',
      '#title' => t('Lock tables during backup'),
      '#default_value' => !empty($settings['utils_lock_tables']) ? $settings['utils_lock_tables'] : NULL,
      '#description' => t('This can help reduce data corruption, but will make your site unresponsive.'),
    );
    return $form;
  }

  /**
   * Backup from this source.
   */
  public function backup_to_file($file, $settings) {
    $file
      ->push_type($this
      ->get_file_type_id());

    // $this->lock_tables($settings);
    // Switch to a different db if specified.
    if (variable_get('backup_migrate_verbose')) {
      _backup_migrate_message('Start peak memory usage: %mem', array(
        '%mem' => backup_migrate_get_peak_memory_usage() . 'MB',
      ), 'success');
    }
    $success = $this
      ->_backup_db_to_file($file, $settings);
    if (variable_get('backup_migrate_verbose')) {
      _backup_migrate_message('Finish peak memory usage: %mem', array(
        '%mem' => backup_migrate_get_peak_memory_usage() . 'MB',
      ), 'success');
    }

    // $this->unlock_tables($settings);
    return $success ? $file : FALSE;
  }

  /**
   * Restore to this source.
   */
  public function restore_from_file($file, &$settings) {
    $num = 0;
    $type = $this
      ->get_file_type_id();

    // Open the file using the file wrapper. Check that the dump is of the right type (allow .sql for legacy reasons).
    if ($file
      ->type_id() !== $this
      ->get_file_type_id() && $file
      ->type_id() !== 'sql') {
      _backup_migrate_message("Unable to restore from file %file because a %type file can't be restored to this database.", array(
        "%file" => $file
          ->filepath(),
        '%type' => $file
          ->type_id(),
      ), 'error');
    }
    else {
      backup_migrate_filters_invoke_all('pre_restore', $file, $settings);

      // Restore the database.
      $num = $this
        ->_restore_db_from_file($file, $settings);
      $settings->performed_action = $num ? t('%num SQL commands executed.', array(
        '%num' => $num,
      )) : '';
      backup_migrate_filters_invoke_all('post_restore', $file, $settings, $num);
    }
    return $num;
  }

  /**
   * Get the db connection for the specified db.
   */
  public function _get_db_connection() {
    if (!$this->connection) {
      $target = $key = '';
      $parts = explode(':', $this
        ->get_id());

      // One of the predefined databases (set in settings.php)
      if ($parts[0] == 'db') {
        $key = empty($parts[1]) ? 'default' : $parts[1];
        $target = empty($parts[2]) ? 'default' : $parts[2];
      }
      else {

        // If the url is specified build it into a connection info array.
        if (!empty($this->dest_url)) {
          $info = array(
            'driver' => empty($this->dest_url['scheme']) ? NULL : $this->dest_url['scheme'],
            'host' => empty($this->dest_url['host']) ? NULL : $this->dest_url['host'],
            'port' => empty($this->dest_url['port']) ? NULL : $this->dest_url['port'],
            'username' => empty($this->dest_url['user']) ? NULL : $this->dest_url['user'],
            'password' => empty($this->dest_url['pass']) ? NULL : $this->dest_url['pass'],
            'database' => empty($this->dest_url['path']) ? NULL : $this->dest_url['path'],
          );
          $key = uniqid('backup_migrate_tmp_');
          $target = 'default';
          Database::addConnectionInfo($key, $target, $info);
        }
        else {
          $key = $target = 'default';
        }
      }
      if ($target && $key) {
        $this->connection = Database::getConnection($target, $key);
      }
    }
    return $this->connection;
  }

  /**
   * Backup the databases to a file.
   */
  public function _backup_db_to_file($file, $settings) {

    // Must be overridden.
  }

  /**
   * Backup the databases to a file.
   */
  public function _restore_db_from_file($file, $settings) {

    // Must be overridden.
  }

  /**
   * Get a list of objects in the database.
   */
  public function get_object_names() {

    // Must be overridden.
    $out = $this
      ->_get_table_names();
    if (method_exists($this, '_get_view_names')) {
      $out += $this
        ->_get_view_names();
    }
    return $out;
  }

  /**
   * Get a list of tables in the database.
   */
  public function get_table_names() {

    // Must be overridden.
    $out = $this
      ->_get_table_names();
    return $out;
  }

  /**
   * Get a list of tables in the database.
   */
  public function _get_table_names() {

    // Must be overridden.
    return array();
  }

  /**
   * Lock the database in anticipation of a backup.
   */
  public function lock_tables($settings) {
    if ($settings->filters['utils_lock_tables']) {
      $tables = array();
      foreach ($this
        ->get_table_names() as $table) {

        // There's no need to lock excluded or structure only tables because it doesn't matter if they change.
        if (empty($settings->filters['exclude_tables']) || !in_array($table, (array) $settings->filters['exclude_tables'])) {
          $tables[] = $table;
        }
      }
      $this
        ->_lock_tables($tables);
    }
  }

  /**
   * Lock the list of given tables in the database.
   */
  public function _lock_tables($tables) {

    // Must be overridden.
  }

  /**
   * Unlock any tables that have been locked.
   */
  public function unlock_tables($settings) {
    if ($settings->filters['utils_lock_tables']) {
      $this
        ->_unlock_tables();
    }
  }

  /**
   * Unlock the list of given tables in the database.
   */
  public function _unlock_tables($tables) {

    // Must be overridden.
  }

  /**
   * Get the file type for to backup this destination to.
   */
  public function get_file_type_id() {
    return 'sql';
  }

  /**
   * Get the version info for the given DB.
   */
  public function _db_info() {
    return array(
      'type' => FALSE,
      'version' => t('Unknown'),
    );
  }

}

Members

Namesort descending Modifiers Type Description Overrides
backup_migrate_item::$settings_path public property
backup_migrate_item::$show_in_list public property
backup_migrate_item::$storage public property
backup_migrate_item::all_items public function Get all of the given items.
backup_migrate_item::decode_db_row public function Decode a loaded db row (unserialize necessary fields).
backup_migrate_item::delete public function Delete the item from the database.
backup_migrate_item::export public function Return as an exported array of values.
backup_migrate_item::from_array public function Load an existing item from an array.
backup_migrate_item::generate_id public function Return a random (very very likely unique) string id for a new item.
backup_migrate_item::get public function Get the member with the given key.
backup_migrate_item::get_actions public function Get the rendered action links for a destination.
backup_migrate_item::get_default_values public function Get the default values for standard parameters. 2
backup_migrate_item::get_id public function Get the primary id for this item (if any is set).
backup_migrate_item::get_list public function Get a table of all items of this type. 1
backup_migrate_item::get_list_header public function Get header for a lost of this type.
backup_migrate_item::get_machine_name_field public function Get the machine name field name from the schema.
backup_migrate_item::get_menu_items public function Get the menu items for manipulating this type. 2
backup_migrate_item::get_primary_key public function Get the primary key field title from the schema.
backup_migrate_item::get_schema public function Get the schema for the item type.
backup_migrate_item::get_serialized_fields public function Return the fields which must be serialized before saving to the db.
backup_migrate_item::get_settings_path public function Get the columns needed to list the type. 1
backup_migrate_item::item public function A particular item.
backup_migrate_item::item_exists public function A particular item.
backup_migrate_item::load_row public function Load an existing item from an database (serialized) array.
backup_migrate_item::revert_confirm_message public function The message to send to the user when confirming the deletion of the item.
backup_migrate_item::save public function Save the item to the database.
backup_migrate_item::set_id public function Set the primary id for this item (if any is set).
backup_migrate_item::show_in_list public function Get the columns needed to list the type.
backup_migrate_item::to_array public function Return as an array of values. 1
backup_migrate_item::unique_id public function Make sure this item has a unique id.
backup_migrate_item::_merge_defaults public function Merge parameters with the given defaults.
backup_migrate_item::__construct public function Set the basic info pulled from the db or generated programatically. 5
backup_migrate_location::$default_values public property Overrides backup_migrate_item::$default_values 1
backup_migrate_location::$subtype public property
backup_migrate_location::backup_settings_form_submit public function Submit the settings form. Any values returned will be saved.
backup_migrate_location::backup_settings_form_validate public function Get the form for the settings for this filter.
backup_migrate_location::can_read_file public function Determine if we can read the given file. 1
backup_migrate_location::create public function Create a new location of the correct type. Overrides backup_migrate_item::create
backup_migrate_location::delete_confirm_message public function Get the message to send to the user when confirming the deletion of the item. Overrides backup_migrate_item::delete_confirm_message 1
backup_migrate_location::file_types public function Retrieve a list of filetypes supported by this source/destination. 3
backup_migrate_location::get_action_links public function Get the action links for a location. Overrides backup_migrate_item::get_action_links 1
backup_migrate_location::get_list_column_info public function Get the columns needed to list the type. Overrides backup_migrate_item::get_list_column_info
backup_migrate_location::get_list_row public function Get a row of data to be used in a list of items of this type. Overrides backup_migrate_item::get_list_row 1
backup_migrate_location::get_name public function Get the name of the item. Overrides backup_migrate_item::get_name
backup_migrate_location::get_subtype_name public function Get the type name of this location for display to the user.
backup_migrate_location::glue_url public function Glue a URLs component parts back into a URL.
backup_migrate_location::op public function Does this location support the given operation.
backup_migrate_location::ops public function
backup_migrate_location::remove_op public function Remove the given op from the support list.
backup_migrate_location::restore_settings_default public function Get the form for the settings for this filter.
backup_migrate_location::restore_settings_form public function Get the form for the settings for this filter.
backup_migrate_location::restore_settings_form_submit public function Submit the settings form. Any values returned will be saved.
backup_migrate_location::restore_settings_form_validate public function Get the form for the settings for this filter.
backup_migrate_location::settings public function
backup_migrate_location::settings_default public function Get the form for the settings for this location type. 1
backup_migrate_location::settings_form public function Get the form for the settings for this location. 1
backup_migrate_location::settings_form_submit public function Submit the settings form. Any values returned will be saved. 1
backup_migrate_location::settings_form_validate public function Validate the form for the settings for this location. 1
backup_migrate_location::set_name public function
backup_migrate_location::set_url public function Break a URL into it's component parts.
backup_migrate_location::url public function Get a url from the parts.
backup_migrate_source::$db_table public property Overrides backup_migrate_location::$db_table
backup_migrate_source::$plural public property Overrides backup_migrate_location::$plural
backup_migrate_source::$singular public property Overrides backup_migrate_location::$singular
backup_migrate_source::$title_plural public property Overrides backup_migrate_location::$title_plural
backup_migrate_source::$title_singular public property Overrides backup_migrate_location::$title_singular
backup_migrate_source::$type_name public property Overrides backup_migrate_location::$type_name
backup_migrate_source::location_types public function Get the available location types. Overrides backup_migrate_location::location_types
backup_migrate_source::strings public function This function is not supposed to be called. Overrides backup_migrate_location::strings
backup_migrate_source_db::$connection public property
backup_migrate_source_db::$db_target public property
backup_migrate_source_db::$supported_ops public property Overrides backup_migrate_location::$supported_ops
backup_migrate_source_db::backup_settings_default public function Get the default settings for this object. Overrides backup_migrate_location::backup_settings_default
backup_migrate_source_db::backup_settings_form public function Get the form for the backup settings for this destination. Overrides backup_migrate_location::backup_settings_form
backup_migrate_source_db::backup_to_file public function Backup from this source.
backup_migrate_source_db::edit_form public function Destination configuration callback. Overrides backup_migrate_source_remote::edit_form
backup_migrate_source_db::edit_form_validate public function Validate the configuration form. Make sure the db info is valid. Overrides backup_migrate_item::edit_form_validate
backup_migrate_source_db::get_file_type_id public function Get the file type for to backup this destination to. 1
backup_migrate_source_db::get_object_names public function Get a list of objects in the database.
backup_migrate_source_db::get_table_names public function Get a list of tables in the database.
backup_migrate_source_db::lock_tables public function Lock the database in anticipation of a backup.
backup_migrate_source_db::restore_from_file public function Restore to this source.
backup_migrate_source_db::save_file public function Save the info by importing it into the database.
backup_migrate_source_db::type_name public function 1
backup_migrate_source_db::unlock_tables public function Unlock any tables that have been locked.
backup_migrate_source_db::_backup_db_to_file public function Backup the databases to a file. 1
backup_migrate_source_db::_db_info public function Get the version info for the given DB. 1
backup_migrate_source_db::_get_db_connection public function Get the db connection for the specified db. 1
backup_migrate_source_db::_get_table_names public function Get a list of tables in the database. 1
backup_migrate_source_db::_lock_tables public function Lock the list of given tables in the database. 1
backup_migrate_source_db::_restore_db_from_file public function Backup the databases to a file. 1
backup_migrate_source_db::_unlock_tables public function Unlock the list of given tables in the database. 1
backup_migrate_source_remote::edit_form_submit public function Submits the configuration form. Overrides backup_migrate_item::edit_form_submit
backup_migrate_source_remote::get_display_location public function The location to display is the url without the password. Overrides backup_migrate_location::get_display_location
backup_migrate_source_remote::get_location public function The location is a URI so parse it and store the parts. Overrides backup_migrate_location::get_location
backup_migrate_source_remote::set_location public function Returns the location with the password. Overrides backup_migrate_location::set_location