GdprDumpGdprSqlMysql.inc in General Data Protection Regulation 7
File
modules/gdpr_dump/inc/GdprDumpGdprSqlMysql.inc
View source
<?php
use Drush\Sql\Sqlmysql;
class GdprDumpGdprSqlMysql extends Sqlmysql {
public function dump($file = '') {
$file_suffix = '';
$table_selection = $this
->get_expanded_table_selection();
$file = $this
->dumpFile($file);
$cmd = '{ ';
$cmd .= $this
->dumpCmd($table_selection);
$cmd .= ' ; ' . $this
->createRenameCommands($table_selection) . '}';
if (drush_get_option('gzip')) {
$cmd .= ' | gzip -f';
$file_suffix .= '.gz';
}
if ($file) {
$file .= $file_suffix;
$cmd .= ' > ' . drush_escapeshellarg($file);
}
if (drush_op_system($cmd) === 0) {
if ($file) {
drush_log(dt('Database dump saved to !path', [
'!path' => $file,
]), 'success');
drush_backend_set_result($file);
}
}
else {
return drush_set_error('DRUSH_SQL_DUMP_FAIL', 'Database dump failed');
}
}
protected function createRenameCommands(array $tableSelection) {
$gdprOptions = variable_get('gdpr_dump_table_map', []);
$emptyTables = variable_get('gdpr_dump_empty_tables', []);
$sensitiveDataTables = \array_keys($gdprOptions);
$skipTables = \array_merge($tableSelection['skip'], $tableSelection['structure']);
$skipTables = \array_flip($skipTables);
$skipTables = $skipTables + $emptyTables;
$command = '';
foreach ($sensitiveDataTables as $table) {
if (\array_key_exists($table, $skipTables)) {
continue;
}
$clone = GdprDumpGdprSqlDump::GDPR_TABLE_PREFIX . $table;
$rename = "RENAME TABLE \\`{$clone}\\` TO \\`{$table}\\`;";
if (drush_get_context('DRUSH_VERBOSE') || drush_get_context('DRUSH_SIMULATE')) {
drush_print("Adding rename command: '{$rename}'", 0, STDERR);
}
$command .= " ( echo \"{$rename}\" ); ";
}
return $command;
}
public function dumpCmd($tableSelection) {
$gdprOptions = variable_get('gdpr_dump_table_map', []);
$emptyTables = array_keys(variable_get('gdpr_dump_empty_tables', []));
$sensitiveDataTables = \array_keys($gdprOptions);
$multipleCommands = FALSE;
$skipTables = $tableSelection['skip'];
$structureTables = $tableSelection['structure'];
$structureTables = \array_merge($emptyTables, $structureTables);
$tables = $tableSelection['tables'];
$ignores = [];
$skipTables = \array_merge($structureTables, $skipTables);
$skipTables = \array_merge($sensitiveDataTables, $skipTables);
$dataOnly = drush_get_option('data-only');
$orderedDump = drush_get_option('ordered-dump');
$exec = 'mysqldump ';
$onlyDbName = \str_replace('--database=', ' ', $this
->creds());
$exec .= $onlyDbName;
$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 {
foreach (array_unique($skipTables) as $table) {
$ignores[] = '--ignore-table=' . $this->db_spec['database'] . '.' . $table;
$multipleCommands = TRUE;
}
$exec .= ' ' . \implode(' ', $ignores);
if (!empty($structureTables)) {
$exec .= ' && mysqldump ' . $onlyDbName . " --no-data {$extra} " . \implode(' ', $structureTables);
$multipleCommands = TRUE;
}
}
return $multipleCommands ? "({$exec})" : $exec;
}
}