Completed
Push — master ( 6a1fad...afb84c )
by Sebastian
05:39
created

Innobackupex::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
ccs 1
cts 1
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
namespace phpbu\App\Cli\Executable;
3
4
use phpbu\App\Cli\Executable;
5
use phpbu\App\Exception;
6
use SebastianFeldmann\Cli\CommandLine;
7
use SebastianFeldmann\Cli\Command\Executable as Cmd;
8
9
/**
10
 * Innobackupex Executable class.
11
 *
12
 * @package    phpbu
13
 * @subpackage Backup
14
 * @author     Francis Chuang <[email protected]>
15
 * @author     Sebastian Feldmann <[email protected]>
16
 * @copyright  Sebastian Feldmann <[email protected]>
17
 * @license    https://opensource.org/licenses/MIT The MIT License (MIT)
18
 * @link       http://phpbu.de/
19
 * @since      Class available since Release 2.1.0
20
 */
21
class Innobackupex extends Abstraction implements Executable
22
{
23
    use OptionMasker;
24
25
    /**
26
     * MySQL data directory
27
     *
28
     * @var string
29
     */
30
    private $dataDir;
31
32
    /**
33
     * Dump directory
34
     *
35
     * @var string
36
     */
37
    private $dumpDir;
38
39
    /**
40
     * Host to connect to
41
     * --host <hostname>
42
     *
43
     * @var string
44
     */
45
    private $host;
46
47
    /**
48
     * User to connect with
49
     * --user <username>
50
     *
51
     * @var string
52
     */
53
    private $user;
54
55
    /**
56
     * Password to authenticate with
57
     * --password <password>
58
     *
59
     * @var string
60
     */
61
    private $password;
62
63
    /**
64
     * Regular expression matching the tables to be backed up.
65
     * The regex should match the full qualified name: myDatabase.myTable
66
     * --tables string
67
     *
68
     * @var string
69
     */
70
    private $include;
71
72
    /**
73
     * List of databases and/or tables to backup
74
     * Tables must e fully qualified: myDatabase.myTable
75
     * --databases array of strings
76
     *
77 10
     * @var array
78
     */
79 10
    private $databases;
80 10
81 10
    /**
82
     * Constructor.
83
     *
84
     * @param string $path
85
     */
86
    public function __construct(string $path = '')
87
    {
88
        $this->setup('innobackupex', $path);
89 9
        $this->setMaskCandidates(['password']);
90
    }
91 9
92 9
    /**
93
     * Set MySQL data dir.
94
     *
95
     * @param  string $path
96
     * @return \phpbu\App\Cli\Executable\Innobackupex
97
     */
98
    public function dumpFrom(string $path) : Innobackupex
99
    {
100
        $this->dataDir = $path;
101 3
        return $this;
102
    }
103 3
104 3
    /**
105
     * Set target dump dir.
106
     *
107
     * @param  string $path
108
     * @return \phpbu\App\Cli\Executable\Innobackupex
109
     */
110
    public function dumpTo(string $path) : Innobackupex
111
    {
112
        $this->dumpDir = $path;
113
        return $this;
114 4
    }
115
116 4
    /**
117 4
     * Set host du connect to.
118 4
     *
119
     * @param  string $host
120
     * @return \phpbu\App\Cli\Executable\Innobackupex
121
     */
122
    public function useHost(string $host) : Innobackupex
123
    {
124
        $this->host = $host;
125
        return $this;
126
    }
127 3
128
    /**
129 3
     * Set mysql credentials.
130 3
     *
131
     * @param  string $user
132
     * @param  string $password
133
     * @return \phpbu\App\Cli\Executable\Innobackupex
134
     */
135
    public function credentials(string $user = '', string $password = '') : Innobackupex
136
    {
137
        $this->user     = $user;
138
        $this->password = $password;
139 3
        return $this;
140
    }
141 3
142 3
    /**
143
     * Set include option
144
     *
145
     * @param  string $include
146
     * @return \phpbu\App\Cli\Executable\Innobackupex
147
     */
148
    public function including(string $include) : Innobackupex
149
    {
150
        $this->include = $include;
151 10
        return $this;
152
    }
153 10
154 1
    /**
155
     * Set databases to dump.
156 9
     *
157 9
     * @param  array $databases
158 9
     * @return \phpbu\App\Cli\Executable\Innobackupex
159 9
     */
160 9
    public function dumpDatabases(array $databases)
161
    {
162
        $this->databases = $databases;
163 9
        return $this;
164 8
    }
165 8
166
    /**
167 8
     * Innobackupex CommandLine generator.
168
     *
169 9
     * @return \SebastianFeldmann\Cli\CommandLine
170 9
     * @throws \phpbu\App\Exception
171 9
     */
172 9
    public function createCommandLine() : CommandLine
173
    {
174 9
        if (empty($this->dumpDir)) {
175 1
            throw new Exception('no directory to dump to');
176 9
        }
177 2
        $process  = new CommandLine();
178 2
        $cmdDump  = new Cmd($this->binary);
179
        $cmdApply = new Cmd($this->binary);
180 9
        $process->addCommand($cmdDump);
181
        $process->addCommand($cmdApply);
182 9
183 9
        $cmdDump->addOption('--no-timestamp');
184
        $cmdDump->addOptionIfNotEmpty('--datadir', $this->dataDir);
185 9
        $cmdDump->addOptionIfNotEmpty('--user', $this->user);
186
        $cmdDump->addOptionIfNotEmpty('--password', $this->password);
187
        $cmdDump->addOptionIfNotEmpty('--host', $this->host);
188
189
        if (!empty($this->include)) {
190
            $cmdDump->addOption('--include', $this->include);
191
        } else if (count($this->databases)) {
192
            $cmdDump->addOption('--databases', implode(' ', $this->databases));
193
        }
194
195
        $cmdDump->addArgument($this->dumpDir);
196
197
        $cmdApply->addOption('--apply-log');
198
        $cmdApply->addArgument($this->dumpDir);
199
200
        return $process;
201
    }
202
}
203