Skip to content

Commit 87c02ab

Browse files
authored
Adding console commands (#22)
* Adding console commands * Adding restore command.
1 parent 2111ad9 commit 87c02ab

File tree

7 files changed

+165
-1
lines changed

7 files changed

+165
-1
lines changed

Command/BackupCommand.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
namespace BM\BackupManagerBundle\Command;
4+
5+
6+
use BackupManager\Config\Config;
7+
use BackupManager\Filesystems\Destination;
8+
use BackupManager\Filesystems\FilesystemProvider;
9+
use BackupManager\Manager;
10+
use Symfony\Component\Console\Command\Command;
11+
use Symfony\Component\Console\Input\InputArgument;
12+
use Symfony\Component\Console\Input\InputInterface;
13+
use Symfony\Component\Console\Input\InputOption;
14+
use Symfony\Component\Console\Output\OutputInterface;
15+
16+
/**
17+
* Backup database.
18+
*
19+
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
20+
*/
21+
class BackupCommand extends Command
22+
{
23+
protected static $defaultName = 'backup-manager:backup';
24+
25+
/**
26+
* @var Manager
27+
*/
28+
private $manager;
29+
30+
/**
31+
* @var string
32+
*/
33+
private $filePrefix;
34+
35+
/**
36+
*
37+
* @param Manager $manager
38+
* @param string $filePrefix
39+
*/
40+
public function __construct(Manager $manager, $filePrefix)
41+
{
42+
$this->manager = $manager;
43+
$this->filePrefix = $filePrefix;
44+
parent::__construct();
45+
}
46+
47+
48+
protected function configure()
49+
{
50+
$this
51+
->setDescription('Starts a new backup.')
52+
->addArgument('database', InputArgument::REQUIRED, 'What database configuration do you want to backup?')
53+
->addArgument('destinations', InputArgument::IS_ARRAY, 'What storages do you want to upload the backup to?')
54+
->addOption('compression', 'c', InputOption::VALUE_OPTIONAL, 'How do you want to compress the file?', 'null')
55+
->addOption('filename', 'name', InputOption::VALUE_OPTIONAL, 'A customized filename', null)
56+
;
57+
}
58+
59+
protected function execute(InputInterface $input, OutputInterface $output)
60+
{
61+
if (null === $filename = $input->getOption('filename')) {
62+
$filename = $this->filePrefix.(new \DateTime())->format('Y-m-d_H-i-s');
63+
}
64+
65+
$destinations = [];
66+
foreach ($input->getArgument('destinations') as $name) {
67+
$destinations[] = new Destination($name, $filename);
68+
}
69+
70+
$this->manager->makeBackup()->run($input->getArgument('database'), $destinations, $input->getOption('compression'));
71+
}
72+
}

Command/RestoreCommand.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace BM\BackupManagerBundle\Command;
4+
5+
6+
use BackupManager\Config\Config;
7+
use BackupManager\Filesystems\Destination;
8+
use BackupManager\Filesystems\FilesystemProvider;
9+
use BackupManager\Manager;
10+
use Symfony\Component\Console\Command\Command;
11+
use Symfony\Component\Console\Input\InputArgument;
12+
use Symfony\Component\Console\Input\InputInterface;
13+
use Symfony\Component\Console\Input\InputOption;
14+
use Symfony\Component\Console\Output\OutputInterface;
15+
16+
/**
17+
* Restore from backup.
18+
*
19+
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
20+
*/
21+
class RestoreCommand extends Command
22+
{
23+
protected static $defaultName = 'backup-manager:restore';
24+
25+
/**
26+
* @var Manager
27+
*/
28+
private $manager;
29+
30+
/**
31+
*
32+
* @param Manager $manager
33+
*/
34+
public function __construct(Manager $manager)
35+
{
36+
$this->manager = $manager;
37+
parent::__construct();
38+
}
39+
40+
41+
protected function configure()
42+
{
43+
$this
44+
->setDescription('Restore form backup.')
45+
->addArgument('database', InputArgument::REQUIRED, 'What database configuration do you want to backup?')
46+
->addArgument('destination', InputArgument::REQUIRED, 'What storage do you want to restore from?')
47+
->addArgument('file_path', InputArgument::REQUIRED, 'Where on the storage is the file?')
48+
->addOption('compression', 'c', InputOption::VALUE_OPTIONAL, 'What file compression is used?', 'null')
49+
;
50+
}
51+
52+
protected function execute(InputInterface $input, OutputInterface $output)
53+
{
54+
$this->manager->makeRestore()->run(
55+
$input->getArgument('destination'),
56+
$input->getArgument('file_path'),
57+
$input->getArgument('database'),
58+
$input->getOption('compression')
59+
);
60+
}
61+
62+
}

DependencyInjection/BMBackupManagerExtension.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ public function load(array $configs, ContainerBuilder $container)
4747

4848
$container->getDefinition('backup_manager.config_database')
4949
->replaceArgument(0, $config['database']);
50+
51+
if (isset($config['output_file_prefix'])) {
52+
$container->getDefinition('backup_manager.command.backup')
53+
->replaceArgument(1, $config['output_file_prefix']);
54+
}
5055
}
5156

5257
/**

DependencyInjection/Configuration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public function getConfigTreeBuilder()
7171
->end()
7272
->end()
7373
->end()
74+
->scalarNode('output_file_prefix')->info('Use as a prefix for default backup filename')->end()
7475
->end()
7576
;
7677

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,11 @@ Backup to / restore from any configured database.
131131
Backup the development database to `Amazon S3`. The S3 backup path will be `test/backup.sql.gz` in the end, when `gzip` is done with it.
132132

133133
```php
134-
$this->container->get('backup_manager')->makeBackup()->run('development', 's3', 'test/backup.sql', 'gzip');
134+
$this->container->get('backup_manager')->makeBackup()->run('development', [new Destination('s3', 'test/backup.sql')], 'gzip');
135+
```
136+
137+
```bash
138+
php bin/console backup-manager:backup development s3 -c gzip --filename test/backup.sql
135139
```
136140

137141
Backup to / restore from any configured filesystem.
@@ -143,6 +147,10 @@ Restore the database file `test/backup.sql.gz` from `Amazon S3` to the `developm
143147
$this->container->get('backup_manager')->makeRestore()->run('s3', 'test/backup.sql.gz', 'development', 'gzip');
144148
```
145149

150+
```bash
151+
php bin/console backup-manager:backup development s3 test/backup.sql.gz -c gzip
152+
```
153+
146154
> This package does not allow you to backup from one database type and restore to another. A MySQL dump is not compatible with PostgreSQL.
147155

148156
Requirements

Resources/config/services.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
11
services:
2+
# Commands
3+
backup_manager.command.backup:
4+
class: BM\BackupManagerBundle\Command\BackupCommand
5+
public: false
6+
arguments: ['@backup_manager', '']
7+
tags:
8+
- { name: 'console.command' }
9+
10+
backup_manager.command.restore:
11+
class: BM\BackupManagerBundle\Command\RestoreCommand
12+
public: false
13+
arguments: ['@backup_manager']
14+
tags:
15+
- { name: 'console.command' }
16+
217
# Storage
318
backup_manager.config_storage:
419
class: BackupManager\Config\Config

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"php": "^5.5 || ^7.0",
1515
"backup-manager/backup-manager": "^1.2",
1616
"symfony/config": "^2.7 || ^3.1 || ^4.0",
17+
"symfony/console": "^2.7 || ^3.1 || ^4.0",
1718
"symfony/dependency-injection": "^2.7 || ^3.1 || ^4.0",
1819
"symfony/filesystem": "^2.7 || ^3.1 || ^4.0",
1920
"symfony/framework-bundle": "^2.7 || ^3.0 || ^4.0",

0 commit comments

Comments
 (0)