Skip to content

Commit 727d7cc

Browse files
committed
returns next backup
1 parent 854e783 commit 727d7cc

File tree

9 files changed

+326
-110
lines changed

9 files changed

+326
-110
lines changed

lib/Controller/LocalController.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
use OCA\Backup\Exceptions\RestoringPointNotFoundException;
4242
use OCA\Backup\Model\BackupEvent;
4343
use OCA\Backup\Service\ConfigService;
44+
use OCA\Backup\Service\CronService;
4445
use OCA\Backup\Service\FilesService;
4546
use OCA\Backup\Service\PointService;
4647
use OCP\AppFramework\Http\DataResponse;
@@ -75,6 +76,9 @@ class LocalController extends OcsController {
7576
/** @var FilesService */
7677
private $filesService;
7778

79+
/** @var CronService */
80+
private $cronService;
81+
7882
/** @var ConfigService */
7983
private $configService;
8084

@@ -88,6 +92,7 @@ class LocalController extends OcsController {
8892
* @param EventRequest $eventRequest
8993
* @param PointService $pointService
9094
* @param FilesService $filesService
95+
* @param CronService $cronService
9196
* @param ConfigService $configService
9297
*/
9398
public function __construct(
@@ -97,6 +102,7 @@ public function __construct(
97102
EventRequest $eventRequest,
98103
PointService $pointService,
99104
FilesService $filesService,
105+
CronService $cronService,
100106
ConfigService $configService
101107
) {
102108
parent::__construct($appName, $request);
@@ -105,6 +111,7 @@ public function __construct(
105111
$this->eventRequest = $eventRequest;
106112
$this->pointService = $pointService;
107113
$this->filesService = $filesService;
114+
$this->cronService = $cronService;
108115
$this->configService = $configService;
109116
}
110117

@@ -153,6 +160,7 @@ public function getRestoringPoint(): DataResponse {
153160
*/
154161
public function getSettings(): DataResponse {
155162
$settings = $this->configService->getSettings();
163+
$settings = array_merge($settings, $this->cronService->nextBackups());
156164

157165
return new DataResponse($settings);
158166
}

lib/Cron/Backup.php

Lines changed: 14 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
use ArtificialOwl\MySmallPhpTools\Traits\Nextcloud\nc23\TNC23Logger;
3636
use OC\BackgroundJob\TimedJob;
3737
use OCA\Backup\Service\ConfigService;
38+
use OCA\Backup\Service\CronService;
3839
use OCA\Backup\Service\PointService;
3940
use Throwable;
4041

@@ -50,12 +51,13 @@ class Backup extends TimedJob {
5051
use TNC23Logger;
5152

5253

53-
const MARGIN = 1800;
54-
5554

5655
/** @var PointService */
5756
private $pointService;
5857

58+
/** @var CronService */
59+
private $cronService;
60+
5961
/** @var ConfigService */
6062
private $configService;
6163

@@ -64,13 +66,19 @@ class Backup extends TimedJob {
6466
* Backup constructor.
6567
*
6668
* @param PointService $pointService
69+
* @param CronService $cronService
6770
* @param ConfigService $configService
6871
*/
69-
public function __construct(PointService $pointService, ConfigService $configService) {
72+
public function __construct(
73+
PointService $pointService,
74+
CronService $cronService,
75+
ConfigService $configService
76+
) {
7077
$this->setInterval(900);
7178
// $this->setInterval(1);
7279

7380
$this->pointService = $pointService;
81+
$this->cronService = $cronService;
7482
$this->configService = $configService;
7583
}
7684

@@ -85,7 +93,7 @@ protected function run($argument) {
8593
$this->configService->setAppValueInt(ConfigService::MOCKUP_DATE, 0);
8694
}
8795

88-
if (!$this->verifyTime($time)) {
96+
if (!$this->cronService->verifyTime($time)) {
8997
return;
9098
}
9199

@@ -97,75 +105,14 @@ protected function run($argument) {
97105
*
98106
*/
99107
private function runBackup(int $time): void {
100-
if ($this->verifyFullBackup($time)) {
108+
if ($this->cronService->verifyFullBackup($time)) {
101109
$this->runFullBackup();
102-
} else if ($this->verifyIncrementalBackup($time)) {
110+
} else if ($this->cronService->verifyIncrementalBackup($time)) {
103111
$this->runIncrementalBackup();
104112
}
105113
}
106114

107115

108-
/**
109-
* @param int $time
110-
*
111-
* @return bool
112-
*/
113-
private function verifyTime(int $time): bool {
114-
[$st, $end] = explode('-', $this->configService->getAppValue(ConfigService::TIME_SLOTS));
115-
116-
if (!is_numeric($st) || !is_numeric($end)) {
117-
$this->log(3, 'Issue with Time Slots format, please check configuration');
118-
119-
return false;
120-
}
121-
122-
$st = (int)$st;
123-
$end = (int)$end;
124-
125-
$timeStart = mktime(
126-
$st,
127-
0,
128-
0,
129-
(int)date('n', $time),
130-
// we go back one day in time under some condition
131-
(int)date('j', $time) - ($st >= $end) * ((int)date('H', $time) < $end),
132-
(int)date('Y', $time)
133-
);
134-
135-
$timeEnd = mktime(
136-
$end,
137-
0,
138-
0,
139-
(int)date('n', $time),
140-
// we go one day forward on a night-day configuration (ie. 23-5)
141-
(int)date('j', $time) + ($st >= $end) * ((int)date('H', $time) > $end),
142-
(int)date('Y', $time)
143-
);
144-
145-
return ($timeStart < $time && $time < $timeEnd);
146-
}
147-
148-
149-
/**
150-
* @param int $time
151-
*
152-
* @return bool
153-
*/
154-
private function verifyFullBackup(int $time): bool {
155-
if (!$this->configService->getAppValueBool(ConfigService::ALLOW_WEEKDAY)
156-
&& !$this->isWeekEnd($time)) {
157-
return false;
158-
}
159-
160-
$last = $this->configService->getAppValueInt(ConfigService::DATE_FULL_RP);
161-
$delay = $this->configService->getAppValueInt(ConfigService::DELAY_FULL_RP);
162-
$delayUnit = $this->configService->getAppValue(ConfigService::DELAY_UNIT);
163-
$delay = $delay * 3600 * (($delayUnit !== 'h') ? 24 : 1);
164-
165-
return ($last + $delay - self::MARGIN < $time);
166-
}
167-
168-
169116
private function runFullBackup(): void {
170117
try {
171118
$this->pointService->create(true);
@@ -174,24 +121,6 @@ private function runFullBackup(): void {
174121
}
175122

176123

177-
/**
178-
* @param int $time
179-
*
180-
* @return bool
181-
*/
182-
private function verifyIncrementalBackup(int $time): bool {
183-
$last = max(
184-
$this->configService->getAppValueInt(ConfigService::DATE_PARTIAL_RP),
185-
$this->configService->getAppValueInt(ConfigService::DATE_FULL_RP)
186-
);
187-
$delay = $this->configService->getAppValueInt(ConfigService::DELAY_FULL_RP);
188-
$delayUnit = $this->configService->getAppValue(ConfigService::DELAY_UNIT);
189-
$delay = $delay * 3600 * (($delayUnit !== 'h') ? 24 : 1);
190-
191-
return ($last + $delay - self::MARGIN < $time);
192-
}
193-
194-
195124
/**
196125
*
197126
*/
@@ -203,13 +132,4 @@ private function runIncrementalBackup(): void {
203132
}
204133

205134

206-
/**
207-
* @param int $time
208-
*
209-
* @return bool
210-
*/
211-
private function isWeekEnd(int $time): bool {
212-
return ((int)date('N', $time) >= 6);
213-
}
214-
215135
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
6+
/**
7+
* Nextcloud - Backup now. Restore later.
8+
*
9+
* This file is licensed under the Affero General Public License version 3 or
10+
* later. See the COPYING file.
11+
*
12+
* @author Maxence Lange <maxence@artificial-owl.com>
13+
* @copyright 2021, Maxence Lange <maxence@artificial-owl.com>
14+
* @license GNU AGPL version 3 or any later version
15+
*
16+
* This program is free software: you can redistribute it and/or modify
17+
* it under the terms of the GNU Affero General Public License as
18+
* published by the Free Software Foundation, either version 3 of the
19+
* License, or (at your option) any later version.
20+
*
21+
* This program is distributed in the hope that it will be useful,
22+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
23+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24+
* GNU Affero General Public License for more details.
25+
*
26+
* You should have received a copy of the GNU Affero General Public License
27+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
28+
*
29+
*/
30+
31+
32+
namespace OCA\Backup\Exceptions;
33+
34+
35+
use Exception;
36+
37+
38+
/**
39+
* Class SettingsException
40+
*
41+
* @package OCA\Backup\Exceptions
42+
*/
43+
class SettingsException extends Exception {
44+
45+
}
46+

lib/Model/RestoringChunkPart.php

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ class RestoringChunkPart implements JsonSerializable, IDeserializable {
5353
/** @var string */
5454
private $name;
5555

56+
/** @var int */
57+
private $order;
58+
5659
/** @var string */
5760
private $content = '';
5861

@@ -70,9 +73,11 @@ class RestoringChunkPart implements JsonSerializable, IDeserializable {
7073
* RestoringChunkPart constructor.
7174
*
7275
* @param string $name
76+
* @param int $order
7377
*/
74-
public function __construct(string $name = '') {
78+
public function __construct(string $name = '', int $order = 0) {
7579
$this->name = $name;
80+
$this->order = $order;
7681
}
7782

7883

@@ -101,6 +106,25 @@ public function setName(string $name): self {
101106
}
102107

103108

109+
/**
110+
* @param int $order
111+
*
112+
* @return RestoringChunkPart
113+
*/
114+
public function setOrder(int $order): self {
115+
$this->order = $order;
116+
117+
return $this;
118+
}
119+
120+
/**
121+
* @return int
122+
*/
123+
public function getOrder(): int {
124+
return $this->order;
125+
}
126+
127+
104128
/**
105129
* @param bool $encrypted
106130
*
@@ -158,9 +182,6 @@ public function setEncryptedChecksum(string $encryptedChecksum): self {
158182
}
159183

160184

161-
162-
163-
164185
/**
165186
* @return string
166187
*/
@@ -199,6 +220,7 @@ public function getContent(): string {
199220
*/
200221
public function import(array $data): IDeserializable {
201222
$this->setName($this->get('name', $data))
223+
->setOrder($this->getInt('order', $data))
202224
->setContent($this->get('content', $data))
203225
->setEncrypted($this->getBool('encrypted', $data))
204226
->setChecksum($this->get('checksum', $data))
@@ -214,6 +236,7 @@ public function import(array $data): IDeserializable {
214236
public function jsonSerialize(): array {
215237
$arr = [
216238
'name' => $this->getName(),
239+
'order' => $this->getOrder(),
217240
'encrypted' => $this->isEncrypted(),
218241
'checksum' => $this->getChecksum(),
219242
'encryptedChecksum' => $this->getEncryptedChecksum()

0 commit comments

Comments
 (0)