dgd-backupper-php/app/Commands/BackupCommand.php

92 lines
2.3 KiB
PHP

<?php
namespace App\Commands;
use Illuminate\Console\Scheduling\Schedule;
use LaravelZero\Framework\Commands\Command;
class BackupCommand extends Command
{
/**
* The signature of the command.
*
* @var string
*/
protected $signature = 'backup {containers*} {--data} {--mysql} {--pack} {--upload} {--clean}';
/**
* The description of the command.
*
* @var string
*/
protected $description = 'Backup docker containers and all data.';
/**
* Execute the console command.
*
* @return mixed
* @throws \Throwable
*/
public function handle()
{
$containers = $this->argument('containers');
$this->line("<fg=red>### Backup is starting... ###</>");
$this->call("backup:container", [ 'containers' => $containers]);
if ($this->option('data')) {
$this->call("backup:data");
}
if ($this->option('mysql')) {
$this->call("backup:mysql");
}
date_default_timezone_set('Europe/Istanbul');
$date = date('d-m-Y_H-i-s', time());
if ($this->option('pack')) {
$this->call('backup:pack', [
'name' => 'backup_' . $date,
]);
}
if ($this->option('upload')) {
if (!$this->option('pack')) {
$this->call('backup:pack', [
'name' => 'backup_' . $date,
]);
}
$this->call("backup:upload", [
'name' => 'backup_' . $date . '.tar.gz',
]);
}
if ($this->option('clean')) {
$this->line("<fg=yellow>|-> Cleaning...</>");
$files = glob(env('BACKUP_FOLDER') . '/*');
foreach($files as $file){
is_file($file) ? unlink($file) : null;
}
$files = glob(env('EXPORT_FOLDER') . '/*');
foreach($files as $file){
is_file($file) ? unlink($file) : null;
}
}
$this->line("<fg=blue>|-> The backup and upload process is finished.</>");
return;
}
/**
* Define the command's schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
public function schedule(Schedule $schedule): void
{
// $schedule->command(static::class)->everyMinute();
}
}