Skip to content

Commit e273d0f

Browse files
Merge branch 'main' into 6.0
2 parents af95caf + b15ff6b commit e273d0f

File tree

12 files changed

+639
-59
lines changed

12 files changed

+639
-59
lines changed

phive.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phive xmlns="https://phar.io/phive">
3-
<phar name="captainhook" version="^5.10.0" location="./tools/captainhook" copy="true" installed="5.10.11"/>
4-
<phar name="phpunit" version="^9.4.3" location="./tools/phpunit" copy="true" installed="9.5.21"/>
5-
<phar name="phpab" version="^1.20.0" location="./tools/phpab" copy="true" installed="1.27.1"/>
6-
<phar name="phpcs" version="^3.5.2" location="./tools/phpcs" copy="true" installed="3.7.1"/>
7-
<phar name="phpstan" version="^0.12.2" location="./tools/phpstan" copy="true" installed="0.12.99"/>
3+
<phar name="captainhook" version="5.10.0" location="./tools/captainhook" copy="true" installed="5.10.0"/>
4+
<phar name="phpunit" version="^9.4.3" location="./tools/phpunit" copy="true" installed="9.6.8"/>
5+
<phar name="phpab" version="^1.20.0" location="./tools/phpab" copy="true" installed="1.27.2"/>
6+
<phar name="phpcs" version="^3.5.2" location="./tools/phpcs" copy="true" installed="3.7.2"/>
7+
<phar name="phpstan" version="^0.12.2" location="./tools/phpstan" copy="true" installed="0.12.100"/>
88
</phive>

src/Backup/Source/XtraBackup8.php

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
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+
* XtraBackup source class.
12+
*
13+
* @package phpbu
14+
* @subpackage Backup
15+
* @author Francis Chuang <[email protected]>
16+
* @author Sebastian Feldmann <[email protected]>
17+
* @copyright Sebastian Feldmann <[email protected]>
18+
* @license https://opensource.org/licenses/MIT The MIT License (MIT)
19+
* @link http://phpbu.de/
20+
* @since Class available since Release 6.0.10
21+
*/
22+
class XtraBackup8 extends SimulatorExecutable implements Simulator
23+
{
24+
/**
25+
* Path to xtrabackup command.
26+
*
27+
* @var string
28+
*/
29+
private $pathToXtraBackup;
30+
31+
/**
32+
* Path to MySQL data directory
33+
*
34+
* @var string
35+
*/
36+
private $dataDir;
37+
38+
/**
39+
* Host to connect to
40+
* --host <hostname>
41+
*
42+
* @var string
43+
*/
44+
private $host;
45+
46+
/**
47+
* User to connect with
48+
* --user <username>
49+
*
50+
* @var string
51+
*/
52+
private $user;
53+
54+
/**
55+
* Password to authenticate with
56+
* --password <password>
57+
*
58+
* @var string
59+
*/
60+
private $password;
61+
62+
/**
63+
* List of databases and/or tables to backup
64+
* Tables must e fully qualified: myDatabase.myTable
65+
* --databases array of strings
66+
*
67+
* @var array
68+
*/
69+
private $databases;
70+
71+
/**
72+
* Setup.
73+
*
74+
* @param array $conf
75+
* @throws Exception
76+
*@see \phpbu\App\Backup\Source
77+
*/
78+
public function setup(array $conf = [])
79+
{
80+
$this->setupSourceData($conf);
81+
82+
$this->pathToXtraBackup = Util\Arr::getValue($conf, 'pathToXtraBackup', '');
83+
$this->dataDir = Util\Arr::getValue($conf, 'dataDir', '');
84+
$this->host = Util\Arr::getValue($conf, 'host', '');
85+
$this->user = Util\Arr::getValue($conf, 'user', '');
86+
$this->password = Util\Arr::getValue($conf, 'password', '');
87+
}
88+
89+
/**
90+
* Get tables and databases to backup.
91+
*
92+
* @param array $conf
93+
*/
94+
protected function setupSourceData(array $conf)
95+
{
96+
$this->databases = Util\Str::toList(Util\Arr::getValue($conf, 'databases', ''));
97+
}
98+
99+
/**
100+
* Execute the backup.
101+
*
102+
* @param Target $target
103+
* @param \phpbu\App\Result $result
104+
* @return \phpbu\App\Backup\Source\Status
105+
* @throws Exception
106+
*@see \phpbu\App\Backup\Source
107+
*/
108+
public function backup(Target $target, Result $result) : Status
109+
{
110+
$innobackupex = $this->execute($target);
111+
112+
$result->debug($this->getExecutable($target)->getCommandPrintable());
113+
114+
if (!$innobackupex->isSuccessful()) {
115+
throw new Exception('XtraBackup failed: ' . $innobackupex->getStdErr());
116+
}
117+
118+
return $this->createStatus($target);
119+
}
120+
121+
/**
122+
* Create the Executable to run the innobackupex backup and apply-log commands.
123+
*
124+
* @param Target $target
125+
* @return Executable
126+
* @throws Exception
127+
*/
128+
protected function createExecutable(Target $target) : Executable
129+
{
130+
$executable = new Executable\Xtrabackup8($this->pathToXtraBackup);
131+
$executable->useHost($this->host)
132+
->credentials($this->user, $this->password)
133+
->dumpDatabases($this->databases)
134+
->dumpFrom($this->dataDir)
135+
->dumpTo($this->getDumpDir($target));
136+
return $executable;
137+
}
138+
139+
/**
140+
* Create backup status.
141+
*
142+
* @param Target $target
143+
* @return \phpbu\App\Backup\Source\Status
144+
*/
145+
protected function createStatus(Target $target) : Status
146+
{
147+
return Status::create()->uncompressedDirectory($this->getDumpDir($target));
148+
}
149+
150+
/**
151+
* Get the XtraBackup dump directory.
152+
*
153+
* @param Target $target
154+
* @return string
155+
*/
156+
public function getDumpDir(Target $target) : string
157+
{
158+
return $target->getPath()->getPath() . '/dump';
159+
}
160+
}

src/Cli/Executable/Mongodump.php

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public function __construct(string $path = '')
123123
* Set path to dump to.
124124
*
125125
* @param string $path
126-
* @return \phpbu\App\Cli\Executable\Mongodump
126+
* @return Mongodump
127127
*/
128128
public function dumpToDirectory(string $path) : Mongodump
129129
{
@@ -135,7 +135,7 @@ public function dumpToDirectory(string $path) : Mongodump
135135
* Use ipv6 to connect.
136136
*
137137
* @param boolean $bool
138-
* @return \phpbu\App\Cli\Executable\Mongodump
138+
* @return Mongodump
139139
*/
140140
public function useIpv6(bool $bool) : Mongodump
141141
{
@@ -147,7 +147,7 @@ public function useIpv6(bool $bool) : Mongodump
147147
* Set uri to dump from
148148
*
149149
* @param string $uri
150-
* @return \phpbu\App\Cli\Executable\Mongodump
150+
* @return Mongodump
151151
*/
152152
public function useUri(string $uri) : Mongodump
153153
{
@@ -159,7 +159,7 @@ public function useUri(string $uri) : Mongodump
159159
* Set host to dump from.
160160
*
161161
* @param string $host
162-
* @return \phpbu\App\Cli\Executable\Mongodump
162+
* @return Mongodump
163163
*/
164164
public function useHost(string $host) : Mongodump
165165
{
@@ -173,7 +173,7 @@ public function useHost(string $host) : Mongodump
173173
* @param string $user
174174
* @param string $password
175175
* @param string $authDatabase
176-
* @return \phpbu\App\Cli\Executable\Mongodump
176+
* @return Mongodump
177177
*/
178178
public function credentials(string $user = '', string $password = '', string $authDatabase = '') : Mongodump
179179
{
@@ -187,7 +187,7 @@ public function credentials(string $user = '', string $password = '', string $au
187187
* Dump only given databases.
188188
*
189189
* @param array $databases
190-
* @return \phpbu\App\Cli\Executable\Mongodump
190+
* @return Mongodump
191191
*/
192192
public function dumpDatabases(array $databases) : Mongodump
193193
{
@@ -199,7 +199,7 @@ public function dumpDatabases(array $databases) : Mongodump
199199
* Dump only given collections.
200200
*
201201
* @param array $collections
202-
* @return \phpbu\App\Cli\Executable\Mongodump
202+
* @return Mongodump
203203
*/
204204
public function dumpCollections(array $collections) : Mongodump
205205
{
@@ -211,7 +211,7 @@ public function dumpCollections(array $collections) : Mongodump
211211
* Exclude collections.
212212
*
213213
* @param array $collections
214-
* @return \phpbu\App\Cli\Executable\Mongodump
214+
* @return Mongodump
215215
*/
216216
public function excludeCollections(array $collections) : Mongodump
217217
{
@@ -223,7 +223,7 @@ public function excludeCollections(array $collections) : Mongodump
223223
* Exclude collections with given prefixes.
224224
*
225225
* @param array $prefixes
226-
* @return \phpbu\App\Cli\Executable\Mongodump
226+
* @return Mongodump
227227
*/
228228
public function excludeCollectionsWithPrefix(array $prefixes) : Mongodump
229229
{
@@ -234,8 +234,8 @@ public function excludeCollectionsWithPrefix(array $prefixes) : Mongodump
234234
/**
235235
* Mongodump CommandLine generator.
236236
*
237-
* @return \SebastianFeldmann\Cli\CommandLine
238-
* @throws \phpbu\App\Exception
237+
* @return CommandLine
238+
* @throws Exception
239239
*/
240240
protected function createCommandLine() : CommandLine
241241
{
@@ -246,28 +246,26 @@ protected function createCommandLine() : CommandLine
246246
$cmd = new Cmd($this->binary);
247247
$process->addCommand($cmd);
248248

249-
$cmd->addOption('--out', $this->dumpDir, ' ');
249+
$cmd->addOption('--out', $this->dumpDir);
250250
$cmd->addOptionIfNotEmpty('--ipv6', $this->useIPv6, false);
251-
$cmd->addOptionIfNotEmpty('--uri', $this->uri, true, ' ');
252-
$cmd->addOptionIfNotEmpty('--host', $this->host, true, ' ');
253-
$cmd->addOptionIfNotEmpty('--username', $this->user, true, ' ');
254-
$cmd->addOptionIfNotEmpty('--password', $this->password, true, ' ');
255-
$cmd->addOptionIfNotEmpty('--authenticationDatabase', $this->authenticationDatabase, true, ' ');
251+
$cmd->addOptionIfNotEmpty('--uri', $this->uri);
252+
$cmd->addOptionIfNotEmpty('--host', $this->host);
253+
$cmd->addOptionIfNotEmpty('--username', $this->user);
254+
$cmd->addOptionIfNotEmpty('--password', $this->password);
255+
$cmd->addOptionIfNotEmpty('--authenticationDatabase', $this->authenticationDatabase);
256256

257-
if (count($this->databases)) {
258-
foreach ($this->databases as $db) {
259-
$cmd->addOption('--db', $db, ' ');
260-
}
257+
foreach ($this->databases as $db) {
258+
$cmd->addOption('--db', $db);
261259
}
262-
263-
if (count($this->collections)) {
264-
foreach ($this->collections as $col) {
265-
$cmd->addOption('--collection', $col, ' ');
266-
}
260+
foreach ($this->collections as $col) {
261+
$cmd->addOption('--collection', $col);
262+
}
263+
foreach ($this->excludeCollections as $col) {
264+
$cmd->addOption('--excludeCollection', $col);
265+
}
266+
foreach ($this->excludeCollectionsWithPrefix as $col) {
267+
$cmd->addOption('--excludeCollectionWithPrefix', $col);
267268
}
268-
269-
$cmd->addOptionIfNotEmpty('--excludeCollection', $this->excludeCollections);
270-
$cmd->addOptionIfNotEmpty('--excludeCollectionWithPrefix', $this->excludeCollectionsWithPrefix);
271269

272270
return $process;
273271
}

0 commit comments

Comments
 (0)