You are here

public function Database::execute in DRD Agent 8.3

Same name and namespace in other branches
  1. 4.0.x src/Agent/Action/Database.php \Drupal\drd_agent\Agent\Action\Database::execute()

Execute an action.

Return value

mixed The response of the action as an array which will be encrypted before returned to DRD.

Overrides Base::execute

File

src/Agent/Action/Database.php, line 16

Class

Database
Provides a 'Database' download file.

Namespace

Drupal\drd_agent\Agent\Action

Code

public function execute() {
  $databases = CoreDatabase::getAllConnectionInfo();
  exec('mysqldump --version', $output, $ret);
  $mysql = $ret === 0;
  if ($mysql) {
    foreach ($databases as $key => $info) {
      foreach ($info as $target => $config) {
        $file = $this
          ->realPath($this->fileSystem
          ->tempnam('temporary://', implode('-', [
          'drd',
          'db',
          $target,
          $key,
          '',
        ])) . '.sql');
        $credentialsfile = $this
          ->realPath($this->fileSystem
          ->tempnam('temporary://', 'mysqldump'));
        $cmd = [
          'mysqldump',
          '--defaults-extra-file=' . $credentialsfile,
          $config['database'],
          '>' . $file,
        ];
        $credentials = [
          '[mysqldump]',
          'host = ' . $config['host'],
          'port = ' . $config['port'],
          'user = ' . $config['username'],
          'password = "' . $config['password'] . '"',
        ];
        file_put_contents($credentialsfile, implode("\n", $credentials));
        chmod($credentialsfile, 0600);
        exec(implode(' ', $cmd), $output, $ret);
        unlink($credentialsfile);
        $databases[$key][$target]['file'] = $file;
      }
    }
    return $databases;
  }
  return FALSE;
}