MysqlProcessor::backup()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 10
c 1
b 0
f 1
dl 0
loc 12
rs 9.9332
cc 3
nc 2
nop 0
1
<?php
2
3
4
namespace floor12\backup\logic\processors;
5
6
7
use floor12\backup\Exceptions\MysqlDumpException;
8
use floor12\backup\Exceptions\PostgresDumpException;
9
10
class MysqlProcessor extends DbProcessor
11
{
12
13
    public $port = 3306;
14
15
    public function backup(): void
16
    {
17
        $mysqldumpPath = $this->module->binaries['mysqldump'];
18
        $gzipPath = $this->module->binaries['gzip'];
19
        $ionicePath = $this->module->binaries['ionice'];
20
        $this->checkBinary($mysqldumpPath);
21
        $this->checkBinary($gzipPath);
22
        $this->checkBinary($ionicePath);
23
        $command = "{$ionicePath} -c{$this->io} {$mysqldumpPath} -h {$this->host} -P {$this->port} -u {$this->username}  -p{$this->password}  {$this->database} | {$gzipPath} -9 -c > {$this->backupFilePath}";
24
        (exec($command));
25
        if (!file_exists($this->backupFilePath) || filesize($this->backupFilePath) < 100)
26
            throw new MysqlDumpException();
27
    }
28
29
    public function restore(array $tableNames = []): void
30
    {
31
        $mysqlPath = $this->module->binaries['mysql'];
32
        $zcatPath = $this->module->binaries['zcat'];
33
        $this->checkBinary($zcatPath);
34
        $this->checkBinary($mysqlPath);
35
        $command = "{$zcatPath} {$this->backupFilePath} | {$mysqlPath} -h {$this->host} -P {$this->port} -u {$this->username} -p{$this->password} {$this->database}";
36
        exec($command, $return);
37
        if (!empty($return))
38
            throw new PostgresDumpException();
39
    }
40
}
41