1 year ago
#361187
commadelimited
Laravel command to connect to remote database via SSH tunnel, generate MySQL dump, and download
I'm building a Laravel application which needs to connect to a remote database, through an SSH tunnel, generate a MySQL dump, then download it to the machine running Laravel. I'm writing it in a command so that I can run it nightly.
I've got Kernel.php updated:
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Laravel\Lumen\Console\Kernel as ConsoleKernel;
use App\Console\Commands\ImportDatabase;
class Kernel extends ConsoleKernel {
protected $commands = [ ImportDatabase::class, ];
protected function schedule(Schedule $schedule) { }
}
And my command file stubbed out.
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
class ImportDatabase extends Command {
protected $signature = 'import:database';
protected $description = 'Run the initial import of remote database';
public function handle() {
// connect to remote server via SSH tunnel
// execute sql dump command on remote RDS instance, save to SSH machine
// download SQL file to "local" machine (box running Laravel)
// execute SQL dump locally
// close SSH tunnel
}
}
I've read on various sites that I can execute an SSH command using Laravel, but that doesn't appear to be included in the 9.x version (although I did find mention of it in 4.x). The package spatie/ssh
seems like it might be a good option. But while the docs are pretty good it's not immediately clear if you can execute a series of commands either synchronously, or by reusing the same connection.
I'm still learning Laravel, so I'd love to some input on whether my workflow makes sense, and if so how I might accomplish it.
laravel
ssh
ssh-tunnel
spatie-ssh
0 Answers
Your Answer