mysqldump-php icon indicating copy to clipboard operation
mysqldump-php copied to clipboard

Provide a restore method

Open sergiokessler opened this issue 3 years ago • 8 comments

Provide a restore method, please?

sergiokessler avatar Dec 13 '21 16:12 sergiokessler

What this method can do?

liamka avatar Dec 14 '21 11:12 liamka

something like this: (minus the bugs people will surely spot given enough eyeballs)


$db = new PDO($db_dsn, $db_user, $db_pass, $db_options);

// Temporary variable, used to store current query
$templine = '';
$handle = fopen($dump_file , 'r');
if ($handle) {
    while (!feof($handle)) { // Loop through each line
        $line = trim(fgets($handle));
        // Skip it if it's a comment
        if (substr($line, 0, 2) == '--' || $line == '') {
            continue;
        }

        // Skip it if it's a DEFINER
        // if (strpos($line, 'DEFINER') !== false) {
        //     $line = '';
        // }
        
        // Add this line to the current segment
        $templine .= $line;

        // If it has a semicolon at the end, it's the end of the query
        if (substr(trim($line), -1, 1) == ';') {
            // Perform the query
            $db->query($templine);
            // Reset temp variable to empty
            $templine = '';
        }
    }
    fclose($handle);
}

sergiokessler avatar Dec 14 '21 16:12 sergiokessler

https://github.com/ifsnop/mysqldump-php/wiki/Importing-dump-from-php

phpony avatar Dec 14 '21 16:12 phpony

yup, add that code to the class as a restore method

sergiokessler avatar Dec 14 '21 16:12 sergiokessler

The example uses mysqli, if you transform it to use PDO I could certainly include it.

yup, add that to the class as a restore method

ifsnop avatar Dec 14 '21 21:12 ifsnop

The example uses mysqli, if you transform it to use PDO I could certainly

my posted code (see 4 comments above) uses PDO...

(and I'm using that code in production, but I think it belongs to this class)

sergiokessler avatar Dec 15 '21 01:12 sergiokessler

To be honest, I'm not a fan of all the examples provided, because:

  1. There's no memory control. Whole query is being loaded into memory, even if it's a huge comma separated 300mb table values dump.
  2. There's no error control. What happens if 1234th query returns execution error (for example, table already exists, or have duplicate keys)?
  3. It's slow. For tiny databases this can be OK, but on large scale this approach can't compete with simple streaming dump file to mysql.

If we talk about proper importing function built into mysqldump-php, we should not go with a simple slow solution, it sould be a solid mysqlimport competitor. Otherwise it's not worth it.

phpony avatar Dec 21 '21 08:12 phpony

Hi Volkov,

  1. No. The file is opened and read line by line.
  2. Feel free to post an enhanced version, but today people are using this code (this is one of the advantages of having this code in the library, there are more people reviewing it and able to post patches)
  3. No one is forcing you to use it, you can choose any other method, don't worry.

I think it should start simple, and growing from there...

sergiokessler avatar Dec 21 '21 14:12 sergiokessler

I merged the proposed solution, since not everyone has mysql client to be able to import in a shared hosting.

ifsnop avatar Feb 09 '23 01:02 ifsnop