Skip to content

Commit b8ff514

Browse files
committed
Added XtraBackup source.
1 parent 4a5af8b commit b8ff514

File tree

5 files changed

+500
-0
lines changed

5 files changed

+500
-0
lines changed

doc/config/source/xtrabackup.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<source type="xtrabackup">
3+
<!-- optional, default=false -->
4+
<option name="showStdErr" value="true" />
5+
6+
<!-- optional, default=localhost -->
7+
<option name="host" value="myhost" />
8+
9+
<!-- optional, default=system user -->
10+
<option name="user" value="myUserName" />
11+
12+
<!-- optional, default=none -->
13+
<option name="password" value="mySecretPassword" />
14+
15+
<!-- optional, default=all -->
16+
<option name="databases" value="myDB1,myDB2,myDB3.table1" />
17+
18+
<!-- optional, default=all -->
19+
<option name="include" value="^mydatabase[.]mytable" />
20+
</source>

src/Backup/Source/XtraBackup.php

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
<?php
2+
namespace phpbu\App\Backup\Source;
3+
4+
use phpbu\App\Backup\Cli\Binary;
5+
use phpbu\App\Backup\Cli\Cmd;
6+
use phpbu\App\Backup\Cli\Exec;
7+
use phpbu\App\Backup\Source;
8+
use phpbu\App\Backup\Target;
9+
use phpbu\App\Exception;
10+
use phpbu\App\Result;
11+
use phpbu\App\Util;
12+
13+
/**
14+
* XtraBackup (using the innobackupex script) source class.
15+
*
16+
* @package phpbu
17+
* @subpackage Backup
18+
* @author Francis Chuang <francis.chuang@gmail.com>
19+
* @author Sebastian Feldmann <sebastian@phpbu.de>
20+
* @copyright Sebastian Feldmann <sebastian@phpbu.de>
21+
* @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
22+
* @link http://phpbu.de/
23+
* @since Class available since Release 2.0.0
24+
*/
25+
class XtraBackup extends Binary implements Source
26+
{
27+
/**
28+
* Show stdErr
29+
*
30+
* @var boolean
31+
*/
32+
private $showStdErr;
33+
34+
/**
35+
* Host to connect to
36+
* --host <hostname>
37+
*
38+
* @var string
39+
*/
40+
private $host;
41+
42+
/**
43+
* User to connect with
44+
* --user <username>
45+
*
46+
* @var string
47+
*/
48+
private $user;
49+
50+
/**
51+
* Password to authenticate with
52+
* --password <password>
53+
*
54+
* @var string
55+
*/
56+
private $password;
57+
58+
/**
59+
* Regular expression matching the tables to be backed up.
60+
* The regex should match the full qualified name: mydatabase.mytable
61+
* --tables string
62+
*
63+
* @var string
64+
*/
65+
private $include;
66+
67+
/**
68+
* List of databases and/or tables to backup
69+
* Tables must e fully qualified: mydatabase.mytable
70+
* --databases array of strings
71+
*
72+
* @var array
73+
*/
74+
private $databases;
75+
76+
/**
77+
* Setup.
78+
*
79+
* @see \phpbu\App\Backup\Source
80+
* @param array $conf
81+
* @throws \phpbu\App\Exception
82+
*/
83+
public function setup(array $conf = array())
84+
{
85+
$this->setupXtraBackup($conf);
86+
$this->setupSourceData($conf);
87+
88+
$this->host = Util\Arr::getValue($conf, 'host');
89+
$this->user = Util\Arr::getValue($conf, 'user');
90+
$this->password = Util\Arr::getValue($conf, 'password');
91+
$this->showStdErr = Util\Str::toBoolean(Util\Arr::getValue($conf, 'showStdErr', ''), false);
92+
}
93+
94+
/**
95+
* Search for innobackupex command.
96+
*
97+
* @param array $conf
98+
*/
99+
protected function setupXtraBackup(array $conf)
100+
{
101+
if (empty($this->binary)) {
102+
$this->binary = $this->detectCommand('innobackupex', Util\Arr::getValue($conf, 'pathToXtraBackup'));
103+
}
104+
}
105+
106+
/**
107+
* Get tables and databases to backup.
108+
*
109+
* @param array $conf
110+
*/
111+
protected function setupSourceData(array $conf)
112+
{
113+
$this->include = Util\Arr::getValue($conf, 'include');
114+
$this->databases = Util\Str::toList(Util\Arr::getValue($conf, 'databases'));
115+
}
116+
117+
/**
118+
* (non-PHPDoc)
119+
*
120+
* @see \phpbu\App\Backup\Source
121+
* @param \phpbu\App\Backup\Target $target
122+
* @param \phpbu\App\Result $result
123+
* @return \phpbu\App\Backup\Source\Status
124+
* @throws \phpbu\App\Exception
125+
*/
126+
public function backup(Target $target, Result $result)
127+
{
128+
$exec = $this->getExec($target);
129+
$innobackupex = $this->execute($exec);
130+
131+
$result->debug($innobackupex->getCmd());
132+
133+
if (!$innobackupex->wasSuccessful()) {
134+
throw new Exception('XtraBackup failed');
135+
}
136+
137+
return Status::create()->uncompressed()->dataPath($this->getDumpDir($target));
138+
}
139+
140+
/**
141+
* Create the Exec to run the innobackupex backup and apply-log commands.
142+
*
143+
* @param \phpbu\App\Backup\Target $target
144+
* @return \phpbu\App\Backup\Cli\Exec
145+
* @throws Exception
146+
*/
147+
public function getExec(Target $target)
148+
{
149+
if (null == $this->exec) {
150+
$dump = $this->getDumpDir($target);
151+
$this->exec = new Exec();
152+
$cmd = new Cmd($this->binary);
153+
$cmd2 = new Cmd($this->binary);
154+
$this->exec->addCommand($cmd);
155+
$this->exec->addCommand($cmd2);
156+
157+
// no std error unless it is activated
158+
if (!$this->showStdErr) {
159+
$cmd->silence();
160+
$cmd2->silence();
161+
// i kill you
162+
}
163+
164+
$cmd->addOption('--no-timestamp');
165+
$this->addOptionIfNotEmpty($cmd, '--user', $this->user);
166+
$this->addOptionIfNotEmpty($cmd, '--password', $this->password);
167+
$this->addOptionIfNotEmpty($cmd, '--host', $this->host);
168+
169+
if (!empty($this->include)) {
170+
$cmd->addOption('--include', $this->include);
171+
} else if (count($this->databases)) {
172+
$cmd->addOption('--databases', implode(' ', $this->databases));
173+
}
174+
175+
$cmd->addArgument($dump);
176+
177+
$cmd2->addOption('--apply-log');
178+
$cmd2->addArgument($dump);
179+
}
180+
181+
return $this->exec;
182+
}
183+
184+
/**
185+
* Get the XtraBackup dump directory.
186+
*
187+
* @param \phpbu\App\Backup\Target $target
188+
* @return string
189+
*/
190+
public function getDumpDir(Target $target)
191+
{
192+
return $target->getPath() . '/dump';
193+
}
194+
}

src/Factory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ abstract class Factory
3838
'tar' => '\\phpbu\\App\\Backup\\Source\\Tar',
3939
'elasticdump' => '\\phpbu\\App\\Backup\\Source\\Elasticdump',
4040
'arangodump' => '\\phpbu\\App\\Backup\\Source\\Arangodump',
41+
'xtrabackup' => '\\phpbu\\App\\Backup\\Source\\XtraBackup',
4142
),
4243
'check' => array(
4344
'sizemin' => '\\phpbu\\App\\Backup\\Check\\SizeMin',

tests/_files/bin/innobackupex

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 innobackupex command that is executable
4+
5+
exit 0

0 commit comments

Comments
 (0)