Skip to content

Commit 57fbe47

Browse files
committed
use Laravel 5 filesystem
1 parent e60761c commit 57fbe47

File tree

6 files changed

+78
-54
lines changed

6 files changed

+78
-54
lines changed

README.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,19 @@ This is the contents of the configuration. These options should be self-explanat
4444
return [
4545

4646
/*
47-
* The directory where the database dumps will be saved.
48-
* A .gitignore file will be automatically placed in this directory
49-
* so you don't accidentally end up committing these dumps.
47+
* The filesystem you want to use. Choose one of the filesystems you
48+
* configured in app/config/filesystems.php
49+
*/
50+
'filesystem' => 'local',
51+
52+
/*
53+
* The path where the database dumps will be saved. This path
54+
* is related to the path you configured with your chosen
55+
* filesystem
56+
*
57+
* If you're using the local filesystem a .gitignore file will
58+
* be automatically placed in this directory so you don't
59+
* accidentally end up committing these dumps.
5060
*/
5161
'path' => storage_path('db-dumps'),
5262

src/Assets/config/laravel-backup.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,19 @@
44
return [
55

66
/*
7-
* The directory where the database dumps will be saved.
8-
* A .gitignore file will be automatically placed in this directory
9-
* so you don't accidentally end up committing these dumps.
7+
* The filesystem you want to use. Choose one of the filesystems you
8+
* configured in app/config/filesystems.php
9+
*/
10+
'filesystem' => 'local',
11+
12+
/*
13+
* The path where the database dumps will be saved. This path
14+
* is related to the path you configured with your chosen
15+
* filesystem
16+
*
17+
* If you're using the local filesystem a .gitignore file will
18+
* be automatically placed in this directory so you don't
19+
* accidentally end up committing these dumps.
1020
*/
1121
'path' => storage_path('db-dumps'),
1222

@@ -19,4 +29,3 @@
1929
'dump_command_path' => '',
2030
),
2131
];
22-

src/Commands/BackupCommand.php

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<?php namespace Spatie\DatabaseBackup\Commands;
22

3+
use Exception;
4+
use Storage;
5+
36
class BackupCommand extends BaseCommand
47
{
58
/**
@@ -17,53 +20,70 @@ class BackupCommand extends BaseCommand
1720
protected $description = 'Backup the database to a file';
1821

1922
protected $filePath;
23+
2024
protected $fileName;
2125

26+
/**
27+
* The disk on which the dumps will be stored
28+
*
29+
* @var Illuminate\Contracts\Filesystem\Factory
30+
*/
31+
protected $disk;
32+
2233
/**
2334
* Execute the console command.
2435
* @return mixed
2536
* @throws Exception
2637
*/
2738
public function fire()
2839
{
40+
$this->disk = Storage::disk(config('laravel-backup.filesystem'));
41+
42+
if (config('laravel-backup.filesystem') == 'local')
43+
{
44+
$this->writeIgnoreFile();
45+
}
46+
2947
$this->info('Starting database dump...');
3048

3149
$database = $this->getDatabase([]);
3250

33-
$this->checkDumpFolder();
51+
$this->createDumpFolder();
3452

35-
$this->fileName = date('YmdHis').'.'.$database->getFileExtension();
36-
$this->filePath = rtrim($this->getDumpsPath(), '/').'/'.$this->fileName;
53+
$dumpFileName = date('YmdHis').'.'.$database->getFileExtension();
54+
$dumpFile = rtrim($this->getDumpsPath(), '/').'/' . $dumpFileName;
3755

38-
$status = $database->dump($this->filePath);
56+
$tempFileHandle = tmpfile();
57+
$tempFile = stream_get_meta_data($tempFileHandle)['uri'];
3958

40-
if ($status === true) {
41-
$this->info('Database dumped successful in:');
42-
$this->comment($this->filePath);
59+
$success = $database->dump($tempFile);
60+
61+
if (! $success)
62+
{
63+
throw new Exception('could not dump database');
4364
}
44-
}
4565

46-
protected function getArguments()
47-
{
48-
return [
49-
];
50-
}
66+
$this->disk->put($dumpFile, file_get_contents($tempFile));
67+
68+
$this->info('Database dumped successful on ' . config('laravel-backup.filesystem') . '-filesystem in file ' . $dumpFile);
69+
70+
5171

52-
protected function getOptions()
53-
{
54-
return [
55-
];
5672
}
5773

5874
/**
59-
* Checks if dump-folder already exists
75+
* Create dump-folder
6076
*/
61-
protected function checkDumpFolder()
77+
protected function createDumpFolder()
6278
{
6379
$dumpsPath = $this->getDumpsPath();
6480

65-
if (!is_dir($dumpsPath)) {
66-
mkdir($dumpsPath);
67-
}
81+
$this->disk->makeDirectory($dumpsPath);
82+
}
83+
84+
public function writeIgnoreFile()
85+
{
86+
$gitIgnoreContents = '*' . PHP_EOL . '!.gitignore';
87+
$this->disk->put(config('laravel-backup.path') . '/.gitignore', $gitIgnoreContents);
6888
}
6989
}

src/Commands/BaseCommand.php

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

3+
use Exception;
34
use Illuminate\Console\Command;
45
use Spatie\DatabaseBackup\DatabaseBuilder;
56
use Spatie\DatabaseBackup\Console;
6-
use Config;
77

88
class BaseCommand extends Command
99
{
@@ -28,8 +28,14 @@ public function __construct(DatabaseBuilder $databaseBuilder)
2828
*/
2929
public function getDatabase($database)
3030
{
31-
$database = $database ?: Config::get('database.default');
32-
$realConfig = Config::get('database.connections.'.$database);
31+
$database = $database ?: config('database.default');
32+
33+
if ($database != 'mysql')
34+
{
35+
throw new Exception('laravel-backup can only backup mysql databases');
36+
}
37+
38+
$realConfig = config('database.connections.'.$database);
3339

3440
return $this->databaseBuilder->getDatabase($realConfig);
3541
}
@@ -41,6 +47,6 @@ public function getDatabase($database)
4147
*/
4248
protected function getDumpsPath()
4349
{
44-
return Config::get('laravel-backup.path');
50+
return config('laravel-backup.path');
4551
}
4652
}

src/DatabaseBackupServiceProvider.php

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ public function boot()
2222
$this->publishes([
2323
__DIR__.'/Assets/config/laravel-backup.php' => config_path('laravel-backup.php'),
2424
]);
25-
26-
$backupConfig = config('laravel-backup');
27-
28-
$this->writeIgnoreFile($backupConfig['path']);
2925
}
3026

3127
/**
@@ -55,19 +51,4 @@ public function provides()
5551
{
5652
return ['command.db:backup'];
5753
}
58-
59-
/**
60-
* Copy the gitignore stub to the given directory
61-
*
62-
* @param $directory
63-
*/
64-
public function writeIgnoreFile($directory)
65-
{
66-
$destinationFile = $directory.'/.gitignore';
67-
68-
if(!file_exists($destinationFile))
69-
{
70-
$this->app['files']->copy(__DIR__.'/../stubs/gitignore.txt', $destinationFile);
71-
}
72-
}
7354
}

stubs/gitignore.txt

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)