<?php
/*
* This file is part of EC-CUBE
*
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
*
* http://www.ec-cube.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Customize\Controller\Admin\Backup;
use Eccube\Controller\AbstractController;
use Customize\Service\BackupService;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\HttpKernel\Event\PostResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Routing\Annotation\Route;
use Customize\Form\Type\Admin\BackupType;
use Customize\Repository\BackupDataRepository;
use Customize\Repository\BackupDataTableRepository;
use Customize\Doctrine\DBAL\MultiDbConnectionWrapper;
class BackupController extends AbstractController
{
/**
* @var BackupService
*/
protected $backupService;
/**
* @var BackupDataRepository
*/
protected $backupDataRepository;
/**
* @var BackupDataTableRepository
*/
protected $backupDataTableRepository;
/**
* BackupController constructor.
*
* @param BackupService $backupService
*/
public function __construct(
BackupService $backupService,
BackupDataRepository $backupDataRepository,
BackupDataTableRepository $backupDataTableRepository
) {
$this->backupService = $backupService;
$this->backupDataRepository = $backupDataRepository;
$this->backupDataTableRepository = $backupDataTableRepository;
}
/**
* @Route("/%eccube_admin_route%/setting/system/migrate", name="admin_setting_system_migrate",methods={"GET","POST"})
* @Template("@admin/Backup/index.twig")
*/
public function migrate(Request $request, EventDispatcherInterface $eventDispatcher)
{
$builder = $this->formFactory
->createBuilder(BackupType::class);
$form = $builder->getForm();
$form->handleRequest($request);
if ($request->isXmlHttpRequest()) {
$migrate_data = $form->get('migrate_data')->getData();
$mode = $form->get('mode')->getData();
$connection = $this->entityManager->getConnection();
if(!$connection instanceof MultiDbConnectionWrapper) {
throw new \RuntimeException('Wrong connection');
}
$backupBaseDir = $this->getParameter('eccube_theme_user_data_dir').'/Databackup';
$version = date('Y-m-d_His');
$backupDir = $backupBaseDir.'/'.$version;
$fs = new Filesystem();
$fs->mkdir($backupDir);
$done = false;
if($mode){
//Switch to Source DB
if($mode == 1) //本番DBからテストDBにデータを移行
$db_name = $this->eccubeConfig['production_db_name'];
else //テストDBから本番DBにデータを移行
$db_name = $this->eccubeConfig['test_db_name'];
$connection->selectDatabase($db_name);
//Dump Source DB
$tables = $this->backupService->listTableNames();
foreach ($tables as $table) {
$this->backupService->dumpCSV($table, $backupDir);
}
//Switch to Destination DB
if($mode == 1) //本番DBからテストDBにデータを移行
$db_name = $this->eccubeConfig['test_db_name'];
else //テストDBから本番DBにデータを移行
$db_name = $this->eccubeConfig['production_db_name'];
$connection->selectDatabase($db_name);
//Migrate DB
$this->execMigrate($migrate_data, $version, $connection);
$done = true;
$this->delete($version);
}
//Restore DB Connection
$connection->selectDatabase($this->eccubeConfig['production_db_name']);
return $this->json(['done' => $done]);
}
return [
'form' => $form->createView()
];
}
private function execMigrate($migrate_data, $version, $connection)
{
$backupBaseDir = $this->getParameter('eccube_theme_user_data_dir').'/Databackup';
$backupDir = $backupBaseDir.'/'.$version;
$backup_fail = false;
foreach($migrate_data as $md)
{
$BackupData = $this->backupDataRepository->find($md);
$backupDataTable = $this->backupDataTableRepository->findBy(['BackupData'=>$BackupData]);
foreach($backupDataTable as $table)
{
$tbl_name = $table->getName();
if (file_exists($backupDir.'/'.$tbl_name.'.csv') && filesize($backupDir.'/'.$tbl_name.'.csv') > 0) {
$this->entityManager->beginTransaction();
$connection->exec('SET FOREIGN_KEY_CHECKS = 0;');
$connection->exec("SET SESSION sql_mode = 'NO_AUTO_VALUE_ON_ZERO'"); // STRICT_TRANS_TABLESを無効にする。
$this->backupService->restoreTable($tbl_name, $backupDir, $connection);
}
else {
$backup_fail = true;
break;
}
}
}
return $backup_fail;
}
private function delete($verision)
{
$backupBaseDir = $this->getParameter('eccube_theme_user_data_dir').'/Databackup';
$backupDir = $backupBaseDir.'/'.$verision;
$fs = new Filesystem();
$fs->remove($backupDir);
}
}