I
I
Ivan2020-12-24 16:14:49
Laravel
Ivan, 2020-12-24 16:14:49

How to correctly form a request to restore a database from a backup for new Process from Symphony?

There is a method that restores a database from a backup. Looks like this:

private function restoreSnapShot()
  {
    $snapshot = $this->option('snapshot');
    if (!$snapshot) {
      $this->error("Snapshot option is required.");
    }

    try {
      $storage = Storage::disk('local');
      $found = $storage->get('/backups/' . $snapshot . '.sql');
      $tempLocation = '/tmp/' . config('database.connections.mysql.database') . '_' . date("Y-m-d_Hi") . '.sql';

      // create a temp file
      $bytes_written = File::put($tempLocation, $found);
      if ($bytes_written === false) {
        $this->info("Error writing to file: " . $tempLocation);
      }

      // run the cli job
       $this->process = new Process([
        'mysql',
        '--host=' . config('database.connections.mysql.host'),
        '--user=' . config('database.connections.mysql.username'),
        '--password=' . config('database.connections.mysql.password'),
        config('database.connections.mysql.database'),
        $tempLocation
      ]);
      $this->process->mustRun();


      if ($this->process->isSuccessful()) {
        $this->info("Restored snapshot: " . $snapshot);
      }
    } catch (\Exception $e) {
      $this->info('File Not Found: ' . $e->getMessage());
    }
  }

All trouble as I understood in request. Takes off in eksepshn. I tried explicitly
$this->process = new Process([" mysql -u user -pPassword < mydatabase.sql"])
, doesn't work either.
What could be wrong here, how to fix it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Medvedev, 2020-12-27
@Djonson86

$this->process = Process::fromShellCommandline( 'mysql -u $username -p"$password" $database < $file' );
$this->process->run( null, [
  'username' => $username,
  'password' => $password,
  'database' => $database,
  'file'     => $file,
] );

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question