Skip to content

Commit 1126ac1

Browse files
committed
Merge pull request #1 from MatthiasDeWinter/master
DB dumper for Laravel 5, only for mySQL db's
2 parents 737837c + a9cc720 commit 1126ac1

File tree

9 files changed

+152
-35
lines changed

9 files changed

+152
-35
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"require": {
2323
"php" : ">=5.4.0",
2424
"illuminate/support": "5.0.x",
25-
"illuminate/console": "5.0.x"
25+
"illuminate/console": "5.0.x",
26+
"fabpot/php-cs-fixer": "~1.4"
2627
},
2728
"require-dev": {
2829
"phpunit/phpunit" : "4.*",

src/Assets/config/laravel-backup.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<?php
33

44
return [
5-
'path' => storage_path() . '/db-dumps/',
5+
'path' => storage_path().'/db-dumps/',
66

77
'mysql' => array(
88
'dump_command_path' => '',

src/Commands/BackupCommand.php

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,69 @@
11
<?php namespace Spatie\DatabaseBackup\Commands;
22

3-
use Illuminate\Console\Command;
4-
5-
class BackupCommand extends Command
3+
class BackupCommand extends BaseCommand
64
{
75
/**
86
* The console command name.
97
*
108
* @var string
119
*/
12-
protected $name = 'db.backup';
10+
protected $name = 'db:backup';
11+
1312
/**
1413
* The console command description.
1514
*
1615
* @var string
1716
*/
1817
protected $description = 'Backup the database to a file';
19-
/**
20-
* Create a new command instance.
21-
*
22-
*/
23-
public function __construct()
24-
{
25-
parent::__construct();
26-
}
18+
19+
protected $filePath;
20+
protected $fileName;
21+
2722
/**
2823
* Execute the console command.
2924
* @return mixed
3025
* @throws Exception
3126
*/
3227
public function fire()
3328
{
29+
$this->info('Starting database dump...');
30+
31+
$database = $this->getDatabase([]);
32+
33+
$this->checkDumpFolder();
34+
35+
$this->fileName = date('YmdHis').'.'.$database->getFileExtension();
36+
$this->filePath = rtrim($this->getDumpsPath(), '/').'/'.$this->fileName;
3437

38+
$status = $database->dump($this->filePath);
39+
40+
if ($status === true) {
41+
$this->info('Database dumped successful in:');
42+
$this->comment($this->filePath);
43+
}
44+
}
45+
46+
protected function getArguments()
47+
{
48+
return [
49+
];
3550
}
3651

52+
protected function getOptions()
53+
{
54+
return [
55+
];
56+
}
3757

38-
}
58+
/**
59+
* Checks if dump-folder already exists
60+
*/
61+
protected function checkDumpFolder()
62+
{
63+
$dumpsPath = $this->getDumpsPath();
64+
65+
if (!is_dir($dumpsPath)) {
66+
mkdir($dumpsPath);
67+
}
68+
}
69+
}

src/Commands/BaseCommand.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php namespace Spatie\DatabaseBackup\Commands;
2+
3+
use Illuminate\Console\Command;
4+
use Spatie\DatabaseBackup\DatabaseBuilder;
5+
use Spatie\DatabaseBackup\Console;
6+
use Config;
7+
8+
class BaseCommand extends Command
9+
{
10+
11+
protected $databaseBuilder;
12+
protected $console;
13+
14+
public function __construct(DatabaseBuilder $databaseBuilder)
15+
{
16+
parent::__construct();
17+
18+
$this->databaseBuilder = $databaseBuilder;
19+
$this->console = new Console();
20+
}
21+
22+
/**
23+
* Get database configuration
24+
*
25+
* @param $database
26+
* @return mixed
27+
* @throws \Exception
28+
*/
29+
public function getDatabase($database)
30+
{
31+
$database = $database ?: Config::get('database.default');
32+
$realConfig = Config::get('database.connections.'.$database);
33+
34+
return $this->databaseBuilder->getDatabase($realConfig);
35+
}
36+
37+
/**
38+
* Gets the path to dump folder from the config
39+
*
40+
* @return mixed
41+
*/
42+
protected function getDumpsPath()
43+
{
44+
return Config::get('laravel-backup.path');
45+
}
46+
}

src/Console.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,10 @@ public function run($command)
1212

1313
$process->run();
1414

15-
if ($process->isSuccessful())
16-
{
15+
if ($process->isSuccessful()) {
1716
return true;
18-
}
19-
else
20-
{
17+
} else {
2118
return $process->getErrorOutput();
2219
}
2320
}
24-
}
21+
}

src/DatabaseBackupServiceProvider.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
<?php
2-
3-
namespace Spatie\DatabaseBackup;
1+
<?php namespace Spatie\DatabaseBackup;
42

53
use Illuminate\Support\ServiceProvider;
64

@@ -21,6 +19,9 @@ class DatabaseBackupServiceProvider extends ServiceProvider
2119
*/
2220
public function boot()
2321
{
22+
$this->publishes([
23+
__DIR__.'/Assets/config/laravel-backup.php' => config_path('laravel-backup.php'),
24+
]);
2425
}
2526

2627
/**
@@ -30,13 +31,15 @@ public function boot()
3031
*/
3132
public function register()
3233
{
33-
$this->app['command.db.backup'] = $this->app->share(
34-
function ($app) {
35-
return new Commands\BackupCommand();
34+
$databaseBuilder = new DatabaseBuilder();
35+
36+
$this->app['command.db:backup'] = $this->app->share(
37+
function ($app) use ($databaseBuilder) {
38+
return new Commands\BackupCommand($databaseBuilder);
3639
}
3740
);
3841

39-
$this->commands('command.db.backup');
42+
$this->commands('command.db:backup');
4043
}
4144

4245
/**
@@ -46,6 +49,6 @@ function ($app) {
4649
*/
4750
public function provides()
4851
{
49-
return ['command.db.backup'];
52+
return ['command.db:backup'];
5053
}
51-
}
54+
}

src/DatabaseBuilder.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php namespace Spatie\DatabaseBackup;
2+
3+
use Exception;
4+
5+
class DatabaseBuilder
6+
{
7+
8+
protected $database;
9+
protected $console;
10+
11+
public function __construct()
12+
{
13+
$this->console = new Console();
14+
}
15+
16+
public function getDatabase(array $realConfig)
17+
{
18+
try {
19+
$this->buildMySQL($realConfig);
20+
} catch (Exception $e) {
21+
throw new \Exception('Whoops, '.$e->getMessage());
22+
}
23+
24+
return $this->database;
25+
}
26+
27+
protected function buildMySQL(array $config)
28+
{
29+
$port = isset($config['port']) ? $config['port'] : 3306;
30+
31+
$this->database = new Databases\MySQLDatabase(
32+
$this->console,
33+
$config['database'],
34+
$config['username'],
35+
$config['password'],
36+
$config['host'],
37+
$port
38+
);
39+
}
40+
}

src/Databases/DatabaseInterface.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,3 @@ public function restore($sourceFile);
2525
*/
2626
public function getFileExtension();
2727
}
28-

src/Databases/MySQLDatabase.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php namespace Spatie\DatabaseBackup\Databases;
22

33
use Spatie\DatabaseBackup\Console;
4-
54
use Config;
65

76
class MySQLDatabase implements DatabaseInterface
@@ -34,6 +33,7 @@ public function dump($destinationFile)
3433
escapeshellarg($this->database),
3534
escapeshellarg($destinationFile)
3635
);
36+
3737
return $this->console->run($command);
3838
}
3939
public function restore($sourceFile)
@@ -47,6 +47,7 @@ public function restore($sourceFile)
4747
escapeshellarg($this->database),
4848
escapeshellarg($sourceFile)
4949
);
50+
5051
return $this->console->run($command);
5152
}
5253

@@ -57,7 +58,6 @@ public function getFileExtension()
5758

5859
protected function getDumpCommandPath()
5960
{
60-
return Config::get('backup::mysql.dump_command_path');;
61+
return Config::get('backup::mysql.dump_command_path');
6162
}
62-
63-
}
63+
}

0 commit comments

Comments
 (0)