|
1
|
|
|
<?php |
|
2
|
|
|
namespace phpbu\Laravel\Backup\Sync; |
|
3
|
|
|
|
|
4
|
|
|
use Illuminate\Support\Facades\Storage; |
|
5
|
|
|
use phpbu\App\Backup\Sync\Simulator; |
|
6
|
|
|
use phpbu\App\Backup\Target; |
|
7
|
|
|
use phpbu\App\Exception; |
|
8
|
|
|
use phpbu\App\Result; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class LaravelStorage |
|
12
|
|
|
* |
|
13
|
|
|
* Syncs a backup with the Laravel filesystem classes. |
|
14
|
|
|
* |
|
15
|
|
|
* @package phpbu\Laravel |
|
16
|
|
|
* @author Sebastian Feldmann <[email protected]> |
|
17
|
|
|
* @copyright Sebastian Feldmann <[email protected]> |
|
18
|
|
|
* @license http://www.opensource.org/licenses/MIT The MIT License (MIT) |
|
19
|
|
|
* @link http://phpbu.de/ |
|
20
|
|
|
*/ |
|
21
|
|
|
class LaravelStorage implements Simulator |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* Laravel filesystem to sync to. |
|
25
|
|
|
* |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
private $filesystem; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Path to copy the backup. |
|
32
|
|
|
* |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
private $path; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Setup the source. |
|
39
|
|
|
* |
|
40
|
|
|
* @param array $conf |
|
41
|
|
|
* @throws \phpbu\App\Exception |
|
42
|
|
|
*/ |
|
43
|
|
|
public function setup(array $conf = array()) |
|
44
|
|
|
{ |
|
45
|
|
|
if (empty($conf['filesystem'])) { |
|
46
|
|
|
throw new Exception('no filesystem configured'); |
|
47
|
|
|
} |
|
48
|
|
|
$this->filesystem = $conf['filesystem']; |
|
49
|
|
|
$this->path = !empty($conf['path']) ? $conf['path'] : ''; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Execute the Sync |
|
54
|
|
|
* Copy your backup to another location |
|
55
|
|
|
* |
|
56
|
|
|
* @param \phpbu\App\Backup\Target $target |
|
57
|
|
|
* @param \phpbu\App\Result $result |
|
58
|
|
|
*/ |
|
59
|
|
|
public function sync(Target $target, Result $result) |
|
60
|
|
|
{ |
|
61
|
|
|
$result->debug('syncing backup'); |
|
62
|
|
|
$storage = Storage::disk($this->filesystem); |
|
63
|
|
|
|
|
64
|
|
|
// if a path is specified make sure the directory exists |
|
65
|
|
|
if (!empty($this->path)) { |
|
66
|
|
|
$storage->makeDirectory($this->path); |
|
67
|
|
|
} |
|
68
|
|
|
$storage->getDriver()->writeStream( |
|
69
|
|
|
$this->path . '/' . $target->getFilename(), |
|
70
|
|
|
fopen($target->getPathname(), 'r+') |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Simulate the sync execution. |
|
76
|
|
|
* |
|
77
|
|
|
* @param \phpbu\App\Backup\Target $target |
|
78
|
|
|
* @param \phpbu\App\Result $result |
|
79
|
|
|
*/ |
|
80
|
|
|
public function simulate(Target $target, Result $result) |
|
81
|
|
|
{ |
|
82
|
|
|
$result->debug( |
|
83
|
|
|
'sync backup to ' . $this->filesystem . PHP_EOL |
|
84
|
|
|
. ' target path: ' . $this->path |
|
85
|
|
|
); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|