Completed
Push — master ( 4f1d49...8186c0 )
by Sebastian
07:15
created

Mysqldump::simulate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 0
cts 0
cp 0
rs 9.4285
cc 1
eloc 2
nc 1
nop 2
crap 2
1
<?php
2
namespace phpbu\App\Backup\Source;
3
4
use phpbu\App\Backup\Target;
5
use phpbu\App\Cli\Executable;
6
use phpbu\App\Exception;
7
use phpbu\App\Result;
8
use phpbu\App\Util;
9
10
/**
11
 * Mysqldump source class.
12
 *
13
 * @package    phpbu
14
 * @subpackage Backup
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 1.0.0
20
 */
21
class Mysqldump extends SimulatorExecutable implements Simulator
22
{
23
    /**
24
     * Path to executable.
25
     *
26
     * @var string
27
     */
28
    private $pathToMysqldump;
29
30
    /**
31
     * Host to connect to
32
     * --host <hostname>
33
     *
34
     * @var string
35
     */
36
    private $host;
37
38
    /**
39
     * User to connect with
40
     * --user <username>
41
     *
42
     * @var string
43
     */
44
    private $user;
45
46
    /**
47
     * Password to authenticate with
48
     * --password <password>
49
     *
50
     * @var string
51
     */
52
    private $password;
53
54
    /**
55
     * List of tables to backup
56
     * --tables array of strings
57
     *
58
     * @var array
59
     */
60
    private $tables;
61
62
    /**
63
     * List of databases to backup
64
     * --databases array of strings
65
     *
66
     * @var array
67
     */
68
    private $databases;
69
70
    /**
71
     * List of tables to ignore
72
     *
73
     * @var array
74
     */
75
    private $ignoreTables;
76
77
    /**
78
     * List of tables where only the table structure is stored
79
     *
80
     * @var array
81
     */
82
    private $structureOnly;
83
84
    /**
85
     * Use mysqldump quick mode
86
     * -q
87
     *
88
     * @var boolean
89
     */
90
    private $quick;
91
92
    /**
93
     *
94
     * Lock tables option
95
     * --lock-tables
96
     *
97
     * @var bool
98
     */
99
    private $lockTables;
100
101
    /**
102
     * Use mysqldump with compression
103
     * -C
104
     *
105
     * @var boolean
106
     */
107
    private $compress;
108
109
    /**
110
     * Use mysqldump with extended insert
111
     * -e
112
     *
113
     * @var boolean
114
     */
115
    private $extendedInsert;
116
117
    /**
118
     * Dump blob fields as hex.
119
     * --hex-blob
120
     *
121
     * @var boolean
122
     */
123
    private $hexBlob;
124
125
    /**
126
     * Dump only table structures
127
     * --no-data
128
     *
129
     * @var boolean
130
     */
131
    private $noData;
132
133
    /**
134
     * Dump target location
135
     * @var string
136
     */
137
    private $dumpPathname;
138
139
    /**
140
     * Setup.
141
     *
142
     * @see    \phpbu\App\Backup\Source
143
     * @param  array $conf
144
     * @throws \phpbu\App\Exception
145
     */
146 5
    public function setup(array $conf = [])
147
    {
148 5
        $this->setupSourceData($conf);
149
150 5
        $this->pathToMysqldump = Util\Arr::getValue($conf, 'pathToMysqldump');
151 5
        $this->host            = Util\Arr::getValue($conf, 'host');
152 5
        $this->user            = Util\Arr::getValue($conf, 'user');
153 5
        $this->password        = Util\Arr::getValue($conf, 'password');
154 5
        $this->hexBlob         = Util\Str::toBoolean(Util\Arr::getValue($conf, 'hexBlob', ''), false);
155 5
        $this->quick           = Util\Str::toBoolean(Util\Arr::getValue($conf, 'quick', ''), false);
156 5
        $this->lockTables      = Util\Str::toBoolean(Util\Arr::getValue($conf, 'lockTables', ''), false);
157 5
        $this->compress        = Util\Str::toBoolean(Util\Arr::getValue($conf, 'compress', ''), false);
158 5
        $this->extendedInsert  = Util\Str::toBoolean(Util\Arr::getValue($conf, 'extendedInsert', ''), false);
159 5
        $this->noData          = Util\Str::toBoolean(Util\Arr::getValue($conf, 'noData', ''), false);
160 5
    }
161
162
    /**
163
     * Get tables and databases to backup.
164
     *
165
     * @param array $conf
166
     */
167 5 View Code Duplication
    protected function setupSourceData(array $conf)
168
    {
169 5
        $this->tables        = Util\Str::toList(Util\Arr::getValue($conf, 'tables'));
170 5
        $this->databases     = Util\Str::toList(Util\Arr::getValue($conf, 'databases'));
171 5
        $this->ignoreTables  = Util\Str::toList(Util\Arr::getValue($conf, 'ignoreTables'));
172 5
        $this->structureOnly = Util\Str::toList(Util\Arr::getValue($conf, 'structureOnly'));
173 5
    }
174
175
    /**
176
     * (non-PHPDoc)
177
     *
178
     * @see    \phpbu\App\Backup\Source
179
     * @param  \phpbu\App\Backup\Target $target
180
     * @param  \phpbu\App\Result        $result
181
     * @return \phpbu\App\Backup\Source\Status
182
     * @throws \phpbu\App\Exception
183
     */
184 2 View Code Duplication
    public function backup(Target $target, Result $result)
185
    {
186
        // setup dump location and execute the dump
187 2
        $this->dumpPathname = $target->getPathnamePlain();
188 2
        $mysqldump          = $this->execute($target);
189
190 2
        $result->debug($mysqldump->getCmd());
191
192 2
        if (!$mysqldump->wasSuccessful()) {
193 1
            throw new Exception('mysqldump failed:' . $mysqldump->getStdErr());
194
        }
195
196 1
        return $this->createStatus($target);
197
    }
198
199
    /**
200
     * Create the Executable to run the mysqldump command.
201
     *
202
     * @param  \phpbu\App\Backup\Target $target
203
     * @return \phpbu\App\Cli\Executable
204
     */
205 5
    public function getExecutable(Target $target)
206
    {
207 5
        if (null == $this->executable) {
208 3
            $this->executable = new Executable\Mysqldump($this->pathToMysqldump);
209 3
            $this->executable->credentials($this->user, $this->password)
210 3
                             ->useHost($this->host)
211 3
                             ->useQuickMode($this->quick)
212 3
                             ->lockTables($this->lockTables)
213 3
                             ->dumpBlobsHexadecimal($this->hexBlob)
214 3
                             ->useCompression($this->compress)
215 3
                             ->useExtendedInsert($this->extendedInsert)
216 3
                             ->dumpTables($this->tables)
217 3
                             ->dumpDatabases($this->databases)
218 3
                             ->ignoreTables($this->ignoreTables)
219 3
                             ->dumpNoData($this->noData)
220 3
                             ->dumpStructureOnly($this->structureOnly)
221 3
                             ->dumpTo($this->dumpPathname);
222 3
        }
223 5
        return $this->executable;
224
    }
225
226
    /**
227
     * Create backup status.
228
     *
229
     * @param  \phpbu\App\Backup\Target
230
     * @return \phpbu\App\Backup\Source\Status
231
     */
232
    protected function createStatus(Target $target)
233
    {
234
        return Status::create()->uncompressed($this->dumpPathname);
235
    }
236
}
237