Completed
Push — master ( d7a5bd...e8d259 )
by Sebastian
04:52
created

Innobackupex   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 187
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 12
c 2
b 0
f 1
lcom 1
cbo 4
dl 0
loc 187
ccs 45
cts 45
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A dumpFrom() 0 5 1
A dumpTo() 0 5 1
A useHost() 0 5 1
A credentials() 0 6 1
A including() 0 5 1
A dumpDatabases() 0 5 1
B createProcess() 0 37 5
1
<?php
2
namespace phpbu\App\Cli\Executable;
3
4
use phpbu\App\Cli\Cmd;
5
use phpbu\App\Cli\Executable;
6
use phpbu\App\Cli\Process;
7
use phpbu\App\Exception;
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
    /**
24
     * MySQL data directory
25
     *
26
     * @var string
27
     */
28
    private $dataDir;
29
30
    /**
31
     * Dump directory
32
     *
33
     * @var string
34
     */
35
    private $dumpDir;
36
37
    /**
38
     * Host to connect to
39
     * --host <hostname>
40
     *
41
     * @var string
42
     */
43
    private $host;
44
45
    /**
46
     * User to connect with
47
     * --user <username>
48
     *
49
     * @var string
50
     */
51
    private $user;
52
53
    /**
54
     * Password to authenticate with
55
     * --password <password>
56
     *
57
     * @var string
58
     */
59
    private $password;
60
61
    /**
62
     * Regular expression matching the tables to be backed up.
63
     * The regex should match the full qualified name: myDatabase.myTable
64
     * --tables string
65
     *
66
     * @var string
67
     */
68
    private $include;
69
70
    /**
71
     * List of databases and/or tables to backup
72
     * Tables must e fully qualified: myDatabase.myTable
73
     * --databases array of strings
74
     *
75
     * @var array
76
     */
77 10
    private $databases;
78
79 10
    /**
80 10
     * Constructor.
81 10
     *
82
     * @param string $path
83
     */
84
    public function __construct($path = null)
85
    {
86
        $this->cmd = 'innobackupex';
87
        parent::__construct($path);
88
    }
89 9
90
    /**
91 9
     * Set MySQL data dir.
92 9
     *
93
     * @param  string $path
94
     * @return \phpbu\App\Cli\Executable\Innobackupex
95
     */
96
    public function dumpFrom($path)
97
    {
98
        $this->dataDir = $path;
99
        return $this;
100
    }
101 3
102
    /**
103 3
     * Set target dump dir.
104 3
     *
105
     * @param  string $path
106
     * @return \phpbu\App\Cli\Executable\Innobackupex
107
     */
108
    public function dumpTo($path)
109
    {
110
        $this->dumpDir = $path;
111
        return $this;
112
    }
113
114 4
    /**
115
     * Set host du connect to.
116 4
     *
117 4
     * @param  string $host
118 4
     * @return \phpbu\App\Cli\Executable\Innobackupex
119
     */
120
    public function useHost($host)
121
    {
122
        $this->host = $host;
123
        return $this;
124
    }
125
126
    /**
127 3
     * Set mysql credentials.
128
     *
129 3
     * @param  string $user
130 3
     * @param  string $password
131
     * @return \phpbu\App\Cli\Executable\Innobackupex
132
     */
133
    public function credentials($user = null, $password = null)
134
    {
135
        $this->user     = $user;
136
        $this->password = $password;
137
        return $this;
138
    }
139 3
140
    /**
141 3
     * Set include option
142 3
     *
143
     * @param  string $include
144
     * @return \phpbu\App\Cli\Executable\Innobackupex
145
     */
146
    public function including($include)
147
    {
148
        $this->include = $include;
149
        return $this;
150
    }
151 10
152
    /**
153 10
     * Set databases to dump.
154 1
     *
155
     * @param  array $databases
156 9
     * @return \phpbu\App\Cli\Executable\Innobackupex
157 9
     */
158 9
    public function dumpDatabases(array $databases)
159 9
    {
160 9
        $this->databases = $databases;
161
        return $this;
162
    }
163 9
164 8
    /**
165 8
     * Subclass Process generator.
166
     *
167 8
     * @return \phpbu\App\Cli\Process
168
     * @throws \phpbu\App\Exception
169 9
     */
170 9
    public function createProcess()
171 9
    {
172 9
        if (empty($this->dumpDir)) {
173
            throw new Exception('no directory to dump to');
174 9
        }
175 1
        $process  = new Process();
176 9
        $cmdDump  = new Cmd($this->binary);
177 2
        $cmdApply = new Cmd($this->binary);
178 2
        $process->addCommand($cmdDump);
179
        $process->addCommand($cmdApply);
180 9
181
        // no std error unless it is activated
182 9
        if (!$this->showStdErr) {
183 9
            $cmdDump->silence();
184
            $cmdApply->silence();
185 9
            // i kill you
186
        }
187
188
        $cmdDump->addOption('--no-timestamp');
189
        $cmdDump->addOptionIfNotEmpty('--datadir', $this->dataDir);
190
        $cmdDump->addOptionIfNotEmpty('--user', $this->user);
191
        $cmdDump->addOptionIfNotEmpty('--password', $this->password);
192
        $cmdDump->addOptionIfNotEmpty('--host', $this->host);
193
194
        if (!empty($this->include)) {
195
            $cmdDump->addOption('--include', $this->include);
196
        } else if (count($this->databases)) {
197
            $cmdDump->addOption('--databases', implode(' ', $this->databases));
198
        }
199
200
        $cmdDump->addArgument($this->dumpDir);
201
202
        $cmdApply->addOption('--apply-log');
203
        $cmdApply->addArgument($this->dumpDir);
204
205
        return $process;
206
    }
207
}
208