diff --git a/.gitignore b/.gitignore index a67f91e..0216fdd 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,5 @@ /var/ /vendor/ ###< symfony/framework-bundle ### + +*_backup.sql diff --git a/composer.json b/composer.json index d2cdbef..6ac3e45 100644 --- a/composer.json +++ b/composer.json @@ -11,6 +11,7 @@ "symfony/console": "5.3.*", "symfony/dotenv": "5.3.*", "symfony/filesystem": "5.3.*", + "symfony/finder": "5.3.*", "symfony/flex": "^1.3.1", "symfony/framework-bundle": "5.3.*", "symfony/process": "5.3.*", diff --git a/composer.lock b/composer.lock index 7fb7d0c..63a89c8 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "8c7050f7bee5a93ee3509d8014488736", + "content-hash": "9ee32d11a76246681509a53554f766a3", "packages": [ { "name": "psr/cache", diff --git a/src/Console/BackupDatabase.php b/src/Console/BackupDatabase.php index 1ac6e4e..f996a1b 100644 --- a/src/Console/BackupDatabase.php +++ b/src/Console/BackupDatabase.php @@ -33,8 +33,8 @@ class BackupDatabase extends Command { $db = $this->getDatabaseName($input, $output); - $output = $this->bs->backup($db); - dump($output); + $backupName = $this->bs->backup($db); + $output->writeln(sprintf('Backup written to: %s', $backupName)); return Command::SUCCESS; } diff --git a/src/Console/RestoreDatabase.php b/src/Console/RestoreDatabase.php new file mode 100644 index 0000000..9200b18 --- /dev/null +++ b/src/Console/RestoreDatabase.php @@ -0,0 +1,52 @@ +addArgument('name', InputArgument::OPTIONAL, 'Database name'); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $question = $this->getHelper('question'); + $db = $this->getDatabaseName($input, $output); + + $finder = new Finder(); + $finder->in(__DIR__ . '/../..'); + $files = [sprintf('%s_backup.sql', $db)]; + foreach ($finder->files()->name('*_backup.sql') as $file) { + $files[] = $file->getFilename(); + } + $selectQuestion = new ChoiceQuestion('Backup name (*_backup.sql): ', $files); + $fileName = $question->ask($input, $output, $selectQuestion); + + $this->bs->restore($db, $fileName); + + return Command::SUCCESS; + } +} \ No newline at end of file diff --git a/src/Service/BackupService.php b/src/Service/BackupService.php index 4c01922..5b898dd 100644 --- a/src/Service/BackupService.php +++ b/src/Service/BackupService.php @@ -3,6 +3,7 @@ namespace App\Service; use Symfony\Component\Filesystem\Filesystem; +use Symfony\Component\Process\InputStream; use Symfony\Component\Process\Process; class BackupService @@ -11,8 +12,12 @@ class BackupService { } - public function backup(string $db): string + public function backup(string $db, ?string $fileName = null): string { + if (!$fileName) { + $fileName = sprintf('%s_backup.sql', $db); + } + $filesystem = new Filesystem(); $process = new Process([ 'mysqldump', @@ -21,10 +26,21 @@ class BackupService ]); $process->setEnv(['MYSQL_PWD' => $this->credentials->getPassword()]); $process->run(); - - $fileName = sprintf('%s_backup.sql', $db); $filesystem->dumpFile($fileName, $process->getOutput()); return $fileName; } + + public function restore(string $db, string $fileName): void + { + $process = new Process([ + 'mysql', + '-u', $this->credentials->getUser(), + $db, + ]); + + $process->setEnv(['MYSQL_PWD' => $this->credentials->getPassword()]); + $process->setInput(file_get_contents($fileName)); + $process->run(); + } } \ No newline at end of file