1
|
|
|
<?php namespace Backpack\BackupManager\app\Http\Controllers; |
2
|
|
|
|
3
|
|
|
use App\Http\Requests; |
4
|
|
|
use App\Http\Controllers\Controller; |
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use Auth; |
7
|
|
|
use App; |
8
|
|
|
use Storage; |
9
|
|
|
use Carbon\Carbon; |
10
|
|
|
use Artisan; |
11
|
|
|
use Log; |
12
|
|
|
|
13
|
|
|
class BackupController extends Controller { |
14
|
|
|
|
15
|
|
|
public function index() |
|
|
|
|
16
|
|
|
{ |
17
|
|
|
if (!count(config('laravel-backup.backup.destination.disks'))) { |
18
|
|
|
dd(trans('backpack::backup.no_disks_configured')); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
$this->data['backups'] = []; |
22
|
|
|
|
23
|
|
|
foreach (config('laravel-backup.backup.destination.disks') as $disk_name) { |
24
|
|
|
$disk = Storage::disk($disk_name); |
25
|
|
|
$adapter = $disk->getDriver()->getAdapter(); |
26
|
|
|
$files = $disk->files(); |
27
|
|
|
|
28
|
|
|
// make an array of backup files, with their filesize and creation date |
29
|
|
|
foreach ($files as $k => $f) { |
30
|
|
|
// only take the zip files into account |
31
|
|
|
if (substr($f, -4) == '.zip' && $disk->exists($f)) { |
32
|
|
|
$this->data['backups'][] = [ |
33
|
|
|
'file_path' => $f, |
34
|
|
|
'file_name' => str_replace('backups/', '', $f), |
35
|
|
|
'file_size' => $disk->size($f), |
36
|
|
|
'last_modified' => $disk->lastModified($f), |
37
|
|
|
'disk' => $disk_name, |
38
|
|
|
'download' => ($adapter instanceof \League\Flysystem\Adapter\Local)?true:false |
39
|
|
|
]; |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
// reverse the backups, so the newest one would be on top |
45
|
|
|
$this->data['backups'] = array_reverse($this->data['backups']); |
46
|
|
|
$this->data['title'] = 'Backups'; |
47
|
|
|
|
48
|
|
|
return view("backupmanager::backup", $this->data); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function create() |
52
|
|
|
{ |
53
|
|
|
try { |
54
|
|
|
ini_set('max_execution_time', 300); |
55
|
|
|
// start the backup process |
56
|
|
|
Artisan::call('backup:run'); |
57
|
|
|
$output = Artisan::output(); |
58
|
|
|
|
59
|
|
|
// log the results |
60
|
|
|
Log::info("Backpack\BackupManager -- new backup started from admin interface \r\n".$output); |
61
|
|
|
// return the results as a response to the ajax call |
62
|
|
|
echo($output); |
63
|
|
|
} catch (Exception $e) { |
|
|
|
|
64
|
|
|
Response::make($e->getMessage(), 500); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return 'success'; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Downloads a backup zip file. |
72
|
|
|
*/ |
73
|
|
|
public function download($file_name) |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
$disk = Storage::disk(\Request::input('disk')); |
76
|
|
|
$adapter = $disk->getDriver()->getAdapter(); |
77
|
|
|
|
78
|
|
|
if ($adapter instanceof \League\Flysystem\Adapter\Local) { |
79
|
|
|
$storage_path = $disk->getDriver()->getAdapter()->getPathPrefix(); |
80
|
|
|
|
81
|
|
|
if ($disk->exists($file_name)) { |
82
|
|
|
return response()->download($storage_path.$file_name); |
83
|
|
|
} |
84
|
|
|
else |
85
|
|
|
{ |
86
|
|
|
abort(404, trans('backpack::backup.backup_doesnt_exist')); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
else |
90
|
|
|
{ |
91
|
|
|
abort(404, trans('backpack::backup.only_local_downloads_supported')); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Deletes a backup file. |
97
|
|
|
*/ |
98
|
|
|
public function delete($file_name) |
|
|
|
|
99
|
|
|
{ |
100
|
|
|
$disk = Storage::disk(\Request::input('disk')); |
101
|
|
|
|
102
|
|
|
if ($disk->exists($file_name)) { |
103
|
|
|
$disk->delete($file_name); |
104
|
|
|
|
105
|
|
|
return 'success'; |
106
|
|
|
} |
107
|
|
|
else |
108
|
|
|
{ |
109
|
|
|
abort(404, trans('backpack::backup.backup_doesnt_exist')); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.