Skip to content

Commit 9d3d5ed

Browse files
author
Alex Schulte
committed
new Mysql-Database Dump Step
1 parent 2ccb020 commit 9d3d5ed

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
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+
}

Documentation/steps.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,20 @@ Breadlesscode:
4545
tables:
4646
- neos_flow_security_account
4747
```
48+
49+
## MySql-Database export step
50+
51+
This step exports a whole MySql-Database via the `mysqldump`-Binary.
52+
53+
Example configuration:
54+
```yaml
55+
Breadlesscode:
56+
Backups:
57+
steps:
58+
'Breadlesscode\Backups\Step\MysqlDatabaseExportStep':
59+
mysqlDumpBinPath: 'mysqldump'
60+
mysqlBinPath: 'mysql'
61+
mysqlDumpOptions:
62+
- --single-transaction
63+
- --skip-extended-insert
64+
```

0 commit comments

Comments
 (0)