Skip to content

Commit

Permalink
Added gzip support for MySql (spatie#72)
Browse files Browse the repository at this point in the history
* Refactor output to standard output and then to file

This way its easier to add gzip support on the command. Echoing to the standard output is possible (see links). Sqlite was already doing this.

See examples of echoing to standard output:
https://docs.mongodb.com/manual/reference/program/mongodump/#examples
https://dev.mysql.com/doc/refman/8.0/en/mysqldump.html#mysqldump-option-examples
https://www.postgresql.org/docs/9.1/static/backup-dump.html

* Gzip compression for all dumpers
  • Loading branch information
glamorous authored and freekmurze committed Apr 27, 2018
1 parent 98a3b7d commit d114fb8
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 68 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to `db-dumper` will be documented in this file

## 2.10.0 - 2018-04-27

- add support for gzip to MySql, PostgreSql and Sqlite

## 2.9.0 - 2018-03-05

- add support for setting `--set-gtid-purged`
Expand Down
21 changes: 2 additions & 19 deletions src/Databases/MongoDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ class MongoDb extends DbDumper
/** @var null|string */
protected $collection = null;

/** @var bool */
protected $enableCompression = false;

/** @var null|string */
protected $authenticationDatabase = null;

Expand Down Expand Up @@ -71,16 +68,6 @@ public function setCollection(string $collection)
return $this;
}

/**
* @return \Spatie\DbDumper\Databases\MongoDb
*/
public function enableCompression()
{
$this->enableCompression = true;

return $this;
}

/**
* @param string $authenticationDatabase
*
Expand All @@ -105,7 +92,7 @@ public function getDumpCommand(string $filename) : string
$command = [
"'{$this->dumpBinaryPath}mongodump'",
"--db {$this->dbName}",
"--archive=$filename",
"--archive",
];

if ($this->userName) {
Expand All @@ -132,10 +119,6 @@ public function getDumpCommand(string $filename) : string
$command[] = "--authenticationDatabase {$this->authenticationDatabase}";
}

if ($this->enableCompression) {
$command[] = '--gzip';
}

return implode(' ', $command);
return $this->echoToFile(implode(' ', $command), $filename);
}
}
4 changes: 1 addition & 3 deletions src/Databases/MySql.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,6 @@ public function getDumpCommand(string $dumpFile, string $temporaryCredentialsFil
$command[] = '--set-gtid-purged='.$this->setGtidPurged;
}

$command[] = "--result-file=\"{$dumpFile}\"";

if (! $this->dbNameWasSetAsExtraOption) {
$command[] = $this->dbName;
}
Expand All @@ -210,7 +208,7 @@ public function getDumpCommand(string $dumpFile, string $temporaryCredentialsFil
$command[] = "--tables {$includeTables}";
}

return implode(' ', $command);
return $this->echoToFile(implode(' ', $command), $dumpFile);
}

public function getContentsOfCredentialsFile(): string
Expand Down
3 changes: 1 addition & 2 deletions src/Databases/PostgreSql.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ public function getDumpCommand(string $dumpFile): string
"-U {$this->userName}",
'-h '.($this->socket === '' ? $this->host : $this->socket),
"-p {$this->port}",
"--file=\"{$dumpFile}\"",
];

if ($this->useInserts) {
Expand All @@ -88,7 +87,7 @@ public function getDumpCommand(string $dumpFile): string
$command[] = '-T '.implode(' -T ', $this->excludeTables);
}

return implode(' ', $command);
return $this->echoToFile(implode(' ', $command), $dumpFile);
}

public function getContentsOfCredentialsFile(): string
Expand Down
8 changes: 4 additions & 4 deletions src/Databases/Sqlite.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ public function dumpToFile(string $dumpFile)
*/
public function getDumpCommand(string $dumpFile): string
{
return sprintf(
"echo 'BEGIN IMMEDIATE;\n.dump' | '%ssqlite3' --bail '%s' >'%s'",
$command = sprintf(
"echo 'BEGIN IMMEDIATE;\n.dump' | '%ssqlite3' --bail '%s'",
$this->dumpBinaryPath,
$this->dbName,
$dumpFile
$this->dbName
);
return $this->echoToFile($command, $dumpFile);
}
}
25 changes: 25 additions & 0 deletions src/DbDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ abstract class DbDumper
/** @var array */
protected $extraOptions = [];

/** @var bool */
protected $enableCompression = false;

public static function create()
{
return new static();
Expand Down Expand Up @@ -214,6 +217,16 @@ public function addExtraOption(string $extraOption)
return $this;
}

/**
* @return $this
*/
public function enableCompression()
{
$this->enableCompression = true;

return $this;
}

abstract public function dumpToFile(string $dumpFile);

protected function checkIfDumpWasSuccessFul(Process $process, string $outputFile)
Expand All @@ -230,4 +243,16 @@ protected function checkIfDumpWasSuccessFul(Process $process, string $outputFile
throw DumpFailed::dumpfileWasEmpty();
}
}

/**
* @param string $command
*
* @return string
*/
protected function echoToFile(string $command, string $dumpFile)
{
$compression = $this->enableCompression ? ' | gzip' : '';

return $command . $compression . ' > ' . $dumpFile;
}
}
24 changes: 12 additions & 12 deletions tests/MongoDbTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function it_can_generate_a_dump_command()
->getDumpCommand('dbname.gz');

$this->assertSame('\'mongodump\' --db dbname'
.' --archive=dbname.gz --host localhost --port 27017', $dumpCommand);
.' --archive --host localhost --port 27017 > dbname.gz', $dumpCommand);
}

/** @test */
Expand All @@ -42,7 +42,7 @@ public function it_can_generate_a_dump_command_with_compression_enabled()
->getDumpCommand('dbname.gz');

$this->assertSame('\'mongodump\' --db dbname'
.' --archive=dbname.gz --host localhost --port 27017 --gzip', $dumpCommand);
.' --archive --host localhost --port 27017 | gzip > dbname.gz', $dumpCommand);
}

/** @test */
Expand All @@ -54,8 +54,8 @@ public function it_can_generate_a_dump_command_with_username_and_password()
->setPassword('password')
->getDumpCommand('dbname.gz');

$this->assertSame('\'mongodump\' --db dbname --archive=dbname.gz'
.' --username \'username\' --password \'password\' --host localhost --port 27017', $dumpCommand);
$this->assertSame('\'mongodump\' --db dbname --archive'
.' --username \'username\' --password \'password\' --host localhost --port 27017 > dbname.gz', $dumpCommand);
}

/** @test */
Expand All @@ -67,8 +67,8 @@ public function it_can_generate_a_command_with_custom_host_and_port()
->setPort(27018)
->getDumpCommand('dbname.gz');

$this->assertSame('\'mongodump\' --db dbname --archive=dbname.gz'
.' --host mongodb.test.com --port 27018', $dumpCommand);
$this->assertSame('\'mongodump\' --db dbname --archive'
.' --host mongodb.test.com --port 27018 > dbname.gz', $dumpCommand);
}

/** @test */
Expand All @@ -79,8 +79,8 @@ public function it_can_generate_a_backup_command_for_a_single_collection()
->setCollection('mycollection')
->getDumpCommand('dbname.gz');

$this->assertSame('\'mongodump\' --db dbname --archive=dbname.gz'
.' --host localhost --port 27017 --collection mycollection', $dumpCommand);
$this->assertSame('\'mongodump\' --db dbname --archive'
.' --host localhost --port 27017 --collection mycollection > dbname.gz', $dumpCommand);
}

/** @test */
Expand All @@ -91,8 +91,8 @@ public function it_can_generate_a_dump_command_with_custom_binary_path()
->setDumpBinaryPath('/custom/directory')
->getDumpCommand('dbname.gz');

$this->assertSame('\'/custom/directory/mongodump\' --db dbname --archive=dbname.gz'
.' --host localhost --port 27017', $dumpCommand);
$this->assertSame('\'/custom/directory/mongodump\' --db dbname --archive'
.' --host localhost --port 27017 > dbname.gz', $dumpCommand);
}

/** @test */
Expand All @@ -103,7 +103,7 @@ public function it_can_generate_a_dump_command_with_authentication_database()
->setAuthenticationDatabase('admin')
->getDumpCommand('dbname.gz');

$this->assertSame('\'mongodump\' --db dbname --archive=dbname.gz'
.' --host localhost --port 27017 --authenticationDatabase admin', $dumpCommand);
$this->assertSame('\'mongodump\' --db dbname --archive'
.' --host localhost --port 27017 --authenticationDatabase admin > dbname.gz', $dumpCommand);
}
}
45 changes: 29 additions & 16 deletions tests/MySqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,20 @@ public function it_can_generate_a_dump_command()
->setPassword('password')
->getDumpCommand('dump.sql', 'credentials.txt');

$this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert --result-file="dump.sql" dbname', $dumpCommand);
$this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert dbname > dump.sql', $dumpCommand);
}

/** @test */
public function it_can_generate_a_dump_command_with_compression_enabled()
{
$dumpCommand = MySql::create()
->setDbName('dbname')
->setUserName('username')
->setPassword('password')
->enableCompression()
->getDumpCommand('dump.sql', 'credentials.txt');

$this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert dbname | gzip > dump.sql', $dumpCommand);
}

/** @test */
Expand All @@ -45,7 +58,7 @@ public function it_can_generate_a_dump_command_without_using_comments()
->dontSkipComments()
->getDumpCommand('dump.sql', 'credentials.txt');

$this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --extended-insert --result-file="dump.sql" dbname', $dumpCommand);
$this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --extended-insert dbname > dump.sql', $dumpCommand);
}

/** @test */
Expand All @@ -58,7 +71,7 @@ public function it_can_generate_a_dump_command_without_using_extended_insterts()
->dontUseExtendedInserts()
->getDumpCommand('dump.sql', 'credentials.txt');

$this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --skip-extended-insert --result-file="dump.sql" dbname', $dumpCommand);
$this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --skip-extended-insert dbname > dump.sql', $dumpCommand);
}

/** @test */
Expand All @@ -71,7 +84,7 @@ public function it_can_generate_a_dump_command_with_custom_binary_path()
->setDumpBinaryPath('/custom/directory')
->getDumpCommand('dump.sql', 'credentials.txt');

$this->assertSame('\'/custom/directory/mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert --result-file="dump.sql" dbname', $dumpCommand);
$this->assertSame('\'/custom/directory/mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert dbname > dump.sql', $dumpCommand);
}

/** @test */
Expand All @@ -84,7 +97,7 @@ public function it_can_generate_a_dump_command_without_using_extending_inserts()
->dontUseExtendedInserts()
->getDumpCommand('dump.sql', 'credentials.txt');

$this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --skip-extended-insert --result-file="dump.sql" dbname', $dumpCommand);
$this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --skip-extended-insert dbname > dump.sql', $dumpCommand);
}

/** @test */
Expand All @@ -97,7 +110,7 @@ public function it_can_generate_a_dump_command_using_single_transaction()
->useSingleTransaction()
->getDumpCommand('dump.sql', 'credentials.txt');

$this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert --single-transaction --result-file="dump.sql" dbname', $dumpCommand);
$this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert --single-transaction dbname > dump.sql', $dumpCommand);
}

/** @test */
Expand All @@ -110,7 +123,7 @@ public function it_can_generate_a_dump_command_with_a_custom_socket()
->setSocket(1234)
->getDumpCommand('dump.sql', 'credentials.txt');

$this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert --socket=1234 --result-file="dump.sql" dbname', $dumpCommand);
$this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert --socket=1234 dbname > dump.sql', $dumpCommand);
}

/** @test */
Expand All @@ -123,7 +136,7 @@ public function it_can_generate_a_dump_command_for_specific_tables_as_array()
->includeTables(['tb1', 'tb2', 'tb3'])
->getDumpCommand('dump.sql', 'credentials.txt');

$this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert --result-file="dump.sql" dbname --tables tb1 tb2 tb3', $dumpCommand);
$this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert dbname --tables tb1 tb2 tb3 > dump.sql', $dumpCommand);
}

/** @test */
Expand All @@ -136,7 +149,7 @@ public function it_can_generate_a_dump_command_for_specific_tables_as_string()
->includeTables('tb1 tb2 tb3')
->getDumpCommand('dump.sql', 'credentials.txt');

$this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert --result-file="dump.sql" dbname --tables tb1 tb2 tb3', $dumpCommand);
$this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert dbname --tables tb1 tb2 tb3 > dump.sql', $dumpCommand);
}

/** @test */
Expand All @@ -163,7 +176,7 @@ public function it_can_generate_a_dump_command_excluding_tables_as_array()
->getDumpCommand('dump.sql', 'credentials.txt');

$this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert '.
'--ignore-table=dbname.tb1 --ignore-table=dbname.tb2 --ignore-table=dbname.tb3 --result-file="dump.sql" dbname', $dumpCommand);
'--ignore-table=dbname.tb1 --ignore-table=dbname.tb2 --ignore-table=dbname.tb3 dbname > dump.sql', $dumpCommand);
}

/** @test */
Expand All @@ -177,7 +190,7 @@ public function it_can_generate_a_dump_command_excluding_tables_as_string()
->getDumpCommand('dump.sql', 'credentials.txt');

$this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert '.
'--ignore-table=dbname.tb1 --ignore-table=dbname.tb2 --ignore-table=dbname.tb3 --result-file="dump.sql" dbname', $dumpCommand);
'--ignore-table=dbname.tb1 --ignore-table=dbname.tb2 --ignore-table=dbname.tb3 dbname > dump.sql', $dumpCommand);
}

/** @test */
Expand Down Expand Up @@ -230,7 +243,7 @@ public function it_can_add_extra_options()
->addExtraOption('--another-extra-option="value"')
->getDumpCommand('dump.sql', 'credentials.txt');

$this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert --extra-option --another-extra-option="value" --result-file="dump.sql" dbname', $dumpCommand);
$this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert --extra-option --another-extra-option="value" dbname > dump.sql', $dumpCommand);
}

/** @test */
Expand All @@ -252,7 +265,7 @@ public function it_can_set_db_name_as_an_extra_options()
->addExtraOption('--databases dbname')
->getDumpCommand('dump.sql', 'credentials.txt');

$this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert --extra-option --another-extra-option="value" --databases dbname --result-file="dump.sql"', $dumpCommand);
$this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert --extra-option --another-extra-option="value" --databases dbname > dump.sql', $dumpCommand);
}

/** @test */
Expand Down Expand Up @@ -287,7 +300,7 @@ public function it_can_generate_a_dump_command_excluding_tables_as_array_when_db
->getDumpCommand('dump.sql', 'credentials.txt');

$this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert '.
'--ignore-table=dbname.tb1 --ignore-table=dbname.tb2 --ignore-table=dbname.tb3 --databases dbname --result-file="dump.sql"', $dumpCommand);
'--ignore-table=dbname.tb1 --ignore-table=dbname.tb2 --ignore-table=dbname.tb3 --databases dbname > dump.sql', $dumpCommand);
}

/** @test */
Expand All @@ -301,7 +314,7 @@ public function it_can_generate_a_dump_command_excluding_tables_as_string_when_d
->getDumpCommand('dump.sql', 'credentials.txt');

$this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert '.
'--ignore-table=dbname.tb1 --ignore-table=dbname.tb2 --ignore-table=dbname.tb3 --databases dbname --result-file="dump.sql"', $dumpCommand);
'--ignore-table=dbname.tb1 --ignore-table=dbname.tb2 --ignore-table=dbname.tb3 --databases dbname > dump.sql', $dumpCommand);
}

/** @test */
Expand All @@ -314,6 +327,6 @@ public function it_can_generate_a_dump_command_with_set_gtid_purged()
->setGtidPurged('OFF')
->getDumpCommand('dump.sql', 'credentials.txt');

$this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert --set-gtid-purged=OFF --result-file="dump.sql" dbname', $dumpCommand);
$this->assertSame('\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert --set-gtid-purged=OFF dbname > dump.sql', $dumpCommand);
}
}
Loading

0 comments on commit d114fb8

Please sign in to comment.