Skip to content

Commit fcfd83c

Browse files
Merge pull request #3 from wysiwyg-software-design/master
new Mysql-Database Dump Step
2 parents aa3a519 + e5b15b9 commit fcfd83c

File tree

3 files changed

+91
-1
lines changed

3 files changed

+91
-1
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace Breadlesscode\Backups\Step;
4+
5+
use Neos\Flow\Annotations as Flow;
6+
7+
class MysqlDatabaseExportStep extends StepAbstract
8+
{
9+
/**
10+
* Folder name in backup file
11+
*
12+
* @var string
13+
*/
14+
protected $name = 'MySqlDatabaseExport';
15+
16+
/**
17+
* @Flow\InjectConfiguration(path="persistence.backendOptions", package="Neos.Flow")
18+
* @var array
19+
*/
20+
protected $dbConfig;
21+
22+
public function backup(): bool
23+
{
24+
$mysqlDumpPath = $this->config['mysqlDumpBinPath'] ?? 'mysqldump';
25+
$mysqlDumpOptions = $this->config['mysqlDumpOptions'] ?? [];
26+
27+
$command = vsprintf('%s -h %s -u %s -p%s %s %s 2>/dev/null 1> %s', [
28+
$mysqlDumpPath,
29+
escapeshellarg($this->dbConfig['host']),
30+
escapeshellarg($this->dbConfig['user']),
31+
escapeshellarg($this->dbConfig['password']),
32+
escapeshellarg($this->dbConfig['dbname']),
33+
implode(' ', $mysqlDumpOptions),
34+
$this->getFilenameForBackup()
35+
]);
36+
37+
exec($command);
38+
39+
return true;
40+
}
41+
42+
public function restore(): bool
43+
{
44+
$mysqlPath = $this->config['mysqlBinPath'] ?? 'mysql';
45+
46+
$command = vsprintf('%s -h %s -u %s -p%s %s 2>/dev/null < %s', [
47+
$mysqlPath,
48+
escapeshellarg($this->dbConfig['host']),
49+
escapeshellarg($this->dbConfig['user']),
50+
escapeshellarg($this->dbConfig['password']),
51+
escapeshellarg($this->dbConfig['dbname']),
52+
$this->getFilenameForBackup()
53+
]);
54+
55+
exec($command);
56+
57+
return true;
58+
}
59+
60+
public function getRestoreWarning(): ?string
61+
{
62+
return 'The database "' . $this->dbConfig['dbname'] . '" will be overwritten:' . PHP_EOL;
63+
}
64+
65+
protected function getFilenameForBackup(): string
66+
{
67+
$backupPath = $this->getBackupStepPath();
68+
return $backupPath . '/' . $this->dbConfig['dbname'] . '.sql';
69+
}
70+
}

Classes/Step/MysqlTableExportStep.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,19 @@ class MysqlTableExportStep extends StepAbstract
2121
public function backup(): bool
2222
{
2323
$mysqlDumpPath = $this->config['mysqlDumpBinPath'] ?? 'mysqldump';
24+
$mysqlDumpOptions = $this->config['mysqlDumpOptions'] ?? [];
2425
$backupPath = $this->getBackupStepPath();
2526

2627
foreach ($this->config['tables'] as $table) {
2728
$outputFileName = $backupPath.'/'.$table.'.sql';
28-
$command = vsprintf('%s -h %s -u %s -p%s %s %s 2>/dev/null 1> %s', [
29+
$command = vsprintf('%s -h %s -u %s -p%s %s %s %s 2>/dev/null 1> %s', [
2930
$mysqlDumpPath,
3031
escapeshellarg($this->dbConfig['host']),
3132
escapeshellarg($this->dbConfig['user']),
3233
escapeshellarg($this->dbConfig['password']),
3334
escapeshellarg($this->dbConfig['dbname']),
3435
$table,
36+
implode(' ', $mysqlDumpOptions),
3537
$outputFileName
3638
]);
3739
exec($command);

Documentation/steps.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* [File/Directory copy step](#file-directory-copy-step)
44
* [Site export step](#site-export-step)
55
* [MySQL table export step](#mysql-table-export-step)
6+
* [MySQL database export step](#mysql-database-export-step)
67

78

89
## File export step
@@ -45,3 +46,20 @@ Breadlesscode:
4546
tables:
4647
- neos_flow_security_account
4748
```
49+
50+
## MySQL database export step
51+
52+
This step exports a whole MySql-Database via the `mysqldump`-Binary.
53+
54+
Example configuration:
55+
```yaml
56+
Breadlesscode:
57+
Backups:
58+
steps:
59+
'Breadlesscode\Backups\Step\MysqlDatabaseExportStep':
60+
mysqlDumpBinPath: 'mysqldump'
61+
mysqlBinPath: 'mysql'
62+
mysqlDumpOptions:
63+
- --single-transaction
64+
- --skip-extended-insert
65+
```

0 commit comments

Comments
 (0)