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