Skip to content

Commit 2314b0d

Browse files
Add Percona xtrabackup version 8
1 parent 7636547 commit 2314b0d

File tree

6 files changed

+572
-0
lines changed

6 files changed

+572
-0
lines changed

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 <francis.chuang@gmail.com>
16+
* @author Sebastian Feldmann <sebastian@phpbu.de>
17+
* @copyright Sebastian Feldmann <sebastian@phpbu.de>
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/Xtrabackup8.php

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
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+
* Xtrabackup8 Executable class.
11+
*
12+
* @package phpbu
13+
* @subpackage Backup
14+
* @author Sebastian Feldmann <sebastian@phpbu.de>
15+
* @copyright Sebastian Feldmann <sebastian@phpbu.de>
16+
* @license https://opensource.org/licenses/MIT The MIT License (MIT)
17+
* @link http://phpbu.de/
18+
* @since Class available since Release 6.0.10
19+
*/
20+
class Xtrabackup8 extends Abstraction implements Executable
21+
{
22+
use OptionMasker;
23+
24+
/**
25+
* MySQL data directory
26+
*
27+
* @var string
28+
*/
29+
private $dataDir;
30+
31+
/**
32+
* Dump directory
33+
*
34+
* @var string
35+
*/
36+
private $dumpDir;
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+
* Regular expression matching the tables to be backed up.
64+
* The regex should match the full qualified name: myDatabase.myTable
65+
* --tables string
66+
*
67+
* @var string
68+
*/
69+
private $include;
70+
71+
/**
72+
* List of databases and/or tables to backup
73+
* Tables must e fully qualified: myDatabase.myTable
74+
* --databases array of strings
75+
*
76+
* @var array
77+
*/
78+
private $databases = [];
79+
80+
/**
81+
* Constructor.
82+
*
83+
* @param string $path
84+
*/
85+
public function __construct(string $path = '')
86+
{
87+
$this->setup('xtrabackup', $path);
88+
$this->setMaskCandidates(['password']);
89+
}
90+
91+
/**
92+
* Set MySQL data dir.
93+
*
94+
* @param string $path
95+
* @return Xtrabackup8
96+
*/
97+
public function dumpFrom(string $path) : Xtrabackup8
98+
{
99+
$this->dataDir = $path;
100+
return $this;
101+
}
102+
103+
/**
104+
* Set target dump dir.
105+
*
106+
* @param string $path
107+
* @return Xtrabackup8
108+
*/
109+
public function dumpTo(string $path) : Xtrabackup8
110+
{
111+
$this->dumpDir = $path;
112+
return $this;
113+
}
114+
115+
/**
116+
* Set host du connect to.
117+
*
118+
* @param string $host
119+
* @return Xtrabackup8
120+
*/
121+
public function useHost(string $host) : Xtrabackup8
122+
{
123+
$this->host = $host;
124+
return $this;
125+
}
126+
127+
/**
128+
* Set mysql credentials.
129+
*
130+
* @param string $user
131+
* @param string $password
132+
* @return Xtrabackup8
133+
*/
134+
public function credentials(string $user = '', string $password = '') : Xtrabackup8
135+
{
136+
$this->user = $user;
137+
$this->password = $password;
138+
return $this;
139+
}
140+
141+
/**
142+
* Set databases to dump.
143+
*
144+
* @param array $databases
145+
* @return Xtrabackup8
146+
*/
147+
public function dumpDatabases(array $databases)
148+
{
149+
$this->databases = $databases;
150+
return $this;
151+
}
152+
153+
/**
154+
* Xtrabackup8 CommandLine generator.
155+
*
156+
* @return \SebastianFeldmann\Cli\CommandLine
157+
* @throws Exception
158+
*/
159+
public function createCommandLine() : CommandLine
160+
{
161+
if (empty($this->dumpDir)) {
162+
throw new Exception('no directory to dump to');
163+
}
164+
$process = new CommandLine();
165+
$cmdDump = new Cmd($this->binary);
166+
$process->addCommand($cmdDump);
167+
168+
$cmdDump->addOption('--backup');
169+
$cmdDump->addOptionIfNotEmpty('--datadir', $this->dataDir);
170+
$cmdDump->addOptionIfNotEmpty('--user', $this->user);
171+
$cmdDump->addOptionIfNotEmpty('--password', $this->password);
172+
$cmdDump->addOptionIfNotEmpty('--host', $this->host);
173+
174+
if (count($this->databases)) {
175+
$cmdDump->addOption('--databases', implode(' ', $this->databases));
176+
}
177+
178+
$cmdDump->addArgument($this->dumpDir);
179+
180+
return $process;
181+
}
182+
}

src/Factory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class Factory
5656
'rsync' => '\\phpbu\\App\\Backup\\Source\\Rsync',
5757
'tar' => '\\phpbu\\App\\Backup\\Source\\Tar',
5858
'xtrabackup' => '\\phpbu\\App\\Backup\\Source\\XtraBackup',
59+
'xtrabackup8' => '\\phpbu\\App\\Backup\\Source\\XtraBackup8',
5960
],
6061
'check' => [
6162
'sizemin' => '\\phpbu\\App\\Backup\\Check\\SizeMin',

tests/_files/bin/xtrabackup

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#! /bin/bash
2+
3+
# unit test fake xtrabackup command that is executable
4+
5+
exit 0

0 commit comments

Comments
 (0)