You are here

public function GdprSqlMysql::dumpCmd in General Data Protection Regulation 8

Same name and namespace in other branches
  1. 8.2 modules/gdpr_dump/src/Sql/GdprSqlMysql.php \Drupal\gdpr_dump\Sql\GdprSqlMysql::dumpCmd()
  2. 3.0.x modules/gdpr_dump/src/Sql/GdprSqlMysql.php \Drupal\gdpr_dump\Sql\GdprSqlMysql::dumpCmd()

Build bash for dumping a database.

Return value

string One or more mysqldump/pg_dump/sqlite3/etc statements that are ready for executing. If multiple statements are needed, enclose in parenthesis.

1 call to GdprSqlMysql::dumpCmd()
GdprSqlMysql::dump in modules/gdpr_dump/src/Sql/GdprSqlMysql.php
Execute a SQL dump and return the path to the resulting dump file.

File

modules/gdpr_dump/src/Sql/GdprSqlMysql.php, line 128

Class

GdprSqlMysql
Class GdprSqlMysql.

Namespace

Drupal\gdpr_dump\Sql

Code

public function dumpCmd($tableSelection) {
  $multipleCommands = FALSE;
  $skipTables = $tableSelection['skip'];
  $structureTables = $tableSelection['structure'];
  $structureTables = \array_merge($this->tablesToSkip, $structureTables);
  $tables = $tableSelection['tables'];
  $ignores = [];
  $skipTables = \array_merge($structureTables, $skipTables);

  // Skip tables with sensitive data.
  $skipTables = \array_merge(\array_keys($this->tablesToAnonymize), $skipTables);
  $dataOnly = drush_get_option('data-only');

  // The ordered-dump option is only supported by MySQL for now.
  // @todo add documentation once a hook for drush_get_option_help() is available.
  // @see drush_get_option_help() in drush.inc
  $orderedDump = drush_get_option('ordered-dump');
  $exec = 'mysqldump ';

  // Mysqldump wants 'databasename' instead of
  // 'database=databasename' for no good reason.
  $onlyDbName = \str_replace('--database=', ' ', $this
    ->creds());
  $exec .= $onlyDbName;

  // We had --skip-add-locks here for a while to help people with
  // insufficient permissions, but removed it because it slows down the
  // import a lot.  See http://drupal.org/node/1283978
  $extra = ' --no-autocommit --single-transaction --opt -Q';
  if (NULL !== $dataOnly) {
    $extra .= ' --no-create-info';
  }
  if (NULL !== $orderedDump) {
    $extra .= ' --skip-extended-insert --order-by-primary';
  }
  if ($option = drush_get_option('extra', $this->query_extra)) {
    $extra .= " {$option}";
  }
  $exec .= $extra;
  if (!empty($tables)) {
    $exec .= ' ' . \implode(' ', $tables);
  }
  else {

    // @todo: Maybe use --ignore-table={db.table1,db.table2,...} syntax.
    // Append the ignore-table options.
    foreach ($skipTables as $table) {
      $ignores[] = '--ignore-table=' . $this->db_spec['database'] . '.' . $table;
      $multipleCommands = TRUE;
    }
    $exec .= ' ' . \implode(' ', $ignores);

    // Run mysqldump again and append output
    // if we need some structure only tables.
    if (!empty($structureTables)) {
      $exec .= ' && mysqldump ' . $onlyDbName . " --no-data {$extra} " . \implode(' ', $structureTables);
      $multipleCommands = TRUE;
    }
  }
  return $multipleCommands ? "({$exec})" : $exec;
}