Creating command to backup MySql database in Laravel



PHP Snippet 1:

  php artisan backup:run --only-db

PHP Snippet 2:

   DB_CONNECTION=mysql
   DB_HOST=127.0.0.1
   DB_PORT=3306
   DB_DATABASE=laravel
   DB_USERNAME=root
   DB_PASSWORD=

PHP Snippet 3:

$name = config('app.name');
$name = trim($name);
$name = Str::slug($name, '-');
$filename = $name . ".sql";

$DUMP_PATH = config('app.DUMP_PATH');
$DB_USERNAME = config('database.connections.mysql.username');
$DB_PASSWORD = config('database.connections.mysql.password');
$DB_HOST = config('database.connections.mysql.host');
$DB_PORT = config('database.connections.mysql.port');
$DB_DATABASE = config('database.connections.mysql.database');

$command = "".$DUMP_PATH." --user=" . $DB_USERNAME . " --password=" . $DB_PASSWORD . " --host=" . $DB_HOST . " " . $DB_DATABASE . "  > " . storage_path() . "/app/backup/" . $filename;
    $returnVar = NULL;
    $output = NULL;
    exec($command, $output, $returnVar);