-
I would like to create 3 types of backups.
These have the name (--filename):
Is there a way to create the following strategy to delete the data as follows:
|
Beta Was this translation helpful? Give feedback.
Answered by
Muetze42
May 29, 2020
Replies: 1 comment
-
I have now created my own Cleanup Command. Here are the code snippets, maybe someone else can use them. Situation for this snippet:
My Schedule $schedule->command('clean:backup')->daily()->at('03:00');
$schedule->command('backup:run --only-files --filename=images_'.Carbon::now()->format('Y-m-d-H-i-s').'.zip')->daily()->at('05:00');
$schedule->command('backup:run --only-db --filename=database_'.Carbon::now()->format('Y-m-d-H-i-s').'.zip')->hourly(); config/backup.php ...
'destination' => [
'filename_prefix' => '',
'disks' => [
'local',
'backup-ftp',
],
],
...
'monitor_backups' => [
[
'name' => 'backup',
'disks' => [
'local',
'backup-ftp',
],
...
'cleanup' => [
'strategy' => \Spatie\Backup\Tasks\Cleanup\Strategies\DefaultStrategy::class,
'huth_strategy' => [
'images' => '2 days',
'database' => '7 days',
],
... app\Console\Commands\CleanBackup.php <?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
use Carbon\Carbon;
class CleanBackup extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'clean:backup';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Remove all backups older than specified time in config.';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$this->info('Start cleanup backups');
$config = config('backup');
$folder = $config['backup']['name'];
$disks = $config['backup']['destination']['disks'];
$cleanup = $config['cleanup']['huth_strategy'];
foreach ($disks as $disk) {
$this->info('Cleanup disk '.$disk);
$storage = Storage::disk($disk);
$files = $storage->allFiles($folder);
foreach ($files as $file) {
$type = explode('_', basename($file))[0];
if (isset($cleanup[$type]) && Carbon::createFromTimestamp($storage->lastModified($file)) < Carbon::now()->sub($cleanup[$type])) {
$this->line('Delete '.basename($file).' from '. $disk);
$storage->delete($file);
}
}
}
$this->info('Cleanup complete');
return 1;
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
Muetze42
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have now created my own Cleanup Command. Here are the code snippets, maybe someone else can use them.
Situation for this snippet:
3 different backups on two disks (local & backup-ftp)
My Schedule