vincent15000's avatar

What's wrong with my code with ZipArchive protected with a password ?

Hello,

I encounter some problems with this code to unzip an archive protected by a password and use the unzipped SQL file to restore the database.

if ($zip->open($archiveFullpath) === true) {
    if ($zip->setPassword($password)) {
        if ($zip->extractTo($this->pathForBackup)) {
            Artisan::call('migrate:fresh');

            $command = 'mysql';
            $command .= ' --host='.config('database.connections.mariadb.host');
            $command .= ' --user='.config('database.connections.mariadb.username');
            $command .= ' --password='.config('database.connections.mariadb.password');
            $command .= ' '.config('database.connections.mariadb.database').' < '.$fileFullpath;
            
            $process = Process::run($command);

            if (!$process->successful()) {
                Log::warning('Failed to restore the database');
                
                $code = 500;
            }

            session(['database_in_maintenance' => false]);

            unlink($fileFullpath);
        } else {
            ...
        }
    } else {
        ...
    }

    $zip->close();
    
    unlink($archiveFullpath);
} else {
    ...
}

When I backup the database on the server in production, it's impossible to use this backup to restore the database on my local computer.

I have logged the errorOutput of the process and I read this error.

ERROR at line 1: Unknown command '\-'..

When I have a look at the SQL file, I find this first line where you can see the \- characters.

/*!999999\- enable the sandbox mode */ 

But it's a comment.

How can I avoid this problem ?

Thanks for your help.

V

0 likes
3 replies
krisi_gjika's avatar
Level 14

"When I backup the database on the server in production, it's impossible to use this backup to restore the database on my local computer" - are you using the same mariadb version on your local computer?

1 like
vincent15000's avatar

@krisi_gjika No I don't use the same version.

I have found the problem : some MariaDB versions use a sandbox mode and add a line at the top of the SQL file and other version don't use it.

1 like
LaryAI's avatar
Level 58

The issue you're encountering seems to be related to the way the SQL file is being interpreted by the mysql command. The \ character in the comment might be causing the command to misinterpret the input.

One way to address this issue is to use the --comments option with the mysql command to ensure that comments are properly handled. Additionally, you should ensure that the SQL file is correctly formatted and that no special characters are causing issues.

Here's a revised version of your code with the --comments option added to the mysql command:

Make sure to replace the Process::run($command) with the appropriate method to execute shell commands in your environment. If you're using Symfony's Process component, it should look something like this:

use Symfony\Component\Process\Process;

$process = Process::fromShellCommandline($command);
$process->run();

This should help ensure that comments in your SQL file are properly handled and do not cause errors during the database restoration process. If the problem persists, you might want to inspect the SQL file for any other special characters or formatting issues that could be causing the mysql command to fail.

Please or to participate in this conversation.