This commit is contained in:
Hesabix 2025-03-06 00:24:11 +00:00
parent 0ae0202d7c
commit 3e953c2f2b

View file

@ -11,6 +11,7 @@ use Symfony\Component\Process\Process;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Ramsey\Uuid\Uuid; use Ramsey\Uuid\Uuid;
use Symfony\Component\Lock\LockFactory; use Symfony\Component\Lock\LockFactory;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
#[AsCommand( #[AsCommand(
name: 'hesabix:update', name: 'hesabix:update',
@ -20,6 +21,7 @@ class UpdateSoftwareCommand extends Command
{ {
private LoggerInterface $logger; private LoggerInterface $logger;
private LockFactory $lockFactory; private LockFactory $lockFactory;
private ParameterBagInterface $params;
private string $rootDir; private string $rootDir;
private string $appDir; private string $appDir;
private string $archiveDir; private string $archiveDir;
@ -27,12 +29,13 @@ class UpdateSoftwareCommand extends Command
private string $stateFile; private string $stateFile;
private string $env; private string $env;
public function __construct(LoggerInterface $logger, LockFactory $lockFactory) public function __construct(LoggerInterface $logger, LockFactory $lockFactory, ParameterBagInterface $params)
{ {
$this->logger = $logger; $this->logger = $logger;
$this->lockFactory = $lockFactory; $this->lockFactory = $lockFactory;
$this->appDir = dirname(__DIR__, 2); $this->params = $params;
$this->rootDir = dirname($this->appDir); $this->appDir = dirname(__DIR__, 2); // src/Command -> hesabixCore
$this->rootDir = dirname($this->appDir); // hesabixCore -> parent dir
$this->archiveDir = $this->rootDir . '/hesabixArchive'; $this->archiveDir = $this->rootDir . '/hesabixArchive';
$this->backupDir = $this->rootDir . '/../backup'; $this->backupDir = $this->rootDir . '/../backup';
$this->stateFile = $this->backupDir . '/update_state.json'; $this->stateFile = $this->backupDir . '/update_state.json';
@ -287,11 +290,42 @@ class UpdateSoftwareCommand extends Command
private function backupDatabase(): string private function backupDatabase(): string
{ {
$backupFile = $this->backupDir . '/db_backup_' . time() . '.sql'; $backupFile = $this->backupDir . '/db_backup_' . time() . '.sql';
// اصلاح به doctrine:database:dump $dbUrl = $this->params->get('database_url');
$this->runProcess(['php', 'bin/console', 'doctrine:database:dump', '--file=' . $backupFile], $this->appDir, new \Symfony\Component\Console\Output\NullOutput()); $urlParts = parse_url($dbUrl);
$dbHost = $urlParts['host'] ?? 'localhost';
$dbUser = $urlParts['user'] ?? 'root';
$dbPass = $urlParts['pass'] ?? '';
$dbName = ltrim($urlParts['path'] ?? '', '/');
$command = [
'mysqldump',
'-h', $dbHost,
'-u', $dbUser,
'-p' . $dbPass,
$dbName,
'>',
$backupFile
];
$this->runProcess($command, $this->rootDir, new \Symfony\Component\Console\Output\NullOutput());
if (!file_exists($backupFile)) {
throw new \RuntimeException('Failed to create database backup.');
}
return $backupFile; return $backupFile;
} }
private function backupArchive(): string
{
$tarFile = $this->backupDir . '/hesabixArchive_backup_' . time() . '.tar';
$this->runProcess(['tar', '-cf', $tarFile, '-C', $this->rootDir, 'hesabixArchive'], $this->rootDir, new \Symfony\Component\Console\Output\NullOutput());
if (!file_exists($tarFile)) {
throw new \RuntimeException('Failed to create tar backup of hesabixArchive.');
}
return $tarFile;
}
private function restoreArchive(string $backupFile): void private function restoreArchive(string $backupFile): void
{ {
$this->runProcess(['rm', '-rf', $this->archiveDir], $this->rootDir, new \Symfony\Component\Console\Output\NullOutput()); $this->runProcess(['rm', '-rf', $this->archiveDir], $this->rootDir, new \Symfony\Component\Console\Output\NullOutput());
@ -395,14 +429,4 @@ class UpdateSoftwareCommand extends Command
$state['log'] .= $output->getVerbosity() >= OutputInterface::VERBOSITY_NORMAL ? $message . "\n" : ''; $state['log'] .= $output->getVerbosity() >= OutputInterface::VERBOSITY_NORMAL ? $message . "\n" : '';
file_put_contents($this->stateFile, json_encode($state)); file_put_contents($this->stateFile, json_encode($state));
} }
private function backupArchive(): string
{
$tarFile = $this->backupDir . '/hesabixArchive_backup_' . time() . '.tar';
$this->runProcess(['tar', '-cf', $tarFile, '-C', $this->rootDir, 'hesabixArchive'], $this->rootDir, new \Symfony\Component\Console\Output\NullOutput());
if (!file_exists($tarFile)) {
throw new \RuntimeException('Failed to create tar backup of hesabixArchive.');
}
return $tarFile;
}
} }