1
|
|
|
<?php |
2
|
|
|
namespace phpbu\App\Backup\Cleaner; |
3
|
|
|
|
4
|
|
|
use phpbu\App\Backup\Collector; |
5
|
|
|
use phpbu\App\Backup\Target; |
6
|
|
|
use phpbu\App\Result; |
7
|
|
|
use phpbu\App\Util\Str; |
8
|
|
|
use RuntimeException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Cleanup backup directory. |
12
|
|
|
* |
13
|
|
|
* Removes oldest backup till the given capacity isn't exceeded anymore. |
14
|
|
|
* |
15
|
|
|
* @package phpbu |
16
|
|
|
* @subpackage Backup |
17
|
|
|
* @author Sebastian Feldmann <[email protected]> |
18
|
|
|
* @copyright Sebastian Feldmann <[email protected]> |
19
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
20
|
|
|
* @link http://phpbu.de/ |
21
|
|
|
* @since Class available since Release 1.0.0 |
22
|
|
|
*/ |
23
|
|
|
class Capacity extends Abstraction implements Simulator |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Original XML value |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $capacityRaw; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Capacity in bytes. |
34
|
|
|
* |
35
|
|
|
* @var mixed <integer|double> |
36
|
|
|
*/ |
37
|
|
|
protected $capacityBytes; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Delete current backup as well |
41
|
|
|
* |
42
|
|
|
* @var boolean |
43
|
|
|
*/ |
44
|
|
|
protected $deleteTarget; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Setup the the Cleaner. |
48
|
|
|
* |
49
|
|
|
* @see \phpbu\App\Backup\Cleanup::setup() |
50
|
|
|
* @param array $options |
51
|
|
|
* @throws \phpbu\App\Backup\Cleaner\Exception |
52
|
|
|
*/ |
53
|
|
|
public function setup(array $options) |
54
|
7 |
|
{ |
55
|
|
|
if (!isset($options['size'])) { |
56
|
7 |
|
throw new Exception('option \'size\' is missing'); |
57
|
1 |
|
} |
58
|
|
|
try { |
59
|
|
|
$bytes = Str::toBytes($options['size']); |
60
|
6 |
|
} catch (RuntimeException $e) { |
61
|
6 |
|
throw new Exception($e->getMessage()); |
62
|
1 |
|
} |
63
|
|
|
$this->deleteTarget = isset($options['deleteTarget']) |
64
|
5 |
|
? Str::toBoolean($options['deleteTarget'], false) |
65
|
5 |
|
: false; |
66
|
5 |
|
$this->capacityRaw = $options['size']; |
67
|
5 |
|
$this->capacityBytes = $bytes; |
68
|
5 |
|
} |
69
|
5 |
|
|
70
|
|
|
/** |
71
|
|
|
* Return list of files to delete. |
72
|
|
|
* |
73
|
|
|
* @param \phpbu\App\Backup\Target $target |
74
|
|
|
* @param \phpbu\App\Backup\Collector $collector |
75
|
|
|
* @return \phpbu\App\Backup\File[] |
76
|
|
|
* @throws \phpbu\App\Exception |
77
|
|
|
*/ |
78
|
|
|
protected function getFilesToDelete(Target $target, Collector $collector) |
79
|
|
|
{ |
80
|
4 |
|
$files = $collector->getBackupFiles(); |
81
|
|
|
$size = $target->getSize(); |
82
|
4 |
|
$delete = []; |
83
|
4 |
|
|
84
|
|
|
/** @var \phpbu\App\Backup\File $file */ |
85
|
|
|
foreach ($files as $file) { |
86
|
4 |
|
$size += $file->getSize(); |
87
|
4 |
|
} |
88
|
4 |
|
|
89
|
|
|
// backups exceed capacity? |
90
|
|
|
if ($size > $this->capacityBytes) { |
91
|
4 |
|
// oldest backups first |
92
|
|
|
ksort($files); |
93
|
3 |
|
|
94
|
|
|
while ($size > $this->capacityBytes && count($files) > 0) { |
95
|
3 |
|
$file = array_shift($files); |
96
|
3 |
|
$size -= $file->getSize(); |
97
|
3 |
|
$delete[] = $file; |
98
|
3 |
|
} |
99
|
1 |
|
|
100
|
|
|
// deleted all old backups but still exceeding the space limit |
101
|
2 |
|
// delete the currently created backup as well |
102
|
2 |
|
if ($this->deleteTarget && $size > $this->capacityBytes) { |
103
|
2 |
|
$delete[] = $target->toFile(); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
2 |
|
return $delete; |
108
|
1 |
|
} |
109
|
|
|
} |
110
|
|
|
|