Add comprehensive backup functionality and refactor email handling

Implemented server, database, and environment backups with configurable modules. Introduced new routes and configuration classes for backups alongside improved zip and S3 storage. Replaced PHPMailer with a cURL-based email service for streamlined message sending.
This commit is contained in:
Jepp9350
2025-02-11 17:41:57 +01:00
parent 797630d8bd
commit 555d4e9918
11 changed files with 230 additions and 81 deletions
+73 -6
View File
@@ -1,7 +1,9 @@
<?php
namespace classes;
require_once WD . '/modules/backups/backups_c.php';
use backups\backups_c;
use Exception;
use interfaces\minio_backups_i;
use traits\minio_t;
@@ -10,15 +12,33 @@ class backup_store implements minio_backups_i
{
use minio_t;
const backup_s3_prefix = 'backups/'; // The prefix for the backup files in the S3 bucket
const metadata_s3_prefix = 'metadata/'; // The prefix for the metadata files in the S3 bucket
const local_backup_path = '/tmp/backups/'; // The path to store the backups locally
/**
* The prefix for the backup files in the S3 bucket
* @var string
*/
const backup_s3_prefix = 'backups/';
/**
* The prefix for the metadata files in the S3 bucket
* @var string
*/
const metadata_s3_prefix = 'metadata/'; // The prefix for the backup files in the S3 bucket
/**
* The path to store the backups locally
* @var string
*/
const local_backup_path = '/tmp/backups/'; // The prefix for the metadata files in the S3 bucket
/**
* The configuration for the backup store
* @var backups_c
*/
public backups_c $config;
/**
* @throws Exception If the bucket does not exist, is not accessible, or is not configured.
*/
public function __construct()
{
$this->config = new backups_c();
self::setBucket('backups'); // Change the bucket to backups
self::validateBucket();
}
@@ -120,7 +140,13 @@ class backup_store implements minio_backups_i
}
// Backup the database
$database_path = $this->backupDatabase($backup_uuid);
$database_path = self::backupDatabase($backup_uuid);
// Web server files
$webserver_files_path = self::backupServerFiles($backup_uuid);
// Environment variables
$env_path = self::backupEnvironmentVariables($backup_uuid);
// Create the backup path
$backup_path = self::local_backup_path . 'backup_' . $backup_uuid . '.zip';
@@ -138,6 +164,12 @@ class backup_store implements minio_backups_i
// Add the database backup to the backup directory
copy($database_path, self::local_backup_path . 'backup_' . $backup_uuid . '/database.sql');
// Add the web server files to the backup directory
copy($webserver_files_path, self::local_backup_path . 'backup_' . $backup_uuid . '/www.zip');
// Add the environment variables to the backup directory
copy($env_path, self::local_backup_path . 'backup_' . $backup_uuid . '/environment_variables.json');
// Zip the backup directory
exec('zip -r ' . $backup_path . ' ' . self::local_backup_path . 'backup_' . $backup_uuid);
@@ -154,7 +186,7 @@ class backup_store implements minio_backups_i
global /** @var db $db */
$db;
// Backup the database
$path = self::local_backup_path . 'backup_' . $backup_uuid;
$path = self::local_backup_path . 'backup_' . $backup_uuid . '/database.sql';
$result = $db->backupDatabase($path);
if (!$result) {
throw new Exception('Failed to backup the database!');
@@ -162,6 +194,28 @@ class backup_store implements minio_backups_i
return $path;
}
/**
* @inheritDoc
*/
public function backupServerFiles(string $backup_uuid): string
{
// Backup the server files from the /var/www/html directory
$path = self::local_backup_path . 'backup_' . $backup_uuid . '/www.zip';
exec('zip -r ' . $path . ' /var/www/html');
return $path;
}
/**
* @inheritDoc
*/
public function backupEnvironmentVariables(string $backup_uuid): string
{
// Backup the config variables
$path = self::local_backup_path . 'backup_' . $backup_uuid . '/environment_variables.json';
file_put_contents($path, json_encode($_ENV));
return $path;
}
/**
* @inheritDoc
*/
@@ -202,6 +256,15 @@ class backup_store implements minio_backups_i
// Remove the database backup
unlink(self::local_backup_path . 'backup_' . $backup_uuid . '/database.sql');
// Remove the web server files backup
unlink(self::local_backup_path . 'backup_' . $backup_uuid . '/www.zip');
// Remove the environment variables backup
unlink(self::local_backup_path . 'backup_' . $backup_uuid . '/environment_variables.json');
// Remove the backup directory
rmdir(self::local_backup_path . 'backup_' . $backup_uuid);
}
/**
@@ -220,7 +283,11 @@ class backup_store implements minio_backups_i
return [];
}
foreach ( $objects['Contents'] as $object ) {
$backup_uuid = str_replace([self::metadata_s3_prefix, '.json'], '', $object['Key']);
$backup_uuid = str_replace(
self::metadata_s3_prefix . 'backup_',
'',
str_replace('.json', '', $object['Key'])
);
$backups[] = $this->getBackupMetadata($backup_uuid);
}
return $backups;