ajout d'un job de dump bdd (ref #158)

This commit is contained in:
afornerot 2020-06-16 10:06:13 +02:00
parent dfe6a90673
commit 3c08e2ef8f
6 changed files with 123 additions and 9 deletions

View File

@ -0,0 +1,95 @@
<?php
namespace Cadoles\CronBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\Response;
use Cadoles\CronBundle\Entity\Cron;
class DumpCommand extends ContainerAwareCommand
{
private $container;
private $em;
private $output;
private $filesystem;
private $rootlog;
private $datahost;
private $database;
private $username;
private $password;
private $path;
protected function configure()
{
$this
->setName('Cron:Dump')
->setDescription('Dump database for backup')
->addArgument('env', InputArgument::OPTIONAL, 'env Mail')
->addArgument('cronid', InputArgument::OPTIONAL, 'ID Cron Job')
->addArgument('lastchance', InputArgument::OPTIONAL, 'Lastchance to run the cron')
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->container = $this->getApplication()->getKernel()->getContainer();
$this->em = $this->container->get('doctrine')->getEntityManager();
$this->output = $output;
$this->filesystem = new Filesystem();
$this->rootlog = $this->container->get('kernel')->getRootDir()."/../var/logs/";
$mailer_host = $this->getContainer()->getParameter('mailer_host');
$this->writelnred('');
$this->writelnred('== Cron:Dump');
$this->writelnred('==========================================================================================================');
$this->datahost = $this->getContainer()->getParameter('database_host');
$this->database = $this->getContainer()->getParameter('database_name') ;
$this->username = $this->getContainer()->getParameter('database_user') ;
$this->password = $this->getContainer()->getParameter('database_password') ;
$cmd = sprintf('mysqldump -h %s -B %s -u %s --password=%s'
, $this->datahost
, $this->database
, $this->username
, $this->password
);
$result = $this->runCommand($cmd);
if($result['exit_status'] == 0) {
$this->filesystem->dumpFile($this->rootlog."ninegate.sql", $result['output']);
}
$this->writeln('');
return 1;
}
protected function runCommand($command)
{
$command .=" >&1";
exec($command, $output, $exit_status);
return array(
"output" => $output
, "exit_status" => $exit_status
);
}
private function writelnred($string) {
$this->output->writeln('<fg=red>'.$string.'</>');
$this->filesystem->appendToFile($this->rootlog.'cron.log', $string."\n");
}
private function writeln($string) {
$this->output->writeln($string);
$this->filesystem->appendToFile($this->rootlog.'cron.log', $string."\n");
}
}

View File

@ -158,6 +158,24 @@ class InitDataCommand extends ContainerAwareCommand
$this->entityManager->persist($entity);
}
// Job Dump
// Toute les 24h à 2h00
$entity = $this->entityManager->getRepository('CadolesCronBundle:Cron')->find(220);
if(!$entity) {
$entity = new Cron;
$nextdate=$entity->getSubmitdate();
$nextdate->setTime(2,0);
$entity->setCommand("Cron:Dump");
$entity->setDescription("Sauvegarde de la BDD");
$entity->setId(220);
$entity->setStatut(2);
$entity->setRepeatcall(0);
$entity->setRepeatexec(0);
$entity->setRepeatinterval(86400);
$entity->setNextexecdate($nextdate);
$this->entityManager->persist($entity);
}
// CRON PORTAIL
// Job purge des registrations obsolètes
// Toute les 5mn

View File

@ -73,7 +73,7 @@ class CronController extends Controller
// Order
switch($order[0]["column"]) {
case 1 :
$qb->orderBy('table.id',$order[0]["dir"]);
$qb->orderBy('table.nextexecdate',$order[0]["dir"]);
break;
case 2 :
$qb->orderBy('table.command',$order[0]["dir"]);
@ -84,9 +84,6 @@ class CronController extends Controller
case 4 :
$qb->orderBy('table.statut',$order[0]["dir"]);
break;
case 5 :
$qb->orderBy('table.nextexecdate',$order[0]["dir"]);
break;
}
// Execution de la requete d'affichage
@ -107,7 +104,7 @@ class CronController extends Controller
if($data->getRepeatCall()>0 AND $data->getRepeatCall()<=$data->getRepeatExec())
$nextexec="Déjà rejoué ".$data->getRepeatExec()." fois";
array_push($output["data"],array($action,$data->getId(),$data->getCommand(),$data->getDescription(),$data->getStatutLabel(),$nextexec));
array_push($output["data"],array($action,$nextexec,$data->getCommand(),$data->getDescription(),$data->getStatutLabel()));
}
// Retour
@ -204,7 +201,11 @@ class CronController extends Controller
public function getlogAction(Request $request, $id)
{
$kernel = $this->get('kernel');
$file = $this->get('kernel')->getRootDir() . '/../var/logs/'.$id.'.log';
if($id=="dump")
$file = $this->get('kernel')->getRootDir() . '/../var/logs/ninegate.sql';
else
$file = $this->get('kernel')->getRootDir() . '/../var/logs/'.$id.'.log';
$response = new BinaryFileResponse($file);
$response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT);
return $response;

View File

@ -17,11 +17,10 @@
<thead>
<tr>
<th width="70px" class="no-sort">Action</th>
<th>Order</th>
<th>Prochaine exécution</th>
<th>Command</th>
<th>Description</th>
<th>Statut</th>
<th>Prochaine exécution</th>
</tr>
</thead>
</table>

View File

@ -6,4 +6,5 @@
<a class="btn btn-default" href={{ path("cadoles_cron_config_getlog",{"id":"cron"}) }}>Log CRON</a>
<a class="btn btn-default" href={{ path("cadoles_cron_config_getlog",{"id":"prod"}) }}>Log PROD</a>
<a class="btn btn-default" href={{ path("cadoles_cron_config_getlog",{"id":"dev"}) }}>Log DEV</a>
<a class="btn btn-default" href={{ path("cadoles_cron_config_getlog",{"id":"dump"}) }}>Dump de la Base</a>
{% endblock %}

View File

@ -83,7 +83,7 @@ INSERT IGNORE INTO `sidebar` (`id`, `parent_id`, `roworder`, `label`, `path`, `f
(7000, NULL, 7000, 'CRON', NULL, 'fa-bolt', 'ROLE_ADMIN,ROLE_MODO', 'cron_activate'),
(7010, 7000, 7010, 'Jobs', 'cadoles_cron_config', 'fa-bullseye', 'ROLE_ADMIN,ROLE_MODO', 'cron_activate'),
(7020, 7000, 7020, 'Logs', 'cadoles_cron_config_log', 'fa-list-alt', 'ROLE_ADMIN,ROLE_MODO', 'cron_activate');
(7020, 7000, 7020, 'Logs / Dump', 'cadoles_cron_config_log', 'fa-list-alt', 'ROLE_ADMIN,ROLE_MODO', 'cron_activate');