Clean Command Added
continuous-integration/drone/push Build is passing Details

master
Muhep Atasoy 2019-12-14 22:56:46 +03:00
parent b6c7fbbbaf
commit aeff1afdb7
5 changed files with 34 additions and 89 deletions

View File

@ -15,7 +15,7 @@ class BackupCommand extends Command
* *
* @var string * @var string
*/ */
protected $signature = 'backup {containers*} {--data} {--mysql} {--upload}'; protected $signature = 'backup {containers*} {--data} {--mysql} {--upload} {--clean}';
/** /**
* The description of the command. * The description of the command.
@ -47,6 +47,14 @@ class BackupCommand extends Command
'--password' => env('MYSQL_PASSWORD'), '--password' => env('MYSQL_PASSWORD'),
]); ]);
} }
if ($this->option('clean')) {
$files = glob(env('BACKUP_FOLDER') . '/*');
foreach($files as $file){
is_file($file) ? unlink($file) : null;
}
}
$this->line("<fg=blue>|-> The backup and upload process is finished.</>"); $this->line("<fg=blue>|-> The backup and upload process is finished.</>");
return; return;
} }

View File

@ -33,13 +33,14 @@ class ContainerBackupCommand extends Command
public function handle() public function handle()
{ {
$containers = $this->argument('containers'); $containers = $this->argument('containers');
$bm = new BackupManager(); $backupManager = new BackupManager();
$backupManager->setBackupFolder(env('BACKUP_FOLDER'));
foreach ($containers as $container) { foreach ($containers as $container) {
$this->line("<fg=blue>|-> Backing up {$container}...</>"); $this->line("<fg=blue>|-> Backing up {$container}...</>");
$builder = new ContainerBuilder(); $builder = new ContainerBuilder();
$builder->setBackupFolder(env('BACKUP_FOLDER'))->setContainerName($container); $builder->setContainerName($container);
$bm->setBuilder($builder)->execute(); $backupManager->setBuilder($builder)->execute();
if ($bm->getShell()->getReturnValue() == 0) { if ($backupManager->getShell()->getReturnValue() == 0) {
$this->line("<fg=green> |-> {$container} successfully backed up.</>"); $this->line("<fg=green> |-> {$container} successfully backed up.</>");
} else { } else {
$this->line("<fg=red> |-> {$container} could not be backed up.</>"); $this->line("<fg=red> |-> {$container} could not be backed up.</>");

View File

@ -36,9 +36,9 @@ class DataBackupCommand extends Command
$bm = new BackupManager(); $bm = new BackupManager();
$builder = new DataBuilder(); $builder = new DataBuilder();
$builder->setDataFolder(env('DATA_FOLDER')) $builder->setDataFolder(env('DATA_FOLDER'));
->setBackupFolder(env('BACKUP_FOLDER')); $bm->setBackupFolder(env('BACKUP_FOLDER'))
$bm->setBuilder($builder)->execute(); ->setBuilder($builder)->execute();
if ($bm->getShell()->getReturnValue() == 0) { if ($bm->getShell()->getReturnValue() == 0) {
$this->line("<fg=green> |-> Data folder successfully backed up.</>"); $this->line("<fg=green> |-> Data folder successfully backed up.</>");

View File

@ -44,9 +44,11 @@ class MysqlBackupCommand extends Command
$builder = new DatabaseBuilder(); $builder = new DatabaseBuilder();
$builder->setCredential($credentials); $builder->setCredential($credentials);
$builder->setBackupFolder(env('BACKUP_FOLDER')) $builder->setContainerName($container);
->setContainerName($container);
$bm->setBuilder($builder)->execute(); $bm->setBackupFolder(env('BACKUP_FOLDER'))
->setBuilder($builder)->execute();
if ($bm->getShell()->getReturnValue() == 0) { if ($bm->getShell()->getReturnValue() == 0) {
$this->line("<fg=green> |-> Mysql databases successfully backed up.</>"); $this->line("<fg=green> |-> Mysql databases successfully backed up.</>");
} else { } else {

View File

@ -2,7 +2,6 @@
namespace App\Utils; namespace App\Utils;
use AdamBrett\ShellWrapper\Command\Builder as CommandBuilder;
use AdamBrett\ShellWrapper\Runners\Exec; use AdamBrett\ShellWrapper\Runners\Exec;
use App\Exceptions\BackupFolderNotFoundException; use App\Exceptions\BackupFolderNotFoundException;
use App\Exceptions\ExportFolderNotFoundException; use App\Exceptions\ExportFolderNotFoundException;
@ -11,19 +10,14 @@ use Throwable;
class BackupManager class BackupManager
{ {
private $exportFolder; private $exportFolder;
private $dataFolder;
private $backupFolder; private $backupFolder;
private $shell; private $shell;
private $params;
private $builder; private $builder;
private $containerName; private $containerName;
public function __construct() public function __construct()
{ {
$this->shell = new Exec(); $this->shell = new Exec();
$this->builder = new CommandBuilder("");
$this->backupFolder = ".";
$this->exportFolder = ".";
} }
/** /**
@ -38,18 +32,6 @@ class BackupManager
return $this; return $this;
} }
/**
* @param mixed $dataFolder
* @return BackupManager
* @throws Throwable
*/
public function setDataFolder($dataFolder): BackupManager
{
throw_if(!file_exists($dataFolder), new BackupFolderNotFoundException('Data folder not exist!'));
$this->dataFolder = $dataFolder;
return $this;
}
/** /**
* @param mixed $exportFolder * @param mixed $exportFolder
* @return BackupManager * @return BackupManager
@ -62,34 +44,6 @@ class BackupManager
return $this; return $this;
} }
/**
* @param $param
* @return BackupManager
*/
public function addParams(array $params): BackupManager
{
$this->params = $params;
return $this;
}
/**
* @param mixed $containerName
* @return BackupManager
*/
public function setContainerName($containerName): BackupManager
{
$this->containerName = $containerName;
return $this;
}
/**
* @return mixed
*/
public function getParams()
{
return $this->params;
}
/** /**
* @return Exec * @return Exec
*/ */
@ -106,14 +60,6 @@ class BackupManager
return $this->backupFolder; return $this->backupFolder;
} }
/**
* @return mixed
*/
public function getDataFolder()
{
return $this->dataFolder;
}
/** /**
* @return mixed * @return mixed
*/ */
@ -130,36 +76,24 @@ class BackupManager
return $this->containerName; return $this->containerName;
} }
/*
* @param int $mode
* @return BackupManager
*/
/*public function builder(int $mode): BackupManager
{
switch ($mode) {
case BuilderMode::CONTAINER:
$this->builder = new Builder("docker export {$this->containerName} -o {$this->backupFolder}/{$this->containerName}.tar.gz");
break;
case BuilderMode::DATA:
$this->builder = new Builder("tar -zcf");
break;
case BuilderMode::MYSQL:
$this->builder = new Builder("docker exec");
break;
default:
$this->builder = new Builder("");
}
return $this;
}*/
public function setBuilder(Builder $builder): BackupManager public function setBuilder(Builder $builder): BackupManager
{ {
$this->builder = $builder->builder(); $this->builder = $builder;
return $this; return $this;
} }
public function getBuilder(): Builder
{
return $this->builder;
}
/**
* @return string
* @throws Throwable
*/
public function execute() public function execute()
{ {
return $this->shell->run($this->builder); $builder = $this->getBuilder()->setBackupFolder($this->backupFolder)->builder();
return $this->shell->run($builder);
} }
} }