Skip to content

Commit cd7279c

Browse files
Issue #9 - introducing the 'Source::backup' return value 'Source\Status'
1 parent 4acd96c commit cd7279c

File tree

7 files changed

+154
-12
lines changed

7 files changed

+154
-12
lines changed

src/Backup/Source.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ interface Source
2424
public function setup(array $conf = array());
2525

2626
/**
27-
* Runner the backup
27+
* Execute the backup.
2828
*
2929
* @param \phpbu\App\Backup\Target $target
3030
* @param \phpbu\App\Result $result
31+
* @return \phpbu\App\Backup\Source\Status
3132
*/
3233
public function backup(Target $target, Result $result);
3334
}

src/Backup/Source/Elasticdump.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ protected function setupSourceData(array $conf)
114114
* @see \phpbu\App\Backup\Source
115115
* @param \phpbu\App\Backup\Target $target
116116
* @param \phpbu\App\Result $result
117-
* @return \phpbu\App\Result
117+
* @return \phpbu\App\Backup\Source\Status
118118
* @throws \phpbu\App\Exception
119119
*/
120120
public function backup(Target $target, Result $result)
@@ -128,7 +128,7 @@ public function backup(Target $target, Result $result)
128128
throw new Exception('elasticdump failed');
129129
}
130130

131-
return $result;
131+
return Status::create();
132132
}
133133

134134
/**
@@ -161,8 +161,17 @@ public function getExec(Target $target)
161161
return $this->exec;
162162
}
163163

164-
private function generateNodeUrl($host, $user = null, $password = null, $index = null){
165-
164+
/**
165+
* Create a elastic node url.
166+
*
167+
* @param string $host
168+
* @param string $user
169+
* @param string $password
170+
* @param string $index
171+
* @return string
172+
*/
173+
private function generateNodeUrl($host, $user = null, $password = null, $index = null)
174+
{
166175
$parsed = parse_url($host);
167176

168177
if (!isset($parsed['scheme'])) {

src/Backup/Source/Mongodump.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ protected function setupSourceData(array $conf)
171171
* @see \phpbu\App\Backup\Source
172172
* @param \phpbu\App\Backup\Target $target
173173
* @param \phpbu\App\Result $result
174-
* @return \phpbu\App\Result
174+
* @return \phpbu\App\Backup\Source\Status
175175
* @throws \phpbu\App\Exception
176176
*/
177177
public function backup(Target $target, Result $result)
@@ -192,7 +192,8 @@ public function backup(Target $target, Result $result)
192192
} catch (\Exception $e) {
193193
throw new Exception('Failed to \'tar\' Mongodump directory', 1, $e);
194194
}
195-
return $result;
195+
// return Status::create()->uncompressed()->dataPath($this->getDumpDir($target));
196+
return Status::create();
196197
}
197198

198199
/**

src/Backup/Source/Mysqldump.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ protected function setupSourceData(array $conf)
160160
* @see \phpbu\App\Backup\Source
161161
* @param \phpbu\App\Backup\Target $target
162162
* @param \phpbu\App\Result $result
163-
* @return \phpbu\App\Result
163+
* @return \phpbu\App\Backup\Source\Status
164164
* @throws \phpbu\App\Exception
165165
*/
166166
public function backup(Target $target, Result $result)
@@ -174,7 +174,7 @@ public function backup(Target $target, Result $result)
174174
throw new Exception('mysqldump failed');
175175
}
176176

177-
return $result;
177+
return Status::create();
178178
}
179179

180180
/**

src/Backup/Source/Status.php

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
namespace phpbu\App\Backup\Source;
3+
4+
use phpbu\App\Exception;
5+
6+
/**
7+
* Status class.
8+
*
9+
* @package phpbu
10+
* @subpackage Backup
11+
* @author Sebastian Feldmann <sebastian@phpbu.de>
12+
* @copyright Sebastian Feldmann <sebastian@phpbu.de>
13+
* @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
14+
* @link http://phpbu.de/
15+
* @since Class available since Release 2.0.1
16+
*/
17+
class Status
18+
{
19+
/**
20+
* Source handles compression by itself.
21+
*
22+
* @var boolean
23+
*/
24+
private $handledCompression;
25+
26+
/**
27+
* Path to generated source data.
28+
*
29+
* @var string
30+
*/
31+
private $dataPath;
32+
33+
/**
34+
* Constructor
35+
*/
36+
public function __construct()
37+
{
38+
$this->handledCompression = true;
39+
}
40+
41+
/**
42+
* Source doesn't handle compression.
43+
*
44+
* @return \phpbu\App\Backup\Source\Status
45+
*/
46+
public function uncompressed()
47+
{
48+
$this->handledCompression = false;
49+
return $this;
50+
}
51+
52+
/**
53+
* Did the Source handle the compression.
54+
*
55+
* @return boolean
56+
*/
57+
public function handledCompression()
58+
{
59+
return $this->handledCompression;
60+
}
61+
62+
/**
63+
* Add a data location.
64+
*
65+
* @param string $path
66+
* @return \phpbu\App\Backup\Source\Status
67+
*/
68+
public function dataPath($path)
69+
{
70+
$this->dataPath = $path;
71+
return $this;
72+
}
73+
74+
/**
75+
* Return data location.
76+
*
77+
* @return string
78+
* @throws \phpbu\App\Exception
79+
*/
80+
public function getDataPath()
81+
{
82+
if ($this->handledCompression) {
83+
throw new Exception('source already handled compression');
84+
}
85+
return $this->dataPath;
86+
}
87+
88+
/**
89+
* Static constructor for fluent interface calls.
90+
*
91+
* @return \phpbu\App\Backup\Source\Status
92+
*/
93+
public static function create()
94+
{
95+
return new self();
96+
}
97+
}

src/Backup/Source/Tar.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ protected function setupTar(array $conf)
8181
* @see \phpbu\App\Backup\Source
8282
* @param \phpbu\App\Backup\Target $target
8383
* @param \phpbu\App\Result $result
84-
* @return \phpbu\App\Result
84+
* @return \phpbu\App\Backup\Source\Status
8585
* @throws \phpbu\App\Exception
8686
*/
8787
public function backup(Target $target, Result $result)
@@ -104,7 +104,7 @@ public function backup(Target $target, Result $result)
104104
throw new Exception('tar failed');
105105
}
106106

107-
return $result;
107+
return Status::create();
108108
}
109109

110110
/**

src/Runner.php

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use phpbu\App\Backup;
55
use phpbu\App\Backup\Compressor;
66
use phpbu\App\Backup\Collector;
7+
use phpbu\App\Backup\Source\Status;
78
use phpbu\App\Backup\Target;
89

910
/**
@@ -237,10 +238,43 @@ protected function executeBackup(Configuration\Backup $conf, Target $target)
237238
{
238239
$this->result->backupStart($conf);
239240
$source = Factory::createSource($conf->getSource()->type, $conf->getSource()->options);
240-
$source->backup($target, $this->result);
241+
$status = $source->backup($target, $this->result);
242+
if (is_a($status, '\\phpbu\\App\\Backup\\Source\\Status') && !$status->handledCompression()) {
243+
$this->handleCompression($target, $status);
244+
}
241245
$this->result->backupEnd($conf);
242246
}
243247

248+
/**
249+
* Handle directory compression for sources which can't handle compression by them self.
250+
*
251+
* @param \phpbu\App\Backup\Target $target
252+
* @param \phpbu\App\Backup\Source\Status $status
253+
* @throws \phpbu\App\Exception
254+
*/
255+
protected function handleCompression(Target $target, Status $status)
256+
{
257+
$pathToCompress = $status->getDataPath();
258+
if (empty($pathToCompress)) {
259+
throw new Exception('no path to compress set');
260+
}
261+
if (!is_dir($pathToCompress)) {
262+
throw new Exception('path to compress should be a directory');
263+
}
264+
try {
265+
$tar = new Backup\Source\Tar();
266+
$tar->setup(
267+
array(
268+
'path' => $pathToCompress,
269+
'removeDir' => 'true',
270+
)
271+
);
272+
$tar->backup($target, $this->result);
273+
} catch (\Exception $e) {
274+
throw new Exception('Failed to \'tar\' directory: ' . $pathToCompress . PHP_EOL . $e->getMessage(), 1, $e);
275+
}
276+
}
277+
244278
/**
245279
* Execute checks.
246280
*

0 commit comments

Comments
 (0)