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