Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Profile backup #430

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"doctrine/annotations": "^1.10",
"lavary/crunz": "^2.2",
"symfony/routing": "^5.1",
"robmorgan/phinx": "^0.12.4"
"robmorgan/phinx": "^0.12.4",
"symfony/yaml": "^5.3"
},
"autoload": {
"psr-4": {
Expand Down
33 changes: 21 additions & 12 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions db/migrations/20210819182053_profile_backup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
declare(strict_types=1);

use Phinx\Migration\AbstractMigration;

final class ProfileBackup extends AbstractMigration
{
public function change(): void
{
$table = $this->table('Profile_Backup_Strategies', ['id' => "PBS_ID", 'primary_key' => ["PBS_ID"]]);
$table->addColumn('PBS_Date_Created', 'datetime', ['default' => 'CURRENT_TIMESTAMP'])
->addColumn('PBS_Name', 'string')
->create();

$this->execute("INSERT INTO `Profile_Backup_Strategies`(`PBS_Name`) VALUES ('Download & Archive')");

$table = $this->table('Profile_Backup_Schedule', ['id' => "PBS_ID", 'primary_key' => ["PBS_ID"]]);
$table->addColumn('PBS_Date_Created', 'datetime', ['default' => 'CURRENT_TIMESTAMP'])
->addColumn('PBS_User_ID', 'integer')
->addColumn('PBS_Schedule_String', 'string')
->addColumn('PBS_Retention', 'integer')
->addColumn('PBS_Host_ID', 'integer')
->addColumn('PBS_Project', 'string')
->addColumn('PBS_Stratergy_ID', 'integer')
->addColumn('PBS_Disabled', 'boolean', ['null'=>false, "default"=>0])
->addColumn('PBS_Disabled_Date', 'datetime', ['null'=>true])
->addColumn('PBS_Disabled_By', 'integer', ['null'=>true])
->addForeignKey('PBS_User_ID', 'Users', 'User_ID', ['delete'=> 'RESTRICT', 'update'=> 'RESTRICT'])
->addForeignKey('PBS_Stratergy_ID', 'Profile_Backup_Strategies', 'PBS_ID', ['delete'=> 'RESTRICT', 'update'=> 'RESTRICT'])
->addForeignKey('PBS_Disabled_By', 'Users', 'User_ID', ['delete'=> 'RESTRICT', 'update'=> 'RESTRICT'])
->addForeignKey('PBS_Host_ID', 'Hosts', 'Host_ID', ['delete'=> 'CASCADE', 'update'=> 'RESTRICT'])
->create();

$table = $this->table('Profile_Backups', ['id' => "PB_ID", 'primary_key' => ["PB_ID"]]);
$table->addColumn('PB_Date_Created', 'datetime', ['default' => 'CURRENT_TIMESTAMP'])
->addColumn('PB_Host_ID', 'integer')
->addColumn('PB_Local_Path', 'string')
->addColumn('PB_Project', 'string')
->addColumn('PB_Filesize', 'biginteger')
->addColumn('PB_Deleted', 'datetime', ['null'=>true])
->addForeignKey('PB_Host_ID', 'Hosts', 'Host_ID', ['delete'=> 'RESTRICT', 'update'=> 'RESTRICT'])
->create();
}
}
7 changes: 7 additions & 0 deletions src/classes/Constants/ProfileBackupStrategies.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
namespace dhope0000\LXDClient\Constants;

class ProfileBackupStrategies
{
const DOWNLOAD_AND_ARCHIVE = 1;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace dhope0000\LXDClient\Controllers\Backups\Profiles;

use dhope0000\LXDClient\Tools\Backups\Profiles\GetProfilesBackupsOverview;

class GetProfilesBackupsOverviewController
{
private $getProfilesBackupsOverview;

public function __construct(GetProfilesBackupsOverview $getProfilesBackupsOverview)
{
$this->getProfilesBackupsOverview = $getProfilesBackupsOverview;
}

public function get($userId)
{
return $this->getProfilesBackupsOverview->get($userId);
}
}
67 changes: 67 additions & 0 deletions src/classes/Model/Backups/Profiles/FetchProfileBackups.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace dhope0000\LXDClient\Model\Backups\Profiles;

use dhope0000\LXDClient\Model\Database\Database;

class FetchProfileBackups
{
public function __construct(Database $database)
{
$this->database = $database->dbObject;
}

public function fetchAll()
{
$sql = "SELECT
`PB_ID` as `id`,
`PB_Date_Created` as `storedDateCreated`,
`PB_Host_ID` as `hostId`,
`PB_Local_Path` as `localPath`,
`PB_Project` as `project`,
`PB_Deleted` as `deletedDate`,
`PB_Filesize` as `filesize`
FROM
`Profile_Backups`
ORDER BY
`storedDateCreated` DESC
";
$do = $this->database->query($sql);
return $do->fetchAll(\PDO::FETCH_ASSOC);
}

public function fetchBackupsUserCanAccess(int $userId)
{
$sql = "SELECT
`PB_ID` as `id`,
`PB_Date_Created` as `storedDateCreated`,
`PB_Host_ID` as `hostId`,
`PB_Local_Path` as `localPath`,
`PB_Project` as `project`,
`PB_Deleted` as `deletedDate`,
`PB_Filesize` as `filesize`
FROM
`Profile_Backups`
WHERE
`PB_Project` IN(
SELECT
`UAP_Project`
FROM
`User_Allowed_Projects`
WHERE
`UAP_User_ID` = :userId
) AND `PB_Host_ID` IN(
SELECT
`UAP_Host_ID`
FROM
`User_Allowed_Projects`
WHERE
`UAP_User_ID` = :userId
)";
$do = $this->database->prepare($sql);
$do->execute([
":userId"=>$userId
]);
return $do->fetchAll(\PDO::FETCH_ASSOC);
}
}
40 changes: 40 additions & 0 deletions src/classes/Model/Hosts/Backups/Profiles/InsertProfilesBackup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace dhope0000\LXDClient\Model\Hosts\Backups\Profiles;

use dhope0000\LXDClient\Model\Database\Database;

class InsertProfilesBackup
{
public function __construct(Database $database)
{
$this->database = $database->dbObject;
}

public function insert(
int $hostId,
string $project,
string $localPath,
int $filesize
) {
$sql = "INSERT INTO `Profile_Backups`(
`PB_Host_ID`,
`PB_Local_Path`,
`PB_Project`,
`PB_Filesize`
) VALUES(
:hostId,
:localPath,
:project,
:filesize
)";
$do = $this->database->prepare($sql);
$do->execute([
":hostId"=>$hostId,
":localPath"=>$localPath,
":project"=>$project,
":filesize"=>$filesize
]);
return $this->database->lastInsertId();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace dhope0000\LXDClient\Model\Hosts\Backups\Profiles\Schedules;

use dhope0000\LXDClient\Model\Database\Database;

class FetchProfilesBackupSchedules
{
public function __construct(Database $database)
{
$this->database = $database->dbObject;
}

public function fetchActiveSchedsGroupedByHostId()
{
$sql = "SELECT
`PBS_Host_ID` as `hostId`,
`PBS_Project` as `project`,
`PBS_Schedule_String` as `scheduleString`,
`PBS_Stratergy_ID` as `strategyId`,
`PBS_Retention` as `scheduleRetention`,
`Profile_Backup_Strategies`.`PBS_Name` as `strategyName`
FROM
`Profile_Backup_Schedule`
LEFT JOIN `Profile_Backup_Strategies` ON
`PBS_Stratergy_ID` = `Profile_Backup_Strategies`.`PBS_ID`
WHERE
`PBS_Disabled` = 0
ORDER BY
`Profile_Backup_Schedule`.`PBS_ID` ASC
";
$do = $this->database->query($sql);
return $do->fetchAll(\PDO::FETCH_GROUP|\PDO::FETCH_ASSOC);
}

public function fetchActive(int $hostId)
{
$sql = "SELECT
`IBS_Host_ID` as `hostId`,
`IBS_Instance` as `instance`,
`IBS_Project` as `project`,
`IBS_Schedule_String` as `scheduleString`,
`IBS_BS_ID` as `strategyId`,
`IBS_Retention` as `scheduleRetention`,
`Backup_Strategies`.`BS_Name` as `strategyName`
FROM
`Instance_Backup_Schedule`
LEFT JOIN `Backup_Strategies` ON
`Instance_Backup_Schedule`.`IBS_BS_ID` = `Backup_Strategies`.`BS_ID`
WHERE
`IBS_Host_ID` = :hostId
AND
`IBS_Disabled` = 0
ORDER BY
`IBS_ID` ASC
";
$do = $this->database->prepare($sql);
$do->execute([
":hostId"=>$hostId
]);
return $do->fetchAll(\PDO::FETCH_ASSOC);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace dhope0000\LXDClient\Model\Hosts\Backups\Profiles\Schedules;

use dhope0000\LXDClient\Model\Database\Database;

class InsertProfileBackupSchedule
{
public function __construct(Database $database)
{
$this->database = $database->dbObject;
}

public function insert(
int $userId,
int $hostId,
string $project,
string $schedule,
int $strategyId,
int $retention
) {
$sql = "INSERT INTO `Profile_Backup_Schedule`
(
`PBS_User_ID`,
`PBS_Host_ID`,
`PBS_Project`,
`PBS_Schedule_String`,
`PBS_Stratergy_ID`,
`PBS_Retention`
) VALUES (
:userId,
:hostId,
:project,
:schedule,
:strategyId,
:retention
);";
$do = $this->database->prepare($sql);
$do->execute([
":userId"=>$userId,
":hostId"=>$hostId,
":project"=>$project,
":schedule"=>$schedule,
":strategyId"=>$strategyId,
":retention"=>$retention
]);
return $do->rowCount() ? true : false;
}
}
Loading