Primo Committ
This commit is contained in:
76
vendor/spatie/laravel-backup/src/Commands/BackupCommand.php
vendored
Normal file
76
vendor/spatie/laravel-backup/src/Commands/BackupCommand.php
vendored
Normal file
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\Backup\Commands;
|
||||
|
||||
use Exception;
|
||||
use Spatie\Backup\Events\BackupHasFailed;
|
||||
use Spatie\Backup\Exceptions\InvalidCommand;
|
||||
use Spatie\Backup\Tasks\Backup\BackupJobFactory;
|
||||
|
||||
class BackupCommand extends BaseCommand
|
||||
{
|
||||
/** @var string */
|
||||
protected $signature = 'backup:run {--filename=} {--only-db} {--db-name=*} {--only-files} {--only-to-disk=} {--disable-notifications} {--timeout=}';
|
||||
|
||||
/** @var string */
|
||||
protected $description = 'Run the backup.';
|
||||
|
||||
public function handle()
|
||||
{
|
||||
consoleOutput()->comment('Starting backup...');
|
||||
|
||||
$disableNotifications = $this->option('disable-notifications');
|
||||
|
||||
if ($this->option('timeout') && is_numeric($this->option('timeout'))) {
|
||||
set_time_limit((int) $this->option('timeout'));
|
||||
}
|
||||
|
||||
try {
|
||||
$this->guardAgainstInvalidOptions();
|
||||
|
||||
$backupJob = BackupJobFactory::createFromArray(config('backup'));
|
||||
|
||||
if ($this->option('only-db')) {
|
||||
$backupJob->dontBackupFilesystem();
|
||||
}
|
||||
if ($this->option('db-name')) {
|
||||
$backupJob->onlyDbName($this->option('db-name'));
|
||||
}
|
||||
|
||||
if ($this->option('only-files')) {
|
||||
$backupJob->dontBackupDatabases();
|
||||
}
|
||||
|
||||
if ($this->option('only-to-disk')) {
|
||||
$backupJob->onlyBackupTo($this->option('only-to-disk'));
|
||||
}
|
||||
|
||||
if ($this->option('filename')) {
|
||||
$backupJob->setFilename($this->option('filename'));
|
||||
}
|
||||
|
||||
if ($disableNotifications) {
|
||||
$backupJob->disableNotifications();
|
||||
}
|
||||
|
||||
$backupJob->run();
|
||||
|
||||
consoleOutput()->comment('Backup completed!');
|
||||
} catch (Exception $exception) {
|
||||
consoleOutput()->error("Backup failed because: {$exception->getMessage()}.");
|
||||
|
||||
if (! $disableNotifications) {
|
||||
event(new BackupHasFailed($exception));
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
protected function guardAgainstInvalidOptions()
|
||||
{
|
||||
if ($this->option('only-db') && $this->option('only-files')) {
|
||||
throw InvalidCommand::create('Cannot use `only-db` and `only-files` together');
|
||||
}
|
||||
}
|
||||
}
|
||||
18
vendor/spatie/laravel-backup/src/Commands/BaseCommand.php
vendored
Normal file
18
vendor/spatie/laravel-backup/src/Commands/BaseCommand.php
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\Backup\Commands;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use Spatie\Backup\Helpers\ConsoleOutput;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
abstract class BaseCommand extends Command
|
||||
{
|
||||
public function run(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
app(ConsoleOutput::class)->setOutput($this);
|
||||
|
||||
return parent::run($input, $output);
|
||||
}
|
||||
}
|
||||
53
vendor/spatie/laravel-backup/src/Commands/CleanupCommand.php
vendored
Normal file
53
vendor/spatie/laravel-backup/src/Commands/CleanupCommand.php
vendored
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\Backup\Commands;
|
||||
|
||||
use Exception;
|
||||
use Spatie\Backup\BackupDestination\BackupDestinationFactory;
|
||||
use Spatie\Backup\Events\CleanupHasFailed;
|
||||
use Spatie\Backup\Tasks\Cleanup\CleanupJob;
|
||||
use Spatie\Backup\Tasks\Cleanup\CleanupStrategy;
|
||||
|
||||
class CleanupCommand extends BaseCommand
|
||||
{
|
||||
/** @var string */
|
||||
protected $signature = 'backup:clean {--disable-notifications}';
|
||||
|
||||
/** @var string */
|
||||
protected $description = 'Remove all backups older than specified number of days in config.';
|
||||
|
||||
/** @var \Spatie\Backup\Tasks\Cleanup\CleanupStrategy */
|
||||
protected $strategy;
|
||||
|
||||
public function __construct(CleanupStrategy $strategy)
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
$this->strategy = $strategy;
|
||||
}
|
||||
|
||||
public function handle()
|
||||
{
|
||||
consoleOutput()->comment('Starting cleanup...');
|
||||
|
||||
$disableNotifications = $this->option('disable-notifications');
|
||||
|
||||
try {
|
||||
$config = config('backup');
|
||||
|
||||
$backupDestinations = BackupDestinationFactory::createFromArray($config['backup']);
|
||||
|
||||
$cleanupJob = new CleanupJob($backupDestinations, $this->strategy, $disableNotifications);
|
||||
|
||||
$cleanupJob->run();
|
||||
|
||||
consoleOutput()->comment('Cleanup completed!');
|
||||
} catch (Exception $exception) {
|
||||
if (! $disableNotifications) {
|
||||
event(new CleanupHasFailed($exception));
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
101
vendor/spatie/laravel-backup/src/Commands/ListCommand.php
vendored
Normal file
101
vendor/spatie/laravel-backup/src/Commands/ListCommand.php
vendored
Normal file
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\Backup\Commands;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
use Spatie\Backup\BackupDestination\Backup;
|
||||
use Spatie\Backup\Helpers\Format;
|
||||
use Spatie\Backup\Helpers\RightAlignedTableStyle;
|
||||
use Spatie\Backup\Tasks\Monitor\BackupDestinationStatus;
|
||||
use Spatie\Backup\Tasks\Monitor\BackupDestinationStatusFactory;
|
||||
|
||||
class ListCommand extends BaseCommand
|
||||
{
|
||||
/** @var string */
|
||||
protected $signature = 'backup:list';
|
||||
|
||||
/** @var string */
|
||||
protected $description = 'Display a list of all backups.';
|
||||
|
||||
public function handle()
|
||||
{
|
||||
$statuses = BackupDestinationStatusFactory::createForMonitorConfig(config('backup.monitor_backups'));
|
||||
|
||||
$this->displayOverview($statuses)->displayFailures($statuses);
|
||||
}
|
||||
|
||||
protected function displayOverview(Collection $backupDestinationStatuses)
|
||||
{
|
||||
$headers = ['Name', 'Disk', 'Reachable', 'Healthy', '# of backups', 'Newest backup', 'Used storage'];
|
||||
|
||||
$rows = $backupDestinationStatuses->map(function (BackupDestinationStatus $backupDestinationStatus) {
|
||||
return $this->convertToRow($backupDestinationStatus);
|
||||
});
|
||||
|
||||
$this->table($headers, $rows, 'default', [
|
||||
4 => new RightAlignedTableStyle(),
|
||||
6 => new RightAlignedTableStyle(),
|
||||
]);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function convertToRow(BackupDestinationStatus $backupDestinationStatus): array
|
||||
{
|
||||
$destination = $backupDestinationStatus->backupDestination();
|
||||
|
||||
$row = [
|
||||
$destination->backupName(),
|
||||
'disk' => $destination->diskName(),
|
||||
Format::emoji($destination->isReachable()),
|
||||
Format::emoji($backupDestinationStatus->isHealthy()),
|
||||
'amount' => $destination->backups()->count(),
|
||||
'newest' => $this->getFormattedBackupDate($destination->newestBackup()),
|
||||
'usedStorage' => Format::humanReadableSize($destination->usedStorage()),
|
||||
];
|
||||
|
||||
if (! $destination->isReachable()) {
|
||||
foreach (['amount', 'newest', 'usedStorage'] as $propertyName) {
|
||||
$row[$propertyName] = '/';
|
||||
}
|
||||
}
|
||||
|
||||
if ($backupDestinationStatus->getHealthCheckFailure() !== null) {
|
||||
$row['disk'] = '<error>'.$row['disk'].'</error>';
|
||||
}
|
||||
|
||||
return $row;
|
||||
}
|
||||
|
||||
protected function displayFailures(Collection $backupDestinationStatuses)
|
||||
{
|
||||
$failed = $backupDestinationStatuses
|
||||
->filter(function (BackupDestinationStatus $backupDestinationStatus) {
|
||||
return $backupDestinationStatus->getHealthCheckFailure() !== null;
|
||||
})
|
||||
->map(function (BackupDestinationStatus $backupDestinationStatus) {
|
||||
return [
|
||||
$backupDestinationStatus->backupDestination()->backupName(),
|
||||
$backupDestinationStatus->backupDestination()->diskName(),
|
||||
$backupDestinationStatus->getHealthCheckFailure()->healthCheck()->name(),
|
||||
$backupDestinationStatus->getHealthCheckFailure()->exception()->getMessage(),
|
||||
];
|
||||
});
|
||||
|
||||
if ($failed->isNotEmpty()) {
|
||||
$this->warn('');
|
||||
$this->warn('Unhealthy backup destinations');
|
||||
$this->warn('-----------------------------');
|
||||
$this->table(['Name', 'Disk', 'Failed check', 'Description'], $failed->all());
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function getFormattedBackupDate(Backup $backup = null)
|
||||
{
|
||||
return is_null($backup)
|
||||
? 'No backups present'
|
||||
: Format::ageInDays($backup->date());
|
||||
}
|
||||
}
|
||||
40
vendor/spatie/laravel-backup/src/Commands/MonitorCommand.php
vendored
Normal file
40
vendor/spatie/laravel-backup/src/Commands/MonitorCommand.php
vendored
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\Backup\Commands;
|
||||
|
||||
use Spatie\Backup\Events\HealthyBackupWasFound;
|
||||
use Spatie\Backup\Events\UnhealthyBackupWasFound;
|
||||
use Spatie\Backup\Tasks\Monitor\BackupDestinationStatusFactory;
|
||||
|
||||
class MonitorCommand extends BaseCommand
|
||||
{
|
||||
/** @var string */
|
||||
protected $signature = 'backup:monitor';
|
||||
|
||||
/** @var string */
|
||||
protected $description = 'Monitor the health of all backups.';
|
||||
|
||||
public function handle()
|
||||
{
|
||||
$hasError = false;
|
||||
|
||||
$statuses = BackupDestinationStatusFactory::createForMonitorConfig(config('backup.monitor_backups'));
|
||||
|
||||
foreach ($statuses as $backupDestinationStatus) {
|
||||
$diskName = $backupDestinationStatus->backupDestination()->diskName();
|
||||
|
||||
if ($backupDestinationStatus->isHealthy()) {
|
||||
$this->info("The backups on {$diskName} are considered healthy.");
|
||||
event(new HealthyBackupWasFound($backupDestinationStatus));
|
||||
} else {
|
||||
$hasError = true;
|
||||
$this->error("The backups on {$diskName} are considered unhealthy!");
|
||||
event(new UnHealthyBackupWasFound($backupDestinationStatus));
|
||||
}
|
||||
}
|
||||
|
||||
if ($hasError) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user