Spatie\Backup\Tasks\Cleanup\CleanupJob::run PHP Method

run() public method

public run ( )
    public function run()
    {
        $this->backupDestinations->each(function (BackupDestination $backupDestination) {
            try {
                if (!$backupDestination->isReachable()) {
                    throw new Exception("Could not connect to disk {$backupDestination->diskName()} because: {$backupDestination->connectionError()}");
                }
                consoleOutput()->info("Cleaning backups of {$backupDestination->backupName()} on disk {$backupDestination->diskName()}...");
                $this->strategy->deleteOldBackups($backupDestination->backups());
                event(new CleanupWasSuccessful($backupDestination));
                $usedStorage = Format::humanReadableSize($backupDestination->usedStorage());
                consoleOutput()->info("Used storage after cleanup: {$usedStorage}.");
            } catch (Exception $exception) {
                consoleOutput()->error("Cleanup failed because: {$exception->getMessage()}.");
                event(new CleanupHasFailed($exception));
            }
        });
    }

Usage Example

Example #1
0
 public function handle()
 {
     consoleOutput()->comment('Starting cleanup...');
     try {
         $config = config('laravel-backup');
         $backupDestinations = BackupDestinationFactory::createFromArray($config['backup']);
         $strategy = app($config['cleanup']['strategy']);
         $cleanupJob = new CleanupJob($backupDestinations, $strategy);
         $cleanupJob->run();
         consoleOutput()->comment('Cleanup completed!');
     } catch (Exception $exception) {
         event(new CleanupHasFailed($exception));
         return -1;
     }
 }