Skip to content

Commit

Permalink
returns next backup
Browse files Browse the repository at this point in the history
  • Loading branch information
ArtificialOwl committed Oct 14, 2021
1 parent 854e783 commit 727d7cc
Show file tree
Hide file tree
Showing 9 changed files with 326 additions and 110 deletions.
8 changes: 8 additions & 0 deletions lib/Controller/LocalController.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
use OCA\Backup\Exceptions\RestoringPointNotFoundException;
use OCA\Backup\Model\BackupEvent;
use OCA\Backup\Service\ConfigService;
use OCA\Backup\Service\CronService;
use OCA\Backup\Service\FilesService;
use OCA\Backup\Service\PointService;
use OCP\AppFramework\Http\DataResponse;
Expand Down Expand Up @@ -75,6 +76,9 @@ class LocalController extends OcsController {
/** @var FilesService */
private $filesService;

/** @var CronService */
private $cronService;

/** @var ConfigService */
private $configService;

Expand All @@ -88,6 +92,7 @@ class LocalController extends OcsController {
* @param EventRequest $eventRequest
* @param PointService $pointService
* @param FilesService $filesService
* @param CronService $cronService
* @param ConfigService $configService
*/
public function __construct(
Expand All @@ -97,6 +102,7 @@ public function __construct(
EventRequest $eventRequest,
PointService $pointService,
FilesService $filesService,
CronService $cronService,
ConfigService $configService
) {
parent::__construct($appName, $request);
Expand All @@ -105,6 +111,7 @@ public function __construct(
$this->eventRequest = $eventRequest;
$this->pointService = $pointService;
$this->filesService = $filesService;
$this->cronService = $cronService;
$this->configService = $configService;
}

Expand Down Expand Up @@ -153,6 +160,7 @@ public function getRestoringPoint(): DataResponse {
*/
public function getSettings(): DataResponse {
$settings = $this->configService->getSettings();
$settings = array_merge($settings, $this->cronService->nextBackups());

return new DataResponse($settings);
}
Expand Down
108 changes: 14 additions & 94 deletions lib/Cron/Backup.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use ArtificialOwl\MySmallPhpTools\Traits\Nextcloud\nc23\TNC23Logger;
use OC\BackgroundJob\TimedJob;
use OCA\Backup\Service\ConfigService;
use OCA\Backup\Service\CronService;
use OCA\Backup\Service\PointService;
use Throwable;

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


const MARGIN = 1800;


/** @var PointService */
private $pointService;

/** @var CronService */
private $cronService;

/** @var ConfigService */
private $configService;

Expand All @@ -64,13 +66,19 @@ class Backup extends TimedJob {
* Backup constructor.
*
* @param PointService $pointService
* @param CronService $cronService
* @param ConfigService $configService
*/
public function __construct(PointService $pointService, ConfigService $configService) {
public function __construct(
PointService $pointService,
CronService $cronService,
ConfigService $configService
) {
$this->setInterval(900);
// $this->setInterval(1);

$this->pointService = $pointService;
$this->cronService = $cronService;
$this->configService = $configService;
}

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

if (!$this->verifyTime($time)) {
if (!$this->cronService->verifyTime($time)) {
return;
}

Expand All @@ -97,75 +105,14 @@ protected function run($argument) {
*
*/
private function runBackup(int $time): void {
if ($this->verifyFullBackup($time)) {
if ($this->cronService->verifyFullBackup($time)) {
$this->runFullBackup();
} else if ($this->verifyIncrementalBackup($time)) {
} else if ($this->cronService->verifyIncrementalBackup($time)) {
$this->runIncrementalBackup();
}
}


/**
* @param int $time
*
* @return bool
*/
private function verifyTime(int $time): bool {
[$st, $end] = explode('-', $this->configService->getAppValue(ConfigService::TIME_SLOTS));

if (!is_numeric($st) || !is_numeric($end)) {
$this->log(3, 'Issue with Time Slots format, please check configuration');

return false;
}

$st = (int)$st;
$end = (int)$end;

$timeStart = mktime(
$st,
0,
0,
(int)date('n', $time),
// we go back one day in time under some condition
(int)date('j', $time) - ($st >= $end) * ((int)date('H', $time) < $end),
(int)date('Y', $time)
);

$timeEnd = mktime(
$end,
0,
0,
(int)date('n', $time),
// we go one day forward on a night-day configuration (ie. 23-5)
(int)date('j', $time) + ($st >= $end) * ((int)date('H', $time) > $end),
(int)date('Y', $time)
);

return ($timeStart < $time && $time < $timeEnd);
}


/**
* @param int $time
*
* @return bool
*/
private function verifyFullBackup(int $time): bool {
if (!$this->configService->getAppValueBool(ConfigService::ALLOW_WEEKDAY)
&& !$this->isWeekEnd($time)) {
return false;
}

$last = $this->configService->getAppValueInt(ConfigService::DATE_FULL_RP);
$delay = $this->configService->getAppValueInt(ConfigService::DELAY_FULL_RP);
$delayUnit = $this->configService->getAppValue(ConfigService::DELAY_UNIT);
$delay = $delay * 3600 * (($delayUnit !== 'h') ? 24 : 1);

return ($last + $delay - self::MARGIN < $time);
}


private function runFullBackup(): void {
try {
$this->pointService->create(true);
Expand All @@ -174,24 +121,6 @@ private function runFullBackup(): void {
}


/**
* @param int $time
*
* @return bool
*/
private function verifyIncrementalBackup(int $time): bool {
$last = max(
$this->configService->getAppValueInt(ConfigService::DATE_PARTIAL_RP),
$this->configService->getAppValueInt(ConfigService::DATE_FULL_RP)
);
$delay = $this->configService->getAppValueInt(ConfigService::DELAY_FULL_RP);
$delayUnit = $this->configService->getAppValue(ConfigService::DELAY_UNIT);
$delay = $delay * 3600 * (($delayUnit !== 'h') ? 24 : 1);

return ($last + $delay - self::MARGIN < $time);
}


/**
*
*/
Expand All @@ -203,13 +132,4 @@ private function runIncrementalBackup(): void {
}


/**
* @param int $time
*
* @return bool
*/
private function isWeekEnd(int $time): bool {
return ((int)date('N', $time) >= 6);
}

}
46 changes: 46 additions & 0 deletions lib/Exceptions/SettingsException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);


/**
* Nextcloud - Backup now. Restore later.
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Maxence Lange <maxence@artificial-owl.com>
* @copyright 2021, Maxence Lange <maxence@artificial-owl.com>
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/


namespace OCA\Backup\Exceptions;


use Exception;


/**
* Class SettingsException
*
* @package OCA\Backup\Exceptions
*/
class SettingsException extends Exception {

}

31 changes: 27 additions & 4 deletions lib/Model/RestoringChunkPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ class RestoringChunkPart implements JsonSerializable, IDeserializable {
/** @var string */
private $name;

/** @var int */
private $order;

/** @var string */
private $content = '';

Expand All @@ -70,9 +73,11 @@ class RestoringChunkPart implements JsonSerializable, IDeserializable {
* RestoringChunkPart constructor.
*
* @param string $name
* @param int $order
*/
public function __construct(string $name = '') {
public function __construct(string $name = '', int $order = 0) {
$this->name = $name;
$this->order = $order;
}


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


/**
* @param int $order
*
* @return RestoringChunkPart
*/
public function setOrder(int $order): self {
$this->order = $order;

return $this;
}

/**
* @return int
*/
public function getOrder(): int {
return $this->order;
}


/**
* @param bool $encrypted
*
Expand Down Expand Up @@ -158,9 +182,6 @@ public function setEncryptedChecksum(string $encryptedChecksum): self {
}





/**
* @return string
*/
Expand Down Expand Up @@ -199,6 +220,7 @@ public function getContent(): string {
*/
public function import(array $data): IDeserializable {
$this->setName($this->get('name', $data))
->setOrder($this->getInt('order', $data))
->setContent($this->get('content', $data))
->setEncrypted($this->getBool('encrypted', $data))
->setChecksum($this->get('checksum', $data))
Expand All @@ -214,6 +236,7 @@ public function import(array $data): IDeserializable {
public function jsonSerialize(): array {
$arr = [
'name' => $this->getName(),
'order' => $this->getOrder(),
'encrypted' => $this->isEncrypted(),
'checksum' => $this->getChecksum(),
'encryptedChecksum' => $this->getEncryptedChecksum()
Expand Down

0 comments on commit 727d7cc

Please sign in to comment.