app/Customize/Controller/Admin/Backup/BackupController.php line 68

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
  6.  *
  7.  * http://www.ec-cube.co.jp/
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Customize\Controller\Admin\Backup;
  13. use Eccube\Controller\AbstractController;
  14. use Customize\Service\BackupService;
  15. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  16. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  17. use Symfony\Component\Filesystem\Filesystem;
  18. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Symfony\Component\HttpFoundation\ResponseHeaderBag;
  21. use Symfony\Component\HttpKernel\Event\PostResponseEvent;
  22. use Symfony\Component\HttpKernel\KernelEvents;
  23. use Symfony\Component\Routing\Annotation\Route;
  24. use Customize\Form\Type\Admin\BackupType;
  25. use Customize\Repository\BackupDataRepository;
  26. use Customize\Repository\BackupDataTableRepository;
  27. use Customize\Doctrine\DBAL\MultiDbConnectionWrapper;
  28. class BackupController extends AbstractController
  29. {
  30.     /**
  31.      * @var BackupService
  32.      */
  33.     protected $backupService;
  34.     /**
  35.      * @var BackupDataRepository
  36.      */
  37.     protected $backupDataRepository
  38.     /**
  39.      * @var BackupDataTableRepository
  40.      */
  41.     protected $backupDataTableRepository;      
  42.     /**
  43.      * BackupController constructor.
  44.      *
  45.      * @param BackupService $backupService
  46.      */
  47.     public function __construct(
  48.         BackupService $backupService,
  49.         BackupDataRepository $backupDataRepository,
  50.         BackupDataTableRepository $backupDataTableRepository                
  51.     ) {
  52.         $this->backupService $backupService;
  53.         $this->backupDataRepository $backupDataRepository;
  54.         $this->backupDataTableRepository $backupDataTableRepository;        
  55.     }
  56.     /**
  57.      * @Route("/%eccube_admin_route%/setting/system/migrate", name="admin_setting_system_migrate",methods={"GET","POST"})
  58.      * @Template("@admin/Backup/index.twig")
  59.      */
  60.     public function migrate(Request $requestEventDispatcherInterface $eventDispatcher)
  61.     {
  62.         $builder $this->formFactory
  63.             ->createBuilder(BackupType::class);
  64.         $form $builder->getForm();            
  65.         $form->handleRequest($request);
  66.         if ($request->isXmlHttpRequest()) {
  67.             $migrate_data $form->get('migrate_data')->getData();
  68.             $mode $form->get('mode')->getData();
  69.             $connection $this->entityManager->getConnection();
  70.             if(!$connection instanceof MultiDbConnectionWrapper) {
  71.                 throw new \RuntimeException('Wrong connection');
  72.             }
  73.             $backupBaseDir $this->getParameter('eccube_theme_user_data_dir').'/Databackup';
  74.             $version date('Y-m-d_His');
  75.             $backupDir $backupBaseDir.'/'.$version;
  76.             $fs = new Filesystem();
  77.             $fs->mkdir($backupDir);
  78.             $done false;
  79.             if($mode){
  80.                 //Switch to Source DB
  81.                 if($mode == 1)  //本番DBからテストDBにデータを移行
  82.                     $db_name $this->eccubeConfig['production_db_name'];
  83.                 else            //テストDBから本番DBにデータを移行
  84.                     $db_name $this->eccubeConfig['test_db_name'];
  85.                 $connection->selectDatabase($db_name);
  86.                 //Dump Source DB
  87.                 $tables $this->backupService->listTableNames();
  88.                 foreach ($tables as $table) {
  89.                     $this->backupService->dumpCSV($table$backupDir);
  90.                 }
  91.                 //Switch to Destination DB
  92.                 if($mode == 1)  //本番DBからテストDBにデータを移行
  93.                     $db_name $this->eccubeConfig['test_db_name'];
  94.                 else            //テストDBから本番DBにデータを移行
  95.                     $db_name $this->eccubeConfig['production_db_name'];
  96.                 $connection->selectDatabase($db_name);
  97.                 //Migrate DB
  98.                 $this->execMigrate($migrate_data$version$connection);
  99.                 $done true;
  100.                 $this->delete($version);
  101.             }
  102.             //Restore DB Connection
  103.             $connection->selectDatabase($this->eccubeConfig['production_db_name']);
  104.             return $this->json(['done' => $done]);
  105.         }
  106.         return [
  107.             'form' => $form->createView()
  108.         ];
  109.     }  
  110.     private function execMigrate($migrate_data$version$connection)
  111.     {
  112.         $backupBaseDir $this->getParameter('eccube_theme_user_data_dir').'/Databackup';        
  113.         $backupDir $backupBaseDir.'/'.$version;
  114.               
  115.         $backup_fail false;            
  116.         foreach($migrate_data as $md)
  117.         {
  118.             $BackupData $this->backupDataRepository->find($md);
  119.             $backupDataTable $this->backupDataTableRepository->findBy(['BackupData'=>$BackupData]);
  120.             foreach($backupDataTable as $table)
  121.             {
  122.                 $tbl_name $table->getName();
  123.                 if (file_exists($backupDir.'/'.$tbl_name.'.csv') && filesize($backupDir.'/'.$tbl_name.'.csv') > 0) {
  124.                     $this->entityManager->beginTransaction();
  125.                     $connection->exec('SET FOREIGN_KEY_CHECKS = 0;');
  126.                     $connection->exec("SET SESSION sql_mode = 'NO_AUTO_VALUE_ON_ZERO'"); // STRICT_TRANS_TABLESを無効にする。
  127.                     $this->backupService->restoreTable($tbl_name$backupDir$connection);
  128.                 }
  129.                 else {
  130.                     $backup_fail true;
  131.                     break;
  132.                 }
  133.             }
  134.         }
  135.         return $backup_fail;
  136.     }
  137.     private function delete($verision)
  138.     {
  139.         $backupBaseDir $this->getParameter('eccube_theme_user_data_dir').'/Databackup';
  140.         $backupDir $backupBaseDir.'/'.$verision;
  141.         $fs = new Filesystem();
  142.         $fs->remove($backupDir);
  143.     }        
  144. }