Created
July 2, 2019 07:38
-
-
Save mortenscheel/e810836fb2e1f85d0c69343f0c5c30f4 to your computer and use it in GitHub Desktop.
Extension to spatie/laravel-backup DefaultStrategy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Helpers; | |
use Illuminate\Filesystem\Filesystem; | |
use Spatie\Backup\BackupDestination\Backup; | |
use Spatie\Backup\BackupDestination\BackupCollection; | |
use Spatie\Backup\Tasks\Cleanup\Strategies\DefaultStrategy; | |
class DefaultStrategyButKeepOnlyOneLocalBackup extends DefaultStrategy | |
{ | |
public function deleteOldBackups(BackupCollection $backups) | |
{ | |
try { | |
/** @var Backup $latest */ | |
$latest = $backups->shift(); | |
$reflection = new \ReflectionClass($latest); | |
/** @var Filesystem $filesystem */ | |
$property = $reflection->getProperty('disk'); | |
$property->setAccessible(true); | |
$filesystem = $property->getValue($latest); | |
$driver = $filesystem->getDriver(); | |
if ($driver instanceof \League\Flysystem\Filesystem){ | |
$adapter = $driver->getAdapter(); | |
if ($adapter instanceof \League\Flysystem\Adapter\Local){ | |
// Delete remaining backups | |
$backups->each(function (Backup $backup){ | |
$backup->delete(); | |
}); | |
return; | |
} | |
} | |
} catch (\ReflectionException $e) { | |
} | |
parent::deleteOldBackups($backups); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Useful if you store your backups on both local and remote storage, and you want to save disk space on the local file system.
Only the latest backup file is kept on local storage. Other storages are unaffected.