[SOLVED] mysqldump command fails in Laravel command

Issue

I have a Laravel command that backs up the MySQL database daily.

This is the command:

/**
 * Create a new command instance.
 *
 * @return void
 */
public function __construct()
{
    parent::__construct();

    $this->process = new Process([sprintf(
        "mysqldump -u%s -p'%s' %s > %s",
        config('database.connections.mysql.username'),
        config('database.connections.mysql.password'),
        config('database.connections.mysql.database'),
        storage_path('app/public/backups/backup.sql')
    )]);
}

/**
 * Execute the console command.
 *
 * @return mixed
 */
public function handle()
{
    try {
        $this->process->mustRun();

        $this->info('The database has been backed up successfully.');
    } catch (ProcessFailedException $exception) {
        logger()->debug($exception->getMessage());
        $this->error('The backup process has failed.');
    }

}

When I check what command actually runs, I see this:

/Users/jovan/path_to_project/mysqldump -uuser -p'passpass' project > /Users/jovan/path_to_project/storage/app/public/backups/backup.sql

It doesn’t work because, for some reason, it creates an absolute path for the mysqldump command (/Users/jovan/path_to_project/mysqldump).

When I change the command to the following and run it manually, it works alright:

mysqldump -uuser -p'passpass' project > /Users/jovan/path_to_project/storage/app/public/backups/backup.sql

How can I fix this?

Solution

You are using path for mysqldump that is not executable.

First, try to locate the mysqldump executable by performing: which mysqldump and then add the output (absolute path) to your CLI command content.

    $this->process = new Process([sprintf(
        "mysqldump -u%s -p'%s' %s > %s",
        config('database.connections.mysql.username'),
        config('database.connections.mysql.password'),
        config('database.connections.mysql.database'),
        storage_path('app/public/backups/backup.sql')
    )]);

into

    $this->process = new Process([sprintf(
        "/absolute/path/to/mysqldump -u%s -p'%s' %s > %s",
        config('database.connections.mysql.username'),
        config('database.connections.mysql.password'),
        config('database.connections.mysql.database'),
        storage_path('app/public/backups/backup.sql')
    )]);

Answered By – miloszowi

Answer Checked By – Candace Johnson (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published.