1
|
|
|
<?php |
2
|
|
|
namespace phpbu\App\Backup\Cleaner; |
3
|
|
|
|
4
|
|
|
use phpbu\App\Backup\Cleaner; |
5
|
|
|
use phpbu\App\Backup\Cleaner\Stepwise\Range; |
6
|
|
|
use phpbu\App\Backup\Collector; |
7
|
|
|
use phpbu\App\Backup\File; |
8
|
|
|
use phpbu\App\Backup\Target; |
9
|
|
|
use phpbu\App\Util\Arr; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Cleanup backup directory. |
13
|
|
|
* Keep less and less backups over time |
14
|
|
|
* |
15
|
|
|
* | for x days | for x days | for x weeks | for x month keep | for x years |
16
|
|
|
* | keep all | keep one per day | keep one per week | keep one per month | keep one per year |
17
|
|
|
* --------+-------------+-------------------+-----------------------+-----------------------------+------------------ |
18
|
|
|
* backups | ............| . . . . . . . . . | . . . | . . | |
19
|
|
|
* |
20
|
|
|
* @package phpbu |
21
|
|
|
* @subpackage Backup |
22
|
|
|
* @author Sebastian Feldmann <[email protected]> |
23
|
|
|
* @copyright Sebastian Feldmann <[email protected]> |
24
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
25
|
|
|
* @link http://phpbu.de/ |
26
|
|
|
* @since Class available since Release 5.0.0 |
27
|
|
|
*/ |
28
|
|
|
class Stepwise extends Abstraction implements Cleaner |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* Amount of days to keep all backups. |
32
|
|
|
* |
33
|
|
|
* @var int |
34
|
|
|
*/ |
35
|
|
|
protected $daysToKeepAll; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Amount of days to keep at least one backup per day. |
39
|
|
|
* |
40
|
|
|
* @var int |
41
|
|
|
*/ |
42
|
|
|
protected $daysToKeepDaily; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Amount of weeks to keep at least one backup per week. |
46
|
|
|
* |
47
|
|
|
* @var int |
48
|
|
|
*/ |
49
|
|
|
protected $weeksToKeepWeekly; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Amount of month to keep at least one backup per month. |
53
|
|
|
* |
54
|
|
|
* @var int |
55
|
|
|
*/ |
56
|
|
|
protected $monthToKeepMonthly; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Amount of years to keep at least one backup per year. |
60
|
|
|
* |
61
|
|
|
* @var int |
62
|
|
|
*/ |
63
|
|
|
protected $yearsToKeepYearly; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* List of ranges defined by the configured settings. |
67
|
|
|
* |
68
|
|
|
* @var \phpbu\App\Backup\Cleaner\Stepwise\Range[] |
69
|
|
|
*/ |
70
|
|
|
protected $ranges; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Setup the Cleaner. |
74
|
|
|
* |
75
|
|
|
* @see \phpbu\App\Backup\Cleanup::setup() |
76
|
|
|
* @param array $options |
77
|
|
|
* @throws \phpbu\App\Backup\Cleaner\Exception |
78
|
|
|
*/ |
79
|
|
|
public function setup(array $options) |
80
|
|
|
{ |
81
|
|
|
$this->daysToKeepAll = Arr::getValue($options, 'daysToKeepAll', 0); |
82
|
|
|
$this->daysToKeepDaily = Arr::getValue($options, 'daysToKeepDaily', 0); |
83
|
|
|
$this->weeksToKeepWeekly = Arr::getValue($options, 'weeksToKeepWeekly', 0); |
84
|
|
|
$this->monthToKeepMonthly = Arr::getValue($options, 'monthToKeepMonthly', 0); |
85
|
|
|
$this->yearsToKeepYearly = Arr::getValue($options, 'yearsToKeepYearly', 0); |
86
|
|
|
|
87
|
|
|
$this->setupRanges(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Setup the date ranges. |
92
|
|
|
*/ |
93
|
|
|
protected function setupRanges() |
94
|
|
|
{ |
95
|
|
|
// keep all backups for x days as specified by 'keep all' |
96
|
|
|
$start = time(); |
97
|
|
|
$end = $start - (86400 * $this->daysToKeepAll); |
98
|
|
|
$all = new Range($start, $end, new Stepwise\Keeper\All()); |
99
|
|
|
|
100
|
|
|
// define the range that keeps one backup per day |
101
|
|
|
$end = mktime(0, 0, 0, date('m', $end), date('d', $end) - $this->daysToKeepDaily, date('Y', $end)); |
102
|
|
|
$daily = new Range($all->getEnd(), $end, new Stepwise\Keeper\OnePerGroup('Ymd')); |
103
|
|
|
|
104
|
|
|
// define the range that keeps one backup per week |
105
|
|
|
$end = mktime(0, 0, 0, date('m', $end), date('d', $end) - (7 * $this->weeksToKeepWeekly), date('Y', $end)); |
106
|
|
|
$weekly = new Range($daily->getEnd(), $end, new Stepwise\Keeper\OnePerGroup('YW')); |
107
|
|
|
|
108
|
|
|
// define the range that keeps one backup per month |
109
|
|
|
$end = mktime(0, 0, 0, date('m', $end) - $this->monthToKeepMonthly, date('d', $end), date('Y', $end)); |
110
|
|
|
$monthly = new Range($weekly->getEnd(), $end, new Stepwise\Keeper\OnePerGroup('Ym')); |
111
|
|
|
|
112
|
|
|
// define the range that keeps one backup per year |
113
|
|
|
$end = mktime(0, 0, 0, date('m', $end), date('d', $end), date('Y', $end) - $this->yearsToKeepYearly); |
114
|
|
|
$yearly = new Range($monthly->getEnd(), $end, new Stepwise\Keeper\OnePerGroup('Y')); |
115
|
|
|
|
116
|
|
|
// delete all backups older then configured year range |
117
|
|
|
$delete = new Range($end, 0, new Stepwise\Keeper\None()); |
118
|
|
|
|
119
|
|
|
$this->ranges = [$all, $daily, $weekly, $monthly, $yearly, $delete]; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Return list of files to delete. |
124
|
|
|
* |
125
|
|
|
* @param \phpbu\App\Backup\Target $target |
126
|
|
|
* @param \phpbu\App\Backup\Collector $collector |
127
|
|
|
* @return \phpbu\App\Backup\File[] |
128
|
|
|
* @throws \phpbu\App\Exception |
129
|
|
|
*/ |
130
|
|
|
protected function getFilesToDelete(Target $target, Collector $collector) |
131
|
|
|
{ |
132
|
|
|
$files = $collector->getBackupFiles(); |
133
|
|
|
$delete = []; |
134
|
|
|
|
135
|
|
|
// for each backup ... |
136
|
|
|
foreach ($files as $file) { |
137
|
|
|
// ... find the right date range ... |
138
|
|
|
$range = $this->getRangeForFile($file); |
139
|
|
|
// ... and check if this backup should be kept or deleted |
140
|
|
|
if (!$range->keep($file)) { |
141
|
|
|
$delete[] = $file; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
return $delete; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Get matching range for given file. |
149
|
|
|
* |
150
|
|
|
* @param \phpbu\App\Backup\File $file |
151
|
|
|
* @return \phpbu\App\Backup\Cleaner\Stepwise\Range |
152
|
|
|
* @throws \phpbu\App\Backup\Cleaner\Exception |
153
|
|
|
*/ |
154
|
|
|
protected function getRangeForFile(File $file) : Range |
155
|
|
|
{ |
156
|
|
|
foreach ($this->ranges as $range) { |
157
|
|
|
if ($file->getMTime() > $range->getEnd()) { |
158
|
|
|
break; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
return $range; |
|
|
|
|
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
It seems like you are relying on a variable being defined by an iteration: