You are here

public function GdprSqlMysql::dump in General Data Protection Regulation 8.2

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

Execute a SQL dump and return the path to the resulting dump file.

Return value

mixed Bool or void.

File

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

Class

GdprSqlMysql
Class GdprSqlMysql.

Namespace

Drupal\gdpr_dump\Sql

Code

public function dump() {

  /** @var string|bool $file Path where dump file should be stored. If TRUE, generate a path based on usual backup directory and current date.*/
  $file = $this
    ->getOption('result-file');
  $fileSuffix = '';
  $tableSelection = $this
    ->getExpandedTableSelection($this
    ->getOptions(), $this
    ->listTables());
  $file = $this
    ->dumpFile($file);
  $cmd = trim($this
    ->dumpCmd($tableSelection));

  // Append the RENAME commands at the end.
  $renames = trim($this
    ->createRenameCommands($tableSelection));
  if (!empty($renames)) {

    // @todo: Cross-platform check.
    $cmd = '{ ' . $cmd . ' ; ' . $renames . ' }';
  }
  $pipefail = '';

  // Gzip the output from dump command(s) if requested.
  if ($this
    ->getOption('gzip')) {

    // See https://github.com/drush-ops/drush/issues/3816.
    $pipefail = $this
      ->getConfig()
      ->get('sh.pipefail', 'bash -c "set -o pipefail; {{cmd}}"');
    $cmd .= ' | gzip -f';
    $fileSuffix .= '.gz';
  }
  if ($file) {
    $file .= $fileSuffix;
    $cmd .= ' > ' . Escape::shellArg($file);
  }
  $cmd = $this
    ->addPipeFail($cmd, $pipefail);
  $process = Drush::shell($cmd, NULL, $this
    ->getEnv());

  // Avoid the php memory of saving stdout.
  $process
    ->disableOutput();

  // Show dump in real-time on stdout, for backward compat.
  $process
    ->run($process
    ->showRealtime());
  return $process
    ->isSuccessful() ? $file : FALSE;
}