Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mortenscheel/e810836fb2e1f85d0c69343f0c5c30f4 to your computer and use it in GitHub Desktop.
Save mortenscheel/e810836fb2e1f85d0c69343f0c5c30f4 to your computer and use it in GitHub Desktop.
Extension to spatie/laravel-backup DefaultStrategy
<?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);
}
}
@mortenscheel
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment